Ansible 添加带有 add_host 的 EC2 但连接给出丢失的 Python 错误

Ansible add EC2 with add_host but connection gives missing Python error

我已经使用 Ansible 使用 Ansible ec2 documentation 中的示例创建了 1 个 AWS EC2 实例。我可以成功创建带有标签的实例。然后我使用 add_host.

临时将它添加到我的本地库存组

执行此操作后,我在尝试配置新创建的实例时遇到了问题。在我的 Ansible 游戏中,我想通过它的标签名称来指定实例。例如。 hosts: <tag_name_here>,但出现错误。

这是我到目前为止所做的:

我的目录布局是

inventory/
   staging/
      hosts
      group_vars/
         all/
            all.yml

site.yml

我的 inventory/staging/hosts 文件是

[local]
localhost ansible_connection=local  ansible_python_interpreter=/home/localuser/ansible_ec2/.venv/bin/python

我的 inventory/staging/group_vars/all/all.yml 文件是

---
ami_image: xxxxx
subnet_id: xxxx
region: xxxxx
launched_tag: tag_Name_NginxDemo

这是我的 Ansible 剧本 site.yml

- name: Launch instance
  hosts: localhost
  gather_facts: no
  tasks:
    - ec2:
         key_name: key-nginx
         group: web_sg
         instance_type: t2.micro
         image: "{{ ami_image }}"
         wait: true
         region: "{{ region }}"
         vpc_subnet_id: "{{ subnet_id }}"
         assign_public_ip: yes
         instance_tags:
           Name: NginxDemo
           exact_count: 1
         count_tag:
           Name: NginxDemo
           exact_count: 1
      register: ec2

    - name: Add EC2 instance to inventory group
      add_host:
        hostname: "{{ item.public_ip }}"
        groupname: tag_Name_NginxDemo
        ansible_user: centos_user
        ansible_become: yes
      with_items: "{{ ec2.instances }}"

- name: Configure EC2 instance in launched group
  hosts: tag_Name_NginxDemo
  become: True
  gather_facts: no
  tasks:
    - ping:

我运行这本剧本

$ cd /home/localuser/ansible_ec2
$ source .venv/bin/activate
$ ansible-playbook -i inventory/staging site.yml -vvv`

这将创建 EC2 实例 - 第一个播放正常。然而,第二场比赛给出了以下错误

TASK [.....] ******************************************************************
The authenticity of host 'xx.xxx.xxx.xx (xx.xxx.xxx.xx)' can't be established.
ECDSA key fingerprint is XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX.
Are you sure you want to continue connecting (yes/no)? yes
fatal: [xx.xxx.xxx.xx]: FAILED! => {"changed": false, "module_stderr": 
"Shared connection to xx.xxx.xxx.xx closed.\r\n", "module_stdout": "/bin/sh: 
1: /usr/bin/python: not found\r\n", "msg": "MODULE FAILURE", "rc": 127}

我遵循了

的指示

如何使用标签名称定位 EC2 主机?

编辑:

附加信息

这是我唯一的剧本运行这一点。我看到此消息需要 Python,但我无法在实例上安装 Python,因为我无法在我的游戏中连接到它 Configure EC2 instance in launched group...如果我可以建立连接,那么我可以安装Python(如果这是问题所在)。不过,我不确定如何连接到实例。

编辑 2:

这是我在本地主机上的 Python 信息 运行ning Ansible

我 运行 在 Python venv 中使用 Ansible。

这是我的 python 里面的 venv

$ python --version
Python 2.7.15rc1

$ which python
~/ansible_ec2/.venv/bin/python

这是我在 Python venv

中安装的关于 Ansible 的详细信息
ansible 2.6.2
  config file = /home/localuser/ansible_ec2/ansible.cfg
  configured module search path = [u'/home/localuser/.ansible/plugins/modules', u'/usr/share/ansible/plugins/modules']
  ansible python module location = /home/localuser/ansible_ec2/.venv/local/lib/python2.7/site-packages/ansible
  executable location = /home/localuser/ansible_ec2/.venv/bin/ansible
  python version = 2.7.15rc1 (default, xxxx, xxxx) [GCC 7.3.0]

好的,经过大量搜索,我找到了 1 种可能的解决方法 here。基本上,此变通方法使用 lineinfile 模块并将新的 EC2 实例详细信息永久添加到 hosts 文件中……而不仅仅是 add_host 任务之后的内存播放。我非常密切地遵循了这个建议,这种方法对我很有效。我不需要使用 add_host 模块。

编辑:

我在 lineinfile 模块中添加的行是

- name: Add EC2 instance to inventory group
  - lineinfile: line="{{ item.public_ip }} ansible_python_interpreter=/usr/bin/python3" insertafter=EOF dest=./inventory/staging/hosts
  with_items: "{{ ec2.instances }}"