我的下方ansible(2.1.1.0)剧本正在投掷"跳过:没有主机匹配"播放主持人[ec2-test]时出错。 ansible hosts文件添加了新创建的实例的fqdn。如果我第二次运行我的剧本,它运行正常。但第一次运行抛出没有主机匹配错误:(
我的剧本:
---
- name: Provision an EC2 instance
hosts: localhost
connection: local
gather_facts: no
become: False
vars_files:
- awsdetails.yml
tasks:
- name: Launch the new EC2 Instance
ec2:
aws_access_key: "{{ aws_id }}"
aws_secret_key: "{{ aws_key }}"
group_id: "{{ security_group_id }}"
instance_type: "{{ instance_type }}"
image: "{{ image }}"
key_name: "{{ ssh_keyname }}"
wait: yes
region: "{{ region }}"
count: 1
register: ec2
- name: Update the ansible hosts file with new IP address
local_action: lineinfile dest="/etc/ansible/hosts" regexp={{ item.dns_name }} insertafter='\[ec2-test\]'line="{{item.dns_name}} ansible_ssh_private_key_file=/etc/ansible/e2-key-file.pem ansible_user=ec2-user"
with_items: ec2.instances
- name: Wait for SSH to come up
wait_for: host={{ item.public_dns_name }} port=22 delay=60 timeout=320 state=started
with_items: ec2.instances
- name: playing ec2-test instances
hosts: ec2-test
gather_facts: no
我的主机文件有这些库存
[localhost]
localhost
....
[ec2-test]
ec2-54-244-180-186.us-west-2.compute.amazonaws.com
我知道为什么要跳过:如果出现在这里没有主机匹配错误?任何帮助将不胜感激
谢谢!
答案 0 :(得分:0)
似乎Ansible只读取一次库存文件,并且在playbook执行期间向其中添加一个条目后不会刷新它。因此,它无法添加新添加的主机。
要解决此问题,您可以在更新清单文件后执行以下任务,强制Ansible到refresh the entire inventory:
- name: Refresh inventory to ensure new instaces exist in inventory
meta: refresh_inventory
或者您可以不更新库存文件,而是使用add_host
任务:
- add_host:
name: "{{ item.dns_name }}"
groups: ec2-test
with_items: ec2.instances