Ansible aws_s3模块发生故障,表明Boto3缺少时不存在

时间:2019-03-22 21:31:30

标签: ansible boto3

此Play会安装python3,pip3,boto3和botocore,并尝试使用aws_s3模块下载文件:

TASK [run yum update -y using yum module] 
**********************************************************************
ok: [ip-10-200-2-137.us-west-2.compute.internal]

TASK [Install python3 and pip3] *************************************************************************************************
changed: [ip-10-200-2-137.us-west-2.compute.internal]

TASK [Install boto3 and botocore with pip3 module] ******************************************************************************
changed: [ip-10-200-2-137.us-west-2.compute.internal]

TASK [Create a directory if it does not exist using file module] ****************************************************************
changed: [ip-10-200-2-137.us-west-2.compute.internal]

TASK [downlod file from s3 with aws_s3 module] **********************************************************************************
fatal: [ip-10-200-2-137.us-west-2.compute.internal]: FAILED! => 
{"changed": false, "msg": "Python modules \"botocore\" or \"boto3\" 
are missing, please install both"}

它失败是因为它说boto3丢失了,但实际上不是:

从目标主机可以看到安装了boto3:

[ec2-user@ip-10-200-2-137 ~]$ pip3 freeze
boto3==1.9.120
botocore==1.12.120
docutils==0.14
jmespath==0.9.4
python-dateutil==2.8.0
s3transfer==0.2.0
six==1.12.0
urllib3==1.24.1
[ec2-user@ip-10-200-2-137 ~]

这是安装boto3的任务:

- name: Install boto3 and botocore with pip3 module
    pip:
      name: 
      - boto3
      - botocore
      executable: pip-3.7

这是失败的任务:

- name: downlod file from s3 with aws_s3 module 
    aws_s3:
      bucket: mybucket
      object: mybucket/jre-8u201-linux-x64.tar.gz
      dest: /home/ec2-user/updater/jre-8u201-linux-x64.tar.gz
      mode: get   

目标主机确实安装了两个版本的Python:

[ec2-user@ip-10-200-2-157 ~]$ which python
/usr/bin/python
[ec2-user@ip-10-200-2-157 ~]$ which python3
/usr/bin/python3

我的配置文件如下:

[defaults]
private_key_file=/home/ec2-user/manual-builds/key.pem
ansible_python_interpreter=/usr/bin/python3

这是一个错误吗?我看到将近一年前有人提出过类似的问题,但我没有解决问题的办法-非常感谢您的帮助

5 个答案:

答案 0 :(得分:1)

可以将/ usr / bin / python用作默认的python解释器。然后,您仅安装适用于python3的AWS库:

- name: Install boto3 and botocore with pip3 module
    pip:
      name: 
      - boto3
      - botocore
      executable: pip-3.7

您可以通过使用pip将AWS Library安装到python2,或者同时安装两者(python3和python2),或者您可以在清单文件中定义:ansible_python_interpreter=/usr/bin/python3,然后将可执行的行为仅限于python3。

答案 1 :(得分:1)

Ansible可能未引用错误的python版本。只需运行ansible version命令即可查看ansible指向的位置。

ansible --version
ansible 2.9.9
  config file = /etc/ansible/ansible.cfg
  configured module search path = [u'/root/.ansible/plugins/modules', u'/usr/share/ansible/plugins/modules']
  ansible python module location = /usr/lib/python2.7/dist-packages/ansible
  executable location = /usr/bin/ansible
  python version = 2.7.17 (default, Apr 15 2020, 17:20:14) [GCC 7.5.0]

如您所见,ansibe指向位于/usr/lib/python2.7/的python模块,您可以查看此python版本是否具有boto3并将botocore软件包安装在/usr/lib/python2.7/dist-packages/路径中。

如果缺少,只需在其中安装boto3和botocore。

答案 2 :(得分:0)

问题是我的剧本有两个任务,而Ansible在第一个和第二个使用python2解释器。第二项任务需要python3解释器才能工作,因此我必须在任务级别进行指定:

- name: downlod file from s3 with aws_s3 module
  vars:
      ansible_python_interpreter: /usr/bin/python3    
  aws_s3:
      bucket: launch-data
      object: jre-8u201-linux-x64.tar.gz
      dest: /home/ec2-user/updater/jre-8u201-linux-x64.tar.gz
      mode: get 

答案 3 :(得分:0)

Ansible说话

“无法导入所需的Python库(botocore或boto3),请阅读模块文档并将其安装在适当的位置。”

模块可能已经安装。通过“ python -m pip list installed”命令进行确认。然后直接在终端中输入“ python”打开有问题的python,然后尝试通过“ import boto3”导入boto3或botocore。如果抛出任何错误,请尝试找出错误。

answer可能有用。

答案 4 :(得分:0)

https://github.com/aws/aws-cli/issues/3092#issuecomment-550281243

卸载 python3-botocore 然后用 pip3 安装似乎可以工作。