To start with I am using:
ansible --version
ansible [core 2.16.3]
config file = None
configured module search path = ['/home/ansible/.ansible/plugins/modules', '/usr/share/ansible/plugins/modules']
ansible python module location = /home/ansible/.local/lib/python3.11/site-packages/ansible
ansible collection location = /home/ansible/.ansible/collections:/usr/share/ansible/collections
executable location = /home/ansible/.local/bin/ansible
python version = 3.11.6 (main, Feb 7 2024, 22:18:53) [GCC 7.3.1 20180712 (Red Hat 7.3.1-17)] (/usr/local/bin/python3.11)
jinja version = 3.1.3
libyaml = True
And I have an Ansible role that I use in 2 different plays in one playbook transfer-files.yml as such:
- name: CMS - Transfer CSV files 1
become: true
hosts: host1
roles:
- { role: transfer_files, TRANSFER_FILES_MODE: "cms" }
- name: COMMERCE - Transfer CSV files 2
become: true
hosts: host2
roles:
- { role: transfer_files, TRANSFER_FILES_MODE: "commerce" }
In my role I use the TRANSFER_FILES_MODE to decide which tasks get executed cms.yml or commerce.yml. In cms.yml I have the following task:
...
- name: If directory is empty end the play silently
ansible.builtin.meta: end_play
when: files_found.matched < 1
...
So with all this said when my play gets to this task in cms.yml and the directory is empty it nicely ends the first play but it still executes the second play.
Is there something I can do to end all executions of all plays listed in a playbook in one task?
Generally, no, because Ansible runs the plays sequentially and the next play knows nothing about the previous one.
But you can set some fact before that meta task, delegate it to
localhost, and useinclude_rolewith a correspondingwhencondition that checkshostvars.localhost.<your_fact>on all the subsequent plays.