错误!无法解析模块/操作“community.aws.ec2_instance_info”。这通常表示拼写错误、集合丢失或模块路径不正确

Cip*_*ica 5 python ubuntu amazon-web-services ansible ansible-galaxy

我有几台安装了 ansible 的 Ubuntu 18.04 机器。当在所有这些上运行时ansible --version,我得到以下输出:

ansible 2.9.27
  config file = /etc/ansible/ansible.cfg
  configured module search path = [u'/home/ubuntu/.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, Feb 27 2021, 15:10:58) [GCC 7.5.0]
Run Code Online (Sandbox Code Playgroud)

顺便说一下,定义的路径configured module search path不存在。

我在所有机器上安装了community.aws集合: ansible-galaxy collection install community.aws

我运行了一个简单的剧本来从某些 AWS EC2 实例收集数据:

ansible 2.9.27
  config file = /etc/ansible/ansible.cfg
  configured module search path = [u'/home/ubuntu/.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, Feb 27 2021, 15:10:58) [GCC 7.5.0]
Run Code Online (Sandbox Code Playgroud)

除了一台机器之外,所有机器上一切正常,我收到以下错误:

ERROR! couldn't resolve module/action 'community.aws.ec2_instance_info'. This often indicates a misspelling, missing collection, or incorrect module path.
Run Code Online (Sandbox Code Playgroud)

我运行了 asudo find / -name "ec2_instance_info.py"查看该文件可能安装在哪里。在所有机器上我得到了完全相同的输出:

- name: test playbook
  hosts: localhost
  tasks:

  - name: "Set vars"
    set_fact:
      ec2_instance_name: "SomeName"

  - name: "Gather EC2 instance info: {{ ec2_instance_name }}."
    community.aws.ec2_instance_info:
      aws_access_key: "{{ lookup('env', 'AWS_ACCESS_KEY') }}"
      aws_secret_key: "{{ lookup('env', 'AWS_SECRET_KEY') }}"
      region: "{{ lookup('env', 'AWS_REGION') }}"
      filters:
        "tag:Name": "{{ ec2_instance_name }}"
    register: ec2_instance_info_ret

  - name: "Show data"
    debug:
      msg: "{{ec2_instance_info_ret}}"
Run Code Online (Sandbox Code Playgroud)

我什至在运行此命令的机器上强制重新安装:

ERROR! couldn't resolve module/action 'community.aws.ec2_instance_info'. This often indicates a misspelling, missing collection, or incorrect module path.
Run Code Online (Sandbox Code Playgroud)

我得到这个输出:

/usr/lib/python2.7/dist-packages/ansible/modules/cloud/amazon/ec2_instance_info.py
/root/.ansible/collections/ansible_collections/amazon/aws/plugins/modules/ec2_instance_info.py
/home/ubuntu/.ansible/collections/ansible_collections/community/aws/plugins/modules/ec2_instance_info.py
Run Code Online (Sandbox Code Playgroud)

但这并没有改变任何事情。

我还在一台之前一切正常的机器上运行了强制重新安装。我也成功地打破了它。现在我有两台机器正在重现问题。

一种解决方案是直接在我的剧本中调用ec2_instance_info而不是community.aws.ec2_instance_info. 这有效,但我不知道为什么。不管怎样,我不喜欢这个解决方案,因为一些 ansible aws 插件可以在没有community.aws 前缀的情况下工作,而其他插件则不能。除此之外,当我调用这样一个没有前缀的插件时,VS Code 会给我很多警告。

您知道如何解决这个问题吗?我想调用community.aws.ec2_instance_info我的剧本,而不会在任何机器上出现任何问题。

小智 -1

在运行此命令的机器上安装:

ansible-galaxy collection install community.aws
ansible-galaxy collection install amazon.aws:==3.3.1 --force
Run Code Online (Sandbox Code Playgroud)