# How-to 'vars'?

**URL:** <https://forum.ansible.com/t/how-to-vars/16272>\
**Category:** Ansible Project\
**Tags:** ubuntu\
**Created:** [September 18, 2014, 1:42pm UTC](https://forum.ansible.com/t/how-to-vars/16272 "2014-09-18T13:42:59Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Nico\_K](https://avatars.discourse-cdn.com/v4/letter/n/46a35a/32.png) [@Nico\_K](https://forum.ansible.com/u/Nico_K)\
**Post date:** [September 18, 2014, 1:42pm UTC](https://forum.ansible.com/t/how-to-vars/16272/1 "2014-09-18T13:42:59Z")

</div>

Hi,

Given a wide-range of distribution versions how does one abstract away the differences?  
For example I have to deal with all Ubuntu versions between 10.04 =\> 14.04, there are differences such as ‘/etc/dhcp’ vs ‘/etc/dhcp3’ and so on.  
The ‘group\_by’ statement here doesn’t really help since that would result in having to create ~8 configurations.

What I would like to do is in an existing group\_vars file say:

dhcp\_dir: /etc/dhcp3  
when: ansible\_distribution\_version|compare … otherwise …

Or otherwise do something like this in a task? Perhaps using ‘set\_fact’?

Any ideas?

Nico

---

<div class="post-metadata">

**Author:** ![James\_Cammarata](https://avatars.discourse-cdn.com/v4/letter/j/7c8e57/32.png) [@James\_Cammarata](https://forum.ansible.com/u/James_Cammarata)\
**Post date:** [September 18, 2014, 1:54pm UTC](https://forum.ansible.com/t/how-to-vars/16272/2 "2014-09-18T13:54:21Z")

</div>

Hi Nico,

One common way to do this is to use the group\_by module to make groups based on the distribution variable, and then to use individual plays to target those groups.

When doing this in a role, you can use include: or include\_vars: to bring in task or vars files based on the distribution variable instead.

We have a few examples of this in our integration tests, and in the Tower installation playbook.

Thanks!

---

<div class="post-metadata">

**Author:** ![Nico\_K](https://avatars.discourse-cdn.com/v4/letter/n/46a35a/32.png) [@Nico\_K](https://forum.ansible.com/u/Nico_K)\
**Post date:** [September 18, 2014, 3:03pm UTC](https://forum.ansible.com/t/how-to-vars/16272/3 "2014-09-18T15:03:44Z")

</div>

Thanks, I was trying to do the following (which pretty much comes straight from the manual) but I can’t get it to work:

(This is within a role)

```auto
# Load a variable file based on the OS type, or a default if not found.
- include_vars: "{{ item }}"
  with_first_found:
   - "{{ ansible_distribution }}.yml"
   - "{{ ansible_os_family }}.yml"
   - "default.yml"

result: failed: [hostname] => {"failed": true, "item": null}

I don't see how 'item' is ever going to be 'null' at this point...

```

---

<div class="post-metadata">

**Author:** ![James\_Cammarata](https://avatars.discourse-cdn.com/v4/letter/j/7c8e57/32.png) [@James\_Cammarata](https://forum.ansible.com/u/James_Cammarata)\
**Post date:** [September 18, 2014, 3:39pm UTC](https://forum.ansible.com/t/how-to-vars/16272/4 "2014-09-18T15:39:21Z")

</div>

I believe with\_first\_found only searches for files at the moment, it does not look in templates or vars files for this, so you’d have to give it a relative path from the main.yml you’re using it in. For example, assuming this is from tasks/main.yml, you would use:

with\_first\_found:

- “…/vars/{{ ansible\_distribution }}.yml”
- “…/vars/{{ ansible\_os\_family }}.yml”
- “…/vars/default.yml”

You could skip the last line too, and just have the defaults stored in defaults/main.yml too, and have a “when: item is not None” as a conditional on the include\_vars.

---

<div class="post-metadata">

**Author:** ![Nico\_K](https://avatars.discourse-cdn.com/v4/letter/n/46a35a/32.png) [@Nico\_K](https://forum.ansible.com/u/Nico_K)\
**Post date:** [September 19, 2014, 7:07am UTC](https://forum.ansible.com/t/how-to-vars/16272/5 "2014-09-19T07:07:17Z")

</div>

I’m afraid that doesn’t work either, same error message.  
I even tried ‘with\_first\_found:’ with a bunch of static values (to verify that it’s not variable dereferencing which doesn’t work in the loop)  
However no matter what I feed it, it always returns '“item”: null".
