# Entirely skipping a \`first\_found\` loop based on a conditional

**URL:** <https://forum.ansible.com/t/entirely-skipping-a-first-found-loop-based-on-a-conditional/39087>\
**Category:** Get Help\
**Created:** [December 12, 2024, 7:27am UTC](https://forum.ansible.com/t/entirely-skipping-a-first-found-loop-based-on-a-conditional/39087 "2024-12-12T07:27:41Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![grifferz](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/grifferz/32/377_2.png) [@grifferz](https://forum.ansible.com/u/grifferz)\
**Post date:** [December 12, 2024, 7:27am UTC](https://forum.ansible.com/t/entirely-skipping-a-first-found-loop-based-on-a-conditional/39087/1 "2024-12-12T07:27:41Z")

</div>

Let’s say I have the task:

```yaml
- name: Template the foo file with the first template found
  loop: "{{ query('first_found', templates) }}"
  vars:
    templates:
      - "templates/fqdn/{{ inventory_hostname }}/foo.j2"
      - "templates/debian/{{ ansible_distribution_release }}/foo.j2"
  ansible.builtin.template:
    src: "{{ item }}"
    dest: /etc/foo

```

All fine, but now let’s say I only want to run that task at all on Debian 12 or newer.

This doesn’t work:

```yaml
- name: Template the foo file with the first template found
  when: ansible_distribution_version is version('12', '>=')
  loop: "{{ query('first_found', templates) }}"
  vars:
    templates:
      - "templates/fqdn/{{ inventory_hostname }}/foo.j2"
      - "templates/debian/{{ ansible_distribution_release }}/foo.j2"
  ansible.builtin.template:
    src: "{{ item }}"
    dest: /etc/foo

```

because the `when:` applies to every iteration of the `loop:`. So if for example on hosts that are not Debian 12+ the `loop:` never gets started because none of those files exist on those hosts (the entire point of trying to skip this task here), and that’s a playbook error.

I also cannot do the trick of supplying a default value to `query('first_found', templates)` because that also will be a non-existent file causing the play to error out.

So what is the correct way to entirely skip this task in the case that `ansible_distribution_version is version('12', '>=')` does not return true?

Thanks!

---

<div class="post-metadata">

**Author:** ![bvitnik](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/bvitnik/32/4132_2.png) [@bvitnik](https://forum.ansible.com/u/bvitnik)\
**Post date:** [December 12, 2024, 1:54pm UTC](https://forum.ansible.com/t/entirely-skipping-a-first-found-loop-based-on-a-conditional/39087/2 "2024-12-12T13:54:02Z")

</div>

You can make a list in `loop:` to be empty for Debian \<= 12, e.g:

```auto
loop: "{{ query('first_found', templates) if ansible_distribution_version is version('12', '>=') else [] }}"

```

The other way is to put your task inside a separate `.yml` file and include it with `include_tasks` and `when: ansible_distribution_version is version('12', '>=')` condition on `include_tasks`.

---

<div class="post-metadata">

**Author:** ![bcoca](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/bcoca/32/9269_2.png) [@bcoca](https://forum.ansible.com/u/bcoca)\
**Post date:** [December 12, 2024, 4:52pm UTC](https://forum.ansible.com/t/entirely-skipping-a-first-found-loop-based-on-a-conditional/39087/3 "2024-12-12T16:52:28Z")

</div>

Just have first\_found return empty instead of error:  
`query('first_found', templates, skip=True)`

---

<div class="post-metadata">

**Author:** ![bvitnik](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/bvitnik/32/4132_2.png) [@bvitnik](https://forum.ansible.com/u/bvitnik)\
**Post date:** [December 12, 2024, 6:42pm UTC](https://forum.ansible.com/t/entirely-skipping-a-first-found-loop-based-on-a-conditional/39087/4 "2024-12-12T18:42:50Z")

</div>

@bcoca nice one 🙂… and definitely the most elegant.

I have to make a notice though. It allows for an unexpected failure to go unnoticed. Since template file paths are dependent on `inventory_hostname` and `ansible_distribution_release`, in case there is something missing for some particular combination of these variables, the task will not fail which is maybe undesirable. Being explicit about the condition `ansible_distribution_version is version('12', '>=')` is more robust.

---

<div class="post-metadata">

**Author:** ![mirabilos](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/mirabilos/32/9721_2.png) [@mirabilos](https://forum.ansible.com/u/mirabilos)\
**Post date:** [January 15, 2026, 2:35am UTC](https://forum.ansible.com/t/entirely-skipping-a-first-found-loop-based-on-a-conditional/39087/5 "2026-01-15T02:35:45Z")

</div>

> [@bcoca](#):
>
> Just have first\_found return empty instead of error

That would hide _other_ errors, like file not found when in the correct distro.
