首页 文章

Windows Server 2012 R2和AWS S3从Ansible下载

提问于
浏览
0

我正在尝试使用IAM Role for EC2从AWS S3下载一些文件,但Ansible收到错误 . 其他Ansible win_ *模块效果很好 .

Windows Server有Python2和Python3,还有boto和boto3模块 . Cmd正在响应python命令 . 它在执行时打开Python3 . 我还在打开Python3时测试了'import boto'命令,以确保安装了模块 .

Ansible Playbook配置如下:

- name: test s3 module
  hosts: windows
  tasks:
    - name: get s3 file
      aws_s3:
        bucket: drktests3
        object: /test
        dest: C:\tests3.txt
        mode: get

当我运行此配置时,输出是这样的:

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代码?

1 回答

  • 1

    我发现Ansible Docs中有一个关于这个问题的部分 .

    我可以运行Python模块吗?不,WinRM连接协议设置为使用PowerShell模块,因此Python模块将无法工作 . 绕过此问题以使用delegate_to:localhost在Ansible控制器上运行Python模块的方法 . 如果在剧本期间需要联系外部服务并且没有可用的等效Windows模块,这将非常有用 .

    因此,如果您想要执行此类过程,则必须解决此问题 .

    我通过Ansible Documentation提供的建议解决了这个问题 .

    - 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将文件下载到Master Ansible服务器 . 之后您将文件复制到Windows主机 .

相关问题