【问题标题】:Ansible add EC2 with add_host but connection gives missing Python errorAnsible 添加 EC2 与 add_host 但连接给出了缺少 Python 错误
【发布时间】:2018-08-13 03:51:02
【问题描述】:

我已经使用 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}

我遵循了

的指示
  • this SO question 使用add_hosts 创建任务
  • here 设置gather_facts: False,但这仍然不允许播放正确运行。

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

编辑:

附加信息

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

编辑 2:

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

我在 Python venv 中运行 Ansible。

这是我在venv里面的python

$ 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]

【问题讨论】:

  • /usr/bin/python: not found 表示/usr/bin/python 未找到。你需要解决这个问题。如何?没有人知道,因为你没有提供任何细节。只能猜测。
  • 谢谢。到目前为止,我刚刚运行了我发布的剧本。尽管我没有在上面安装任何东西,但我已经创建了 EC2 实例。你的意思是我需要在实例上安装 Python 吗?
  • 如果未安装,则可能是。您在问题中发布的信息xxxxx 对 SO 读者的信息量不是很大。

标签: amazon-web-services amazon-ec2 ansible ansible-inventory


【解决方案1】:

好的,经过大量搜索,我找到了 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 }}"

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-08-18
    • 2018-08-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多