使用 ansible playbook 获取“跳过:没有匹配的主机”

问题描述 投票:0回答:1

我下面的 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

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

谢谢!

amazon-ec2 ansible-2.x
1个回答
0
投票

似乎 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
© www.soinside.com 2019 - 2024. All rights reserved.