Importing OS-specific variables

Hello,
I want Ansible to examine the vars folder, and even if it’s empty or doesn’t contain a default.yml file, I don’t want it to fail. I’ve tried the following code, but it doesn’t seem to be working. Do you have any ideas on what I might be doing wrong?

tasks/main.yml:

---
- name: Include OS-specific variables
  ansible.builtin.include_vars: "{{ lookup('ansible.builtin.first_found', params) }}"
  vars:
    params:
      files:
        - "{{ ansible_distribution | lower }}.yml"
        - "{{ ansible_os_family | lower }}.yml"
        - default.yml
      paths:
        - "vars"
      skip: true
  when: params | length > 0

I would be grateful for any suggestion. Thank you.

Zoltan

Are you looking for ignore_errors: true? Error handling in playbooks — Ansible Documentation

Thank you. I haven’t tried it, but it doesn’t seem to be the most elegant solution. Normally, the task shouldn’t even run if it doesn’t find a file. That’s why I would have preferred using ‘when’ in this case.

Oh, I took I don't want it to fail as in you wanted it to keep going regardless whether it was there or not.

I don’t think you can do a when using variables defined in the same task.

Perhaps do a check on the files (ansible.builtin.stat) as a separate task before, and use the results of that for your when statement here?

So the logic would be:

  1. task 1 runs ansible.builtin.stat to check for file(s) and register result
  2. task 2 is above with when task 1 sees file(s)

Thank you. Then I think it would be easier to include a ‘default.yml’ as a fallback in the ‘vars’ folder, even if it’s empty, and do this for each role.