通过 Ansible 行动手册获得"skipping: no hosts matched"



我下面的ansible(2.1.1.0)剧本抛出"跳过:没有主机匹配"错误时玩主机[ec2-test]。在sible hosts文件中添加了新创建实例的fqdn。它运行良好,如果我再次运行我的剧本。但是第一次运行会抛出no hosts matched error:(

)

我的剧本:

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

我的hosts文件有这些清单

[localhost]
localhost 
....
[ec2-test]
ec2-54-244-180-186.us-west-2.compute.amazonaws.com

知道为什么我得到跳过:没有主机匹配错误,如果出现在这里吗?如有任何帮助,不胜感激

谢谢!

似乎Ansible只读取一次库存文件,并且在剧本执行期间添加条目后不刷新它。因此,无法添加新添加的主机。

要解决这个问题,可以强制Ansible在更新库存文件后执行以下任务来刷新整个库存:

- 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

相关内容

最新更新