Ansible - End a play for all hosts using meta end_play
by
Jeremy Canfield |
Updated: June 15 2023
| Ansible articles
end_play and end_host are similar, but not exactly the same.
- end_play will end a play for all hosts (this article)
- end_host will end a play for certain hosts
end_play can be used to end a play in a playbook for all hosts in the play. Take for example the following playbook which has 2 plays.
---
- name: first play
hosts: all
tasks:
- debug:
msg: "first task in first play"
- debug:
msg: "second task in first play"
- name: second play
hosts: all
tasks:
- debug:
msg: "first task in second play"
- debug:
msg: "second task in second play"
...
Running this playbook will output the following, where all tasks in both plays were completed.
PLAY [first play]
TASK [debug]
ok: [server1.example.com] => {
"msg": "first task in first play"
}
TASK [debug]
ok: [server1.example.com] => {
"msg": "second task in first play"
}
PLAY [second play]
TASK [debug]
ok: [server1.example.com] => {
"msg": "first task in second play"
}
TASK [debug]
ok: [server1.example.com] => {
"msg": "second task in second play"
}
PLAY RECAP
server1.example.com : ok=4 changed=0 unreacable=0 failed=0
If you include - meta: end_play the subsequent tasks in the play will not be invoked.
---
- hosts: all
tasks:
- debug:
msg: "first task in first play"
- meta: end_play
- debug:
msg: "second task in first play"
- hosts: all
tasks:
- debug:
msg: "first task in second play"
- debug:
msg: "second task in second play"
...
Which in this example will result in the following, where the second task in the first play was not invoked.
PLAY [first play]
TASK [debug]
ok: [server1.example.com] => {
"msg": "first task in first play"
}
PLAY [second play]
TASK [debug]
ok: [server1.example.com] => {
"msg": "first task in second play"
}
TASK [debug]
ok: [server1.example.com] => {
"msg": "second task in second play"
}
PLAY RECAP
server1.example.com : ok=3 changed=0 unreacable=0 failed=0
Did you find this article helpful?
If so, consider buying me a coffee over at