1

我有几台安装了 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]

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

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

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

- 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}}"

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

ERROR! couldn't resolve module/action 'community.aws.ec2_instance_info'. This often indicates a misspelling, missing collection, or incorrect module path.

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

/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

我什至在运行这个的机器上强制重新安装:

ansible-galaxy collection install community.aws --force-with-deps

我得到了这个输出:

Installing 'community.aws:2.0.0' to '/home/ubuntu/.ansible/collections/ansible_collections/community/aws'
Installing 'amazon.aws:2.0.0' to '/home/ubuntu/.ansible/collections/ansible_collections/amazon/aws'

但这并没有改变什么。

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

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

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

4

0 回答 0