Ansible - Stop halt or exit a playbook using meta end_play

The end_play parameter can be used to end a playbook. Take for example the following plays.

- debug: 
    msg: "Hello"

- meta: end_play

- debug: 
    msg: "World"

 

The following will be returned. Notice that "Hello" was printed but "World" was not printed, because the play was ended.

PLAY [all]

TASK [debug]
ok: [server1.example.com] => {
    "msg": "Hello"
}

PLAY RECAP
server1.example.com : ok=1  changed=0  unreacable=0  failed=0

 

Typically, end_play is used when some condition evaluates to true or false. For example, end_play could be used along with x.stat.exists to end a play if a certain directory exists.

- name: determine if directory exists
  stat:
    path: /path/to/directory
  register: out

- name: directory exists ending play
  meta: end_play
  when: out.stat.exists == true

 


Multiple hosts problem

However, this is not a great approach if a play is being run against two or more hosts, as the play will end while executing against the first host, and never move onto the next host. A better solution would be to apply a when statement to each task in the play to skip the task when a condition evaluates to true or false.



Add a Comment




We will never share your name or email with anyone. Enter your email if you would like to be notified when we respond to your comment.





Please enter 8ecfe in the box below so that we can be sure you are a human.




Comments

Web design by yours truely - me, myself, and I   |   jeremy.canfield@freekb.net   |