ansible

How to use 'skip: true' with 'with_first_found'?


I would like to use the following task in a playbook:

- include: "{{ prerequisites_file }}"
  with_first_found:
    - "prerequisites-{{ ansible_distribution }}.yml"
    - "prerequisites-{{ ansible_os_family }}.yml"
  loop_control:
    loop_var: prerequisites_file

I would like it to just pass if no files matching the architecture were found.

When run as is, in such a case, it produces an error:

TASK [ansible-playbook : include] ***************************************
fatal: [ansible-playbook]: FAILED! => {"failed": true, "msg": "No file was found when using with_first_found. Use the 'skip: true' option to allow this task to be skipped if no files are found"}

I know I can add a dummy file at the end, but if I were to follow the advice, how am I supposed to add the skip: true option here?

It's definitely not an argument of the include module, it should be somehow bound to with_first_found clause...


Solution

  • with_first_found has a lot of parameters variations.
    Take a look at first_found.py – there are some examples in the beginning of the file.

    Answering your question:

    - include: "{{ prerequisites_file }}"
      with_first_found:
        - files:
            - "prerequisites-{{ ansible_facts.ansible_distribution }}.yml"
            - "prerequisites-{{ ansible_facts.ansible_os_family }}.yml"
          skip: true
      loop_control:
        loop_var: prerequisites_file