# same variable on different roles

**URL:** <https://forum.ansible.com/t/same-variable-on-different-roles/30459>\
**Category:** Ansible Project\
**Created:** [May 31, 2019, 10:41am UTC](https://forum.ansible.com/t/same-variable-on-different-roles/30459 "2019-05-31T10:41:11Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Omer\_Sen](https://avatars.discourse-cdn.com/v4/letter/o/f4b2a3/32.png) [@Omer\_Sen](https://forum.ansible.com/u/Omer_Sen)\
**Post date:** [May 31, 2019, 10:41am UTC](https://forum.ansible.com/t/same-variable-on-different-roles/30459/1 "2019-05-31T10:41:11Z")

</div>

Hi

I want to ask what is expected behaviour on variables. So for example I have two roles and on vars i have same variable name like tmp\_dir but pointing on different directories

role1/vars/  
role2/vars/

and what happens if i create a yml file contains

roles:

- role1
- role2

I was thinking while running role1 tmp\_dir value will be used from role1/vars/main.yml and when running role2 tmp\_dir variable is taken from role2/vars/main.yml. But it seems either something has changed with 2.8 or I was wrong from the beginning.

Also what should i do to realize my goal (using different variable value for same variable name on different role)

Regards.

---

<div class="post-metadata">

**Author:** ![Kai\_Stian\_Olstad](https://avatars.discourse-cdn.com/v4/letter/k/e19adc/32.png) [@Kai\_Stian\_Olstad](https://forum.ansible.com/u/Kai_Stian_Olstad)\
**Post date:** [May 31, 2019, 11:47am UTC](https://forum.ansible.com/t/same-variable-on-different-roles/30459/2 "2019-05-31T11:47:30Z")

</div>

It's woking great for me.

$ ansible --version  
ansible 2.8.0

$ more roles/role{1,2}/{tasks,vars}/main.yml | cat  
::::::::::::::  
roles/role1/tasks/main.yml  
::::::::::::::

---

<div class="post-metadata">

**Author:** ![Omer\_Sen](https://avatars.discourse-cdn.com/v4/letter/o/f4b2a3/32.png) [@Omer\_Sen](https://forum.ansible.com/u/Omer_Sen)\
**Post date:** [May 31, 2019, 12:05pm UTC](https://forum.ansible.com/t/same-variable-on-different-roles/30459/3 "2019-05-31T12:05:33Z")

</div>

Hi,

Actually that is what I expect but don’t know what happened but now variables seems to be all combined so I can’t use same variable name to contain different values on different roles.

Regards.

---

<div class="post-metadata">

**Author:** ![Kai\_Stian\_Olstad](https://avatars.discourse-cdn.com/v4/letter/k/e19adc/32.png) [@Kai\_Stian\_Olstad](https://forum.ansible.com/u/Kai_Stian_Olstad)\
**Post date:** [May 31, 2019, 12:11pm UTC](https://forum.ansible.com/t/same-variable-on-different-roles/30459/4 "2019-05-31T12:11:08Z")

</div>

You are probably also setting the variables other places?  
Since variables in Ansible are global they are overwritten according to this precedence list  
[https://docs.ansible.com/ansible/latest/user\_guide/playbooks\_variables.html#variable-precedence-where-should-i-put-a-variable](https://docs.ansible.com/ansible/latest/user_guide/playbooks_variables.html#variable-precedence-where-should-i-put-a-variable)
