Ansible Playbook跳过多个播放剧本文件的一些播放

时间:2017-03-31 09:25:24

标签: ansible

我有一个ansible playbook YAML文件,其中包含3个剧本。

第一个播放和第三个播放在localhost上运行,但第二个播放在远程计算机上运行,​​如下所示:

- name: Play1
  hosts: localhost
  connection: local
  gather_facts: false
  tasks:
    - ... task here

- name: Play2
  hosts: remote_host
  tasks:
    - ... task here

- name: Play3
  hosts: localhost
  connection: local
  gather_facts: false
  tasks:
    - ... task here

我发现,在第一次运行时,Ansible Playbook会执行Play1Play3并跳过Play2。然后,我尝试再次运行,它正确执行所有这些。

这里有什么问题?

1 个答案:

答案 0 :(得分:2)

问题是,在Play2,我使用像tag_Name_my_machine这样的ec2发明者,但是这个实例还没有创建,因为它将在Play1的任务中创建。

Play1完成后,它会运行Play2,但找不到主机,所以它会默默跳过此播放。

解决方案是创建动态发明人并在Play1的任务中手动注册:

Playbook可能如下所示:

- name: Play1
  hosts: localhost
  connection: local
  gather_facts: false
  tasks:
    - name: Launch new ec2 instance
      register: ec2
      ec2: ...
    - name: create dynamic group
      add_host:
        name: "{{ ec2.instances[0].private_ip }}"
        group: host_dynamic_lastec2_created

- name: Play2
  user: ...
  hosts: host_dynamic_lastec2_created
  become: yes
  become_method: sudo
  become_user: root
  tasks:
    - name: do something
      shell: ...

- name: Play3
  hosts: localhost
  connection: local
  gather_facts: false
  tasks:
    - ... task here