# different groups with different variables using the same role in the same host

**URL:** <https://forum.ansible.com/t/different-groups-with-different-variables-using-the-same-role-in-the-same-host/14444>\
**Category:** Ansible Project\
**Created:** [January 28, 2014, 3:38pm UTC](https://forum.ansible.com/t/different-groups-with-different-variables-using-the-same-role-in-the-same-host/14444 "2014-01-28T15:38:11Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![Guillem\_Liarte](https://avatars.discourse-cdn.com/v4/letter/g/e95f7d/32.png) [@Guillem\_Liarte](https://forum.ansible.com/u/Guillem_Liarte)\
**Post date:** [January 28, 2014, 3:38pm UTC](https://forum.ansible.com/t/different-groups-with-different-variables-using-the-same-role-in-the-same-host/14444/1 "2014-01-28T15:38:11Z")

</div>

Hello,

I think I have got to a point where I really get confused about teh way variables are used. I need some clarity to understand how you would do this with ansible:

Lets say I have one single hosts where I want to create a collection of directories in /data/db/{project1,project2,n}

I am using the structure described in the Ansible best practices:

[http://docs.ansible.com/playbooks\_best\_practices.html](http://docs.ansible.com/playbooks_best_practices.html)

in /etc/ansible/hosts I have:

[test1]  
host1

[test2]  
host1

Yes, the same host in two different groups. I believe this is legal.

cat test.yml

---

<div class="post-metadata">

**Author:** ![kahlil.hodgson](https://avatars.discourse-cdn.com/v4/letter/k/b9bd4f/32.png) [@kahlil.hodgson](https://forum.ansible.com/u/kahlil.hodgson)\
**Post date:** [January 28, 2014, 10:11pm UTC](https://forum.ansible.com/t/different-groups-with-different-variables-using-the-same-role-in-the-same-host/14444/2 "2014-01-28T22:11:43Z")

</div>

Hi Guillem.

This looks like a modeling problem: the project variables need to be  
detached from the host/group variables.

Try something like:

# group\_vars/all.yml

---

<div class="post-metadata">

**Author:** ![Guillem\_Liarte](https://avatars.discourse-cdn.com/v4/letter/g/e95f7d/32.png) [@Guillem\_Liarte](https://forum.ansible.com/u/Guillem_Liarte)\
**Post date:** [January 31, 2014, 10:43am UTC](https://forum.ansible.com/t/different-groups-with-different-variables-using-the-same-role-in-the-same-host/14444/3 "2014-01-31T10:43:52Z")

</div>

Kahlil,

Thanks for the reply.

I have tried your exact example in a new ansible instance.

I had to adapt it as the playbook would not pass syntax checks:

---

<div class="post-metadata">

**Author:** ![Guillem\_Liarte](https://avatars.discourse-cdn.com/v4/letter/g/e95f7d/32.png) [@Guillem\_Liarte](https://forum.ansible.com/u/Guillem_Liarte)\
**Post date:** [January 31, 2014, 3:07pm UTC](https://forum.ansible.com/t/different-groups-with-different-variables-using-the-same-role-in-the-same-host/14444/4 "2014-01-31T15:07:26Z")

</div>

This works however:

---

<div class="post-metadata">

**Author:** ![Guillem\_Liarte](https://avatars.discourse-cdn.com/v4/letter/g/e95f7d/32.png) [@Guillem\_Liarte](https://forum.ansible.com/u/Guillem_Liarte)\
**Post date:** [January 31, 2014, 3:31pm UTC](https://forum.ansible.com/t/different-groups-with-different-variables-using-the-same-role-in-the-same-host/14444/5 "2014-01-31T15:31:00Z")

</div>

Kahlil,

I found a way to adress those values stored in the array:

- name: Create directories  
file: state=directory path=/data/db/{{ project[item].domain }} owner=root group=root mode=0770  
with\_items: project\_list

This creates effectively:

/data/  
└── db  
├── [test1.com](http://test1.com)  
└── [test2.com](http://test2.com)

Which is what i wanted to do. Excellent.

---

<div class="post-metadata">

**Author:** ![adam2](https://avatars.discourse-cdn.com/v4/letter/a/ecae2f/32.png) [@adam2](https://forum.ansible.com/u/adam2)\
**Post date:** [January 31, 2014, 7:25pm UTC](https://forum.ansible.com/t/different-groups-with-different-variables-using-the-same-role-in-the-same-host/14444/6 "2014-01-31T19:25:25Z")

</div>

I've been using variants of dict.keys(), dict.values(), and dict.items() when utilizing with\_items iteration.

---

<div class="post-metadata">

**Author:** ![Guillem\_Liarte](https://avatars.discourse-cdn.com/v4/letter/g/e95f7d/32.png) [@Guillem\_Liarte](https://forum.ansible.com/u/Guillem_Liarte)\
**Post date:** [February 1, 2014, 6:07pm UTC](https://forum.ansible.com/t/different-groups-with-different-variables-using-the-same-role-in-the-same-host/14444/7 "2014-02-01T18:07:53Z")

</div>

Adam,

Would you be so kind to explain that or link to the right documentation? I have not been able to find a solution that way.

Are you saying there is a better way to do this?

---

<div class="post-metadata">

**Author:** ![kahlil.hodgson](https://avatars.discourse-cdn.com/v4/letter/k/b9bd4f/32.png) [@kahlil.hodgson](https://forum.ansible.com/u/kahlil.hodgson)\
**Post date:** [February 2, 2014, 6:16am UTC](https://forum.ansible.com/t/different-groups-with-different-variables-using-the-same-role-in-the-same-host/14444/8 "2014-02-02T06:16:23Z")

</div>

Sorry Guillem, I typed that up in a hurry. I should have quoted the  
key 'domain' like so:

- debug: msg="{{ project[item]['domain'] }}"

or as use have discovered, the equivalent:

&nbsp;&nbsp;&nbsp;{{ project[item].domain }}

Mixing dictionary lookup forms (that is, using both dots and brackets)  
can be a little confusing to other people reading the code, so I  
prefer to use one or the other but not both at the same time.

K

Kahlil (Kal) Hodgson GPG: C9A02289  
Head of Technology (m) +61 (0) 4 2573 0382  
DealMax Pty Ltd (w) +61 (0) 3 9008 5281

Suite 1415  
401 Docklands Drive  
Docklands VIC 3008 Australia

"All parts should go together without forcing. You must remember that  
the parts you are reassembling were disassembled by you. Therefore,  
if you can't get them together again, there must be a reason. By all  
means, do not use a hammer." -- IBM maintenance manual, 1925

---

<div class="post-metadata">

**Author:** ![Guillem\_Liarte](https://avatars.discourse-cdn.com/v4/letter/g/e95f7d/32.png) [@Guillem\_Liarte](https://forum.ansible.com/u/Guillem_Liarte)\
**Post date:** [February 4, 2014, 8:46am UTC](https://forum.ansible.com/t/different-groups-with-different-variables-using-the-same-role-in-the-same-host/14444/9 "2014-02-04T08:46:40Z")

</div>

Kahlil,

Many thanks for this. I am finding lots of power in Ansible, but also lots of ‘hidden’ gotchas. I have now revamped my configuration to be used with your suggestion and it works really well.

The next pit I found was in the variable precedence.

I honestly understood from the best practice and the docuementation that if you had a role called web, then placing a file called web.yml in there would make the tasks in web to lookup variables stored in web.yml. However if I had another file lets say test.yml before, it would use it instead. So in order to overcome this problem I see two solutions, manually forcing vars\_file from each playbook or store all variables for all playbooks in one file. I’m still deciding what would be the best behavior for the future…

Many thanks for your input so far, you healped me overcome a fundamental issue I had in here.

Guillem
