Amazon ec2 获得;跳过:没有匹配的主机;与ansible剧本

Amazon ec2 获得;跳过:没有匹配的主机;与ansible剧本,amazon-ec2,ansible-2.x,Amazon Ec2,Ansible 2.x,在为主机[ec2测试]播放时,我下面的ansible(2.1.1.0)剧本抛出了“跳过:没有匹配的主机”错误。ansible hosts文件已添加新创建实例的fqdn。如果我第二次重新运行我的剧本,它运行良好。但第一次运行时不会抛出主机匹配错误:( 我的剧本: --- - name: Provision an EC2 instance hosts: localhost connection: local gather_facts: no become: False

在为主机[ec2测试]播放时,我下面的ansible(2.1.1.0)剧本抛出了“跳过:没有匹配的主机”错误。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
知道我为什么会被跳过吗:如果出现在这里,没有主机匹配错误?任何帮助都将不胜感激


谢谢!

在playbook执行过程中,Ansible似乎只读取清单文件一次,并且在向其中添加条目后不会刷新它。因此,它无法添加新添加的主机

要解决此问题,您可以在更新库存文件后,强制Ansible To执行以下任务:

- name: Refresh inventory to ensure new instaces exist in inventory
  meta: refresh_inventory
或者,您可能根本不更新库存文件,而是使用任务:

- add_host:
    name: "{{ item.dns_name }}"
    groups: ec2-test
  with_items: ec2.instances