从 Ansible 下载 Windows Server 2012 R2 和 AWS S3 [英] Windows Server 2012 R2 and AWS S3 Download from Ansible
问题描述
我正在尝试使用适用于 EC2 的 IAM 角色从 AWS S3 下载一些文件,但 Ansible 出现错误.其他 Ansible win_* 模块运行良好.
I'm trying to download some files from AWS S3 with IAM Role for EC2 but Ansible is getting an error. Other Ansible win_* modules works great.
Windows Server 具有 Python2 和 Python3,以及 boto 和 boto3 模块.Cmd 正在响应 python 命令.它在执行时打开 Python3.我还在打开 Python3 时测试了import boto"命令,以确保安装了模块.
Windows Server has Python2 and Python3, and also boto and boto3 modules. Cmd is responding to python command. It opens Python3 when it is executed. I also tested the 'import boto' command when the Python3 is opened to be sure that module is installed.
Ansible Playbook 的配置如下:
Ansible Playbook is configured like:
- name: test s3 module
hosts: windows
tasks:
- name: get s3 file
aws_s3:
bucket: drktests3
object: /test
dest: C:\tests3.txt
mode: get
当我运行这个配置时,输出是这样的:
When i run this configuration, the output is like that:
root@ip-172-31-22-4:/etc/ansible/playbooks# ansible-playbook s3test
PLAY [test s3 module] *******************************************************************************************************************************************
TASK [Gathering Facts] ******************************************************************************************************************************************
ok: [38.210.201.10]
TASK [get s3 file] **********************************************************************************************************************************************
[WARNING]: FATAL ERROR DURING FILE TRANSFER:
fatal: [38.210.201.10]: FAILED! => {"msg": "winrm send_input failed; \nstdout: Unable to initialize device PRN\r\nUnable to initialize device PRN\r\nUnable to initialize device PRN\r\n\nstderr ANSIBALLZ_WRAPPER : The term 'ANSIBALLZ_WRAPPER' is not recognized as the name \r\nof a cmdlet, function, script file, or operable program. Check the spelling of \r\nthe name, or if a path was included, verify that the path is correct and try \r\nagain.\r\nAt line:1 char:1\r\n+ ANSIBALLZ_WRAPPER = True # For test-module script to tell this is a \r\nANSIBALLZ_WR ...\r\n+ ~~~~~~~~~~~~~~~~~\r\n + CategoryInfo : ObjectNotFound: (ANSIBALLZ_WRAPPER:String) [], C \r\n ommandNotFoundException\r\n + FullyQualifiedErrorId :
如果我将主机值更改为 localhost,则相同的脚本可在主服务器(Linux Ubuntu)上运行.为什么 Ansible 无法在 Windows 服务器上执行 python 代码?
The same script works on the Master server(Linux Ubuntu) if i change the hosts value to localhost. Why Ansible cannot execute the python code on the Windows server?
推荐答案
我发现Ansible 文档.
我可以运行 Python 模块吗?不可以,WinRM 连接协议设置为使用 PowerShell 模块,因此 Python 模块将不起作用.绕过此问题的方法是使用 delegate_to:localhost 在 Ansible 控制器上运行 Python 模块.如果在 playbook 期间需要联系外部服务并且没有等效的 Windows 模块可用,这将非常有用.
Can I run Python modules? No, the WinRM connection protocol is set to use PowerShell modules, so Python modules will not work. A way to bypass this issue to use delegate_to: localhost to run a Python module on the Ansible controller. This is useful if during a playbook, an external service needs to be contacted and there is no equivalent Windows module available.
所以如果你想做这样一个过程,你必须解决这个问题.
So if you want to do such a process, you have to workaround the problem.
我通过 Ansible 文档给出的建议解决了这个问题.
I solved it with suggestion that Ansible Documentation gives.
- name: test s3 module
hosts: windows
tasks:
- name: get s3 file
aws_s3:
bucket: bucketname
object: /filename.jpg
dest: /etc/ansible/playbooks/test.jpg
mode: get
delegate_to: localhost
- name: copy to win server
win_copy:
src: /etc/ansible/playbooks/test.jpg
dest: C:/test.jpg
当您使用这些示例代码时,首先您要使用 delegate_to 将文件下载到主 Ansible 服务器.之后,您将文件复制到 Windows 主机.
When you use these sample codes, firstly you are downloading the files to Master Ansible server with delegate_to. And after then that you are copying the files to Windows host.
这篇关于从 Ansible 下载 Windows Server 2012 R2 和 AWS S3的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!