Ansible EC2:"boto required for this module"

Ansible ec2: "boto required for this module"

当我 运行 这个简单的 Ansible 剧本时:

- name: EC2 Test Example
  hosts: localhost
  connection: local
  gather_facts: False

  tasks:
  - name: EC2 Instance
    ec2:
      # Amazon EC2 key pair name
      key_name: my-key-pair
      # Amazon EC2 Security Group
      group: my-security-group
      instance_type: t2.micro
      # Latest from https://wiki.debian.org/Cloud/AmazonEC2Image/Jessie
      image: ami-221ea342
      wait: yes
    register: ec2

我 运行 与 venv/bin/ansible-playbook -i localhost, playbook.yml:

PLAY [EC2 Test Example] ********************************************************

TASK [EC2 Instance] ************************************************************
fatal: [localhost]: FAILED! => {"changed": false, "failed": true, "msg": "boto required for this module"}
    to retry, use: --limit @/Users/admin/temp/ansec2/playbook.retry

PLAY RECAP *********************************************************************
localhost                  : ok=0    changed=0    unreachable=0    failed=1

很明显,我在我使用的 venv 以及我的默认系统中安装了 boto Python:

➜  ansec2 venv/bin/pip list
Package         Version 
--------------- --------
ansible         2.2.1.0 
boto            2.45.0  
boto3           1.4.4   
botocore        1.5.4   
...

我已经阅读了一些类似的帖子,但没有找到可行的解决方案。

问题的根本原因是 -i localhost, 黑客攻击。你不需要再在 Ansible 中使用它。

你可以 运行:

ansible-playbook playbook.yml

并且在 connection: local 中 Ansible 将使用由 venv 设置的 Python 可执行文件。


当您使用 -i localhost, hack 时,Ansible 调用其默认值 /usr/bin/python

在这种情况下,您仍然可以添加 ansible_python_interpreter 参数来告诉 Ansible 使用此特定环境:

ansible-playbook -i localhost, playbook.yml --extra-vars "ansible_python_interpreter=/Users/admin/temp/ansec2/venv/bin/python" 

但我认为你应该避免它并使用第一种方法。

在我的例子中,消息是因为我缺少 boto,尽管我有 boto3。所以我做了 pip install boto 并修复了它。

如果您已经为要使用的 python 解释器安装了 boto(如 OP 所做的那样),那么您可以告诉 Ansible 使用该 python 解释器,如下所示:

ansible-playbook --extra-vars "ansible_python_interpreter=/path/to/desired/python" playbook.yml

如果要用python3,这个可以

ansible-playbook --extra-vars "ansible_python_interpreter=$(command -v python3)" playbook.yml

如果您还没有安装 boto,则必须先使用 pip 安装它,然后再 运行 您的剧本。如果你想为你的 python3 解释器安装 boto,你可以使用这个命令:

python3 -m pip install boto

如果要为不同的 python 解释器安装 boto,请使用

/path/to/desired/python -m pip install boto