使用ec2_snapshot获取错误消息“此模块需要boto”

时间:2018-02-06 16:38:58

标签: python-2.7 amazon-ec2 ansible boto

我在AWS上使用带有ansible 2.4的Ubuntu Server 16.4

我的剧本正试图拍摄ec2 vol的快照。见下文

- hosts: localhost
  connection: local
  become: yes
  become_method: sudo
  gather_facts: yes
  any_errors_fatal: True

- name: Take snapshots of all volume"
  ec2_snapshot:
      volume_id: "{{item.id}}"
      description: "Taken on {{ ansible_date_time.date }}"
      snapshot_tags:
          frequency: hourly
  with_items: "{{ aws_ec2_vol_setting }}"

我使用以下cmd运行playbook

ansible-playbook -vvv  pb_aws_backup_nw_us_sat.yml

Using module file /usr/lib/python2.7/dist-packages/ansible/modules/cloud/amazon/ec2_snapshot.py
<127.0.0.1> ESTABLISH LOCAL CONNECTION FOR USER: r_ansible
<127.0.0.1> EXEC /bin/sh -c 'echo ~ && sleep 0'
<127.0.0.1> EXEC /bin/sh -c '( umask 77 && mkdir -p "` echo /home/r_ansible/.ansible/tmp/ansible-tmp-1517934063.54-197986659054036 `" && echo ansible-tmp-1517934063.54-197986659054036="` echo /home/r_ansible/.ansible/tmp/ansible-tmp-1517934063.54-197986659054036 `" ) && sleep 0'
<127.0.0.1> PUT /tmp/tmprERpt_ TO /home/r_ansible/.ansible/tmp/ansible-tmp-1517934063.54-197986659054036/ec2_snapshot.py
<127.0.0.1> EXEC /bin/sh -c 'chmod u+x /home/r_ansible/.ansible/tmp/ansible-tmp-1517934063.54-197986659054036/ /home/r_ansible/.ansible/tmp/ansible-tmp-1517934063.54-197986659054036/ec2_snapshot.py && sleep 0'
<127.0.0.1> EXEC /bin/sh -c 'sudo -H -S -n -u root /bin/sh -c '"'"'echo BECOME-SUCCESS-ociuiywkpfvurbbesjwxhczxoglttlsa; /usr/bin/python /home/r_ansible/.ansible/tmp/ansible-tmp-1517934063.54-197986659054036/ec2_snapshot.py; rm -rf "/home/r_ansible/.ansible/tmp/ansible-tmp-1517934063.54-197986659054036/" > /dev/null 2>&1'"'"' && sleep 0'
Using module file /usr/lib/python2.7/dist-packages/ansible/modules/cloud/amazon/ec2_snapshot.py
<127.0.0.1> EXEC /bin/sh -c 'echo ~ && sleep 0'
The full traceback is:
  File "/tmp/ansible_gUIlz4/ansible_module_ec2_snapshot.py", line 127, in <module>
    import boto.ec2

failed: [localhost] (item={u'vol': u'vol-us-sat-01', u'id': u'vol-0b6aaa3b8289580f6', u'server': u'us-nv-sat-01'}) => {
    "changed": false,
    "invocation": {
        "module_args": {
            "aws_access_key": null,
            "aws_secret_key": null,
            "description": null,
            "device_name": null,
            "ec2_url": null,
            "instance_id": null,
            "last_snapshot_min_age": 0,
            "profile": null,
            "region": null,
            "security_token": null,
            "snapshot_id": null,
            "snapshot_tags": {
                "frequency": "hourly"
            },
            "state": "present",
            "validate_certs": true,
            "volume_id": "vol-0b6aaXXXXXXXX",
            "wait": true,
            "wait_timeout": 0
        }
    },
    "item": {
        "id": "vol-0b6aXXXXXXXXXX",
        "server": "us-nv-sat-01",
        "vol": "vol-us-sat-01"
    },
    "msg": "boto required for this module"
}

注意:访问密钥和密钥为空,因为我使用的是分配给服务器的IAM角色。

我已经检查了我的主人和我,至少看起来我有所有的要求。

