Reputation: 529
I have an ansible playbook YAML file which contains 3 plays.
The first play and the third play run on localhost but the second play runs on remote machine as you can see an example below:
- 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
I found that, on the first run, Ansible Playbook executes Play1
and Play3
and skips Play2
. Then, I try to run again, it executes all of them correctly.
What is wrong here?
Upvotes: 3
Views: 4504
Reputation: 529
The problem is that, at Play2
, I use ec2 inventor like tag_Name_my_machine
but this instance was not created yet, because it would be created at Play1
's task.
Once Play1
finished, it will run Play2
but no host found so it silently skip this play.
The solution is to create dynamic inventor and manually register at Play1
's tasks:
Playbook may look like this:
- 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
Upvotes: 2