Depending on how you have your workflows laid out, you’re probably better off doing something more like
- name: Include Role only for supported Ubuntu versions
include_role: Ubuntu_Role
when: ( ansible_distribution == "Ubuntu" and ansible_distribution_major_version | version("18", "=>") )
and just not include the role for systems, rather than include it and then fail when its not needed.
If you want to end the role (but continue with the next tasks / roles), you should take a look at ansible.bulitin.meta with end_role (ansible.builtin.meta module – Execute Ansible ‘actions’ — Ansible Community Documentation). That requires ansible-core 2.18+ though. There are also several other end_xxx values that might be of interest (which also work for older ansible-core versions).
If you are wanting to stop running tasks against a specific version, I would recommend using the meta module.
- name: "End host if not the right diistributiion and major version"
ansible.builtin.meta: end_host
when:
- ansible_distribution == "Ubuntu"
- ansible_distribution_major_version <= 18
Using a list for your conditionals is an implied “and”.
Thank you all. It took me some time to test your recommendations. I believe that what I need is the meta with end_role as my playbooks looks something like this. Moreover, I want the rest of the roles to run against the Ubuntu 18 and below machines.
However, i cannot use the actionend_role as my ansible-core needs to be updated. I was hoping to avoid the update as I encountered issue with it before. Any other tricks I can use while I try to test updating?
Thank you all for all the help. It is much appreciated.
You can also put conditionals on roles instead of using the meta: attribute. I personally prefer to use include_role in my tasks section instead of using the roles section in my playbooks.