# Include default variables based on conditions

**URL:** <https://forum.ansible.com/t/include-default-variables-based-on-conditions/8433>\
**Category:** Get Help\
**Tags:** windows, ansible-core, howto\
**Created:** [September 11, 2024, 11:10am UTC](https://forum.ansible.com/t/include-default-variables-based-on-conditions/8433 "2024-09-11T11:10:54Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![guenther](https://avatars.discourse-cdn.com/v4/letter/g/e36b37/32.png) [@guenther](https://forum.ansible.com/u/guenther)\
**Post date:** [September 11, 2024, 11:10am UTC](https://forum.ansible.com/t/include-default-variables-based-on-conditions/8433/1 "2024-09-11T11:10:54Z")

</div>

Hi,

I’m using and help maintaining the role [GitHub - riemers/ansible-gitlab-runner: Ansible role to install gitlab-runner](https://github.com/riemers/ansible-gitlab-runner)

In this role, there in one `defaults/main.yml` which defines a few good general default values. Additionally, there is this:

```yaml
- name: Load platform-specific variables
  ansible.builtin.include_vars: "{{ lookup('first_found', possible_files) }}"
  vars:
    possible_files:
      files:
        - "{{ ansible_distribution }}.yml"
        - "{{ ansible_os_family }}.yml"
        - default.yml
      paths:
        - vars

```

which picks then a file out of those:

- Archlinux.yml
- Darwin.yml
- Debian.yml
- RedHat.yml
- Windows.yml
- default.yml

The problem we have is, that variables of the presented list cannot be overwritten then in the playbook, because they are imported with `include_vars` what has very high precedence.

What we want to archive is, including `defaults/main.yml` for basic vars and including the platform specific vars but also as “default” vars, to allow all variables to be overwritten in the playbook. How can this be archived?

Kind rgards  
guenther

---

<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:** [September 11, 2024, 3:15pm UTC](https://forum.ansible.com/t/include-default-variables-based-on-conditions/8433/2 "2024-09-11T15:15:09Z")

</div>

Use a role’s defaults for low precedence. With `include_role` you can use `defaults_from:` + `first_found` lookup or you can just include the role with the defaults when `include_vars` fails (after removing `default.yml`).

---

<div class="post-metadata">

**Author:** ![RayJin2000](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/rayjin2000/32/3641_2.png) [@RayJin2000](https://forum.ansible.com/u/RayJin2000)\
**Post date:** [September 13, 2024, 9:22am UTC](https://forum.ansible.com/t/include-default-variables-based-on-conditions/8433/3 "2024-09-13T09:22:47Z")

</div>

Hi,

we have two different solutions for the Problem. The first one is like yours:

```yaml
- name: Include OS-specific variables
 ansible.builtin.include_vars: "{{ item }}"
 with_first_found:
   - files:
       - "vars/{{ ansible_os_family }}-{{ ansible_distribution_major_version }}.yml"
       - "vars/{{ ansible_os_family }}.yml"
     skip: true

- name: Define mysql_commercial_packages
 ansible.builtin.set_fact:
   mysql_commercial_packages: "{{ __mysql_commercial_packages | list }}"
 when: mysql_commercial_packages is not defined

```

`__mysql_commercial_packages` is defined in `vars/RedHat.yml`

now we can set `mysql_commercial_packages` in our group\_vars and overwrite the default from the OS specific Vars.

Second Solution: `defaults/main.yml`

```auto
mysql_community_packages: "{{ lookup('ansible.builtin.vars', 'mysql_community_packages_' + os_version, default='mysql-server') }}"
mysql_community_packages_redhat9:
  - mysql
  - mysql-server
  - mysql-common

```
