Windows Server 2012 R2 and AWS S3 Download from Ansible

≡放荡痞女 提交于 2020-08-26 11:18:10

问题


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 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 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 : 

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?


回答1:


I found that there is a section about the problem in Ansible Docs.

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.

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

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.



来源:https://stackoverflow.com/questions/49941248/windows-server-2012-r2-and-aws-s3-download-from-ansible

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!