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

**URL:** <https://forum.ansible.com/t/need-to-end-role-when-it-is-run-against-ubuntu-18-and-below/41661>\
**Category:** Get Help\
**Tags:** ubuntu\
**Created:** [April 10, 2025, 5:16pm UTC](https://forum.ansible.com/t/need-to-end-role-when-it-is-run-against-ubuntu-18-and-below/41661 "2025-04-10T17:16:56Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![jyanga](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/jyanga/32/8108_2.png) [@jyanga](https://forum.ansible.com/u/jyanga)\
**Post date:** [April 10, 2025, 5:16pm UTC](https://forum.ansible.com/t/need-to-end-role-when-it-is-run-against-ubuntu-18-and-below/41661/1 "2025-04-10T17:16:56Z")

</div>

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?

```auto
 - 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.

---

<div class="post-metadata">

**Author:** ![Lyle\_McKarns](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/lyle_mckarns/32/8142_2.png) [@Lyle\_McKarns](https://forum.ansible.com/u/Lyle_McKarns)\
**Post date:** [April 10, 2025, 5:24pm UTC](https://forum.ansible.com/t/need-to-end-role-when-it-is-run-against-ubuntu-18-and-below/41661/2 "2025-04-10T17:24:25Z")

</div>

Depending on how you have your workflows laid out, you’re probably better off doing something more like

```auto
- 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.

---

<div class="post-metadata">

**Author:** ![felixfontein](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/felixfontein/32/7530_2.png) [@felixfontein](https://forum.ansible.com/u/felixfontein)\
**Post date:** [April 10, 2025, 5:38pm UTC](https://forum.ansible.com/t/need-to-end-role-when-it-is-run-against-ubuntu-18-and-below/41661/3 "2025-04-10T17:38:41Z")

</div>

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](https://docs.ansible.com/ansible/latest/collections/ansible/builtin/fail_module.html)).

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](https://docs.ansible.com/ansible/latest/collections/ansible/builtin/meta_module.html#parameters)). 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).

---

<div class="post-metadata">

**Author:** ![binbashroot](https://avatars.discourse-cdn.com/v4/letter/b/ad7895/32.png) [@binbashroot](https://forum.ansible.com/u/binbashroot)\
**Post date:** [April 11, 2025, 12:12pm UTC](https://forum.ansible.com/t/need-to-end-role-when-it-is-run-against-ubuntu-18-and-below/41661/4 "2025-04-11T12:12:20Z")

</div>

If you are wanting to stop running tasks against a specific version, I would recommend using the meta module.

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

---

<div class="post-metadata">

**Author:** ![jyanga](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/jyanga/32/8108_2.png) [@jyanga](https://forum.ansible.com/u/jyanga)\
**Post date:** [April 11, 2025, 5:07pm UTC](https://forum.ansible.com/t/need-to-end-role-when-it-is-run-against-ubuntu-18-and-below/41661/5 "2025-04-11T17:07:45Z")

</div>

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.

```auto
---
 - 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.

---

<div class="post-metadata">

**Author:** ![binbashroot](https://avatars.discourse-cdn.com/v4/letter/b/ad7895/32.png) [@binbashroot](https://forum.ansible.com/u/binbashroot)\
**Post date:** [April 11, 2025, 5:43pm UTC](https://forum.ansible.com/t/need-to-end-role-when-it-is-run-against-ubuntu-18-and-below/41661/6 "2025-04-11T17:43:24Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![jyanga](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/jyanga/32/8108_2.png) [@jyanga](https://forum.ansible.com/u/jyanga)\
**Post date:** [April 17, 2025, 6:58pm UTC](https://forum.ansible.com/t/need-to-end-role-when-it-is-run-against-ubuntu-18-and-below/41661/7 "2025-04-17T18:58:35Z")

</div>

Thank you all again. Though I really wanted to use the `meta` with `end_role` solution, I cannot at the moment update my `ansible-core`. I did go with the `include_role` way which satisfied what I need to do for now.
