Ansible
ec2
documentation
. 我可以用标记成功地创建实例。然后我使用
add_host
.
这样做之后,我在尝试配置新创建的实例时遇到问题。在Ansible play中,我想通过实例的标记名来指定实例。如。
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
这是我的答案
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:
这是我运行Ansible的本地主机上的Python信息
我在一个Python的venv里运行Ansible。
$ 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]