Need to end role when it is run against Ubuntu 18 and below

I would like the playbook to stop running against any machine that has Ubuntu 18 and below.

Here is my attempt. Did I go about it the correct way?

 - name: "{{ ansible_os_family }}.yml - checking for unsupported distro versions"
   debug:
     msg: "{{ ansible_distribution }} {{ ansible_distribution_version }} is NOT supported by playbook."
   failed_when: ( ansible_distribution == "Ubuntu" and ansible_distribution_major_version | version("18", "<=") )

I am running ansible [core 2.16.14].

Help.

Thank you in advance.

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.

Instead of using ansible.builtin.debug with failed_when, I’d rather use ansible.builtin.fail with a regular when (ansible.builtin.fail module – Fail with custom message — Ansible Community Documentation).

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).

1 Like

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”.

1 Like

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.

---
 - name: setup test machines
   hosts: test_machines
   roles:
     - role1
     - role2
     - role3
     - role4

However, i cannot use the action end_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.