$ which python
/usr/bin/python
$ pip list boto | grep boto
boto (2.48.0)
boto3 (1.5.23)
botocore (1.8.37)
$ python -V
Python 2.7.12
似乎所有的python模块都存在并且导入很好

$ python
Python 2.7.12 (default, Dec  4 2017, 14:50:18)
[GCC 5.4.0 20160609] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import boto
>>> import boto.ec2
>>> import boto3

所以我不确定为什么我收到错误“boto这个模块需要”。 我也尝试过没有任何成功的解决方案建议here但我仍然遇到了问题。

3 个答案:

答案 0 :(得分:1)

可能的情况是,您看到的pip包无法以安装的默认python正常运行(如果您可以运行sudo pip freeze | grep boto,您可以看到完整的包列表),如果boto不是你可能需要使用sudo pip install安装它。另一方面,我宁愿做的事情是设置一个python虚拟环境并安装Ansible仅需要在那个隔离环境中的所有包,例如:

依赖关系: sudo apt-get install python-setuptools sudo apt-get install python-pip sudo pip install virtualenv

然后创建虚拟环境: virtualenv ansible_vEnv

激活虚拟环境: source ansible_vEnv/bin/activate

然后使用pip为ec2.py安装所有python依赖项:

  • boto&gt; 2.45
  • boto3&gt; 1.5(不确定是否需要这个)
  • botocore

希望它有所帮助!

答案 1 :(得分:0)

我建议您使用apt-get卸载ansible并使用pip安装它。

sudo apt uninstall ansible
sudo apt install gcc python-dev python-pip
sudo pip install --upgrade PyCrypto ansible awscli boto boto3 ansible-role-manager ansible-playbook-debugger retry

答案 2 :(得分:0)

@Konstantin Suvorov的建议是正确的。 Boto不是为root / sudo安装的

我不知道我是怎么做到的,但 boto 是在本地用户下安装的: p, h1 { margin: 0; }

当我以root用户身份登录并尝试运行 pip 来安装 boto 时,出现以下错误:

~/.local/lib/python2.7/site-packages/

要找到正在使用的pip root版本,我执行了以下操作: root$ pip Traceback (most recent call last): File "/usr/local/bin/pip", line 5, in <module> from pkg_resources import load_entry_point File "/usr/lib/python2.7/dist-packages/pkg_resources/__init__.py", line 2927, in <module> @_call_aside File "/usr/lib/python2.7/dist-packages/pkg_resources/__init__.py", line 2913, in _call_aside f(*args, **kwargs) File "/usr/lib/python2.7/dist-packages/pkg_resources/__init__.py", line 2940, in _initialize_master_working_set working_set = WorkingSet._build_master() File "/usr/lib/python2.7/dist-packages/pkg_resources/__init__.py", line 637, in _build_master return cls._build_from_requirements(__requires__) File "/usr/lib/python2.7/dist-packages/pkg_resources/__init__.py", line 650, in _build_from_requirements dists = ws.resolve(reqs, Environment()) File "/usr/lib/python2.7/dist-packages/pkg_resources/__init__.py", line 829, in resolve raise DistributionNotFound(req, requirers) pkg_resources.DistributionNotFound: The 'pip==9.0.1' distribution was not found and is required by the application

并发现它是pip版本 8.1.1 。然后我更新了以下文件:$python -c "import pip; print(pip.__version__)

$vi /usr/local/bin/pip

并将 9.0.1 的所有引用更改为 8.1.1 。 这允许以root用户身份登录时使用pip。然后我使用 #!/usr/bin/python # EASY-INSTALL-ENTRY-SCRIPT: 'pip==9.0.1','console_scripts','pip' __requires__ = 'pip==9.0.1' import sys from pkg_resources import load_entry_point if __name__ == '__main__': sys.exit( load_entry_point('pip==9.0.1', 'console_scripts', 'pip')() ) 更新了点子,并使用$"sudo -H pip install --upgrade pip检查显示了root和ansible用户的正确版本

这意味着盒子上安装的所有pip现在都是相同的版本。我相信这不是一个好的解决方案,并且对所有用户(ansible和root)更好地指向相同的pip安装,但我不知道如何做到这一点。

使用pip工作我能够安装boto和boto3 $python -c "import pip; print(pip.__version__)pip install boto