# Variable precedence in Role

**URL:** <https://forum.ansible.com/t/variable-precedence-in-role/13418>\
**Category:** Ansible Project\
**Created:** [September 4, 2013, 4:37pm UTC](https://forum.ansible.com/t/variable-precedence-in-role/13418 "2013-09-04T16:37:19Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Adrien\_Montfort](https://avatars.discourse-cdn.com/v4/letter/a/65b543/32.png) [@Adrien\_Montfort](https://forum.ansible.com/u/Adrien_Montfort)\
**Post date:** [September 4, 2013, 4:37pm UTC](https://forum.ansible.com/t/variable-precedence-in-role/13418/1 "2013-09-04T16:37:19Z")

</div>

Hello,

I think I don’t understand perfectly what is meant by :

“In Ansible 1.3 and later, however, vars/ directories are guaranteed to be scoped to the current role, just like roles parameters.”

I have the following test project :

├── all.yml  
├── roles  
│ ├── role1  
│ │ ├── tasks  
│ │ │ └── main.yml  
│ │ └── vars  
│ │ └── main.yml  
│ └── role2  
│ ├── tasks  
│ │ └── main.yml  
│ └── vars  
│ └── main.yml  
└── test

where :  
role1/vars/main.yml sets the variable _var_ to _role1_  
role2/vars/main.yml sets the variable _var_ to _role2_

_and all.yml is_

> * * *
> 
> - hosts: all  
> roles:
> - role1
> - role2

I am using the variable _var_ in role1/tasks/main.yml and in role1/tasks/main.yml and in both cases its value is _role2._ Is it what I should have expected?

Real case scenario :  
I have a few steps that I want to reuse for different roles. They have been extracted to a playbook that I can include and I’m using a var to configure the behavior. In production the roles might be split between different servers but in test they are configured on the same server… But the var has always the same value every time the extracted playbook is played.

Thanks a lot

---

<div class="post-metadata">

**Author:** ![James\_Cammarata1](https://avatars.discourse-cdn.com/v4/letter/j/d26b3c/32.png) [@James\_Cammarata1](https://forum.ansible.com/u/James_Cammarata1)\
**Post date:** [September 4, 2013, 4:54pm UTC](https://forum.ansible.com/t/variable-precedence-in-role/13418/2 "2013-09-04T16:54:41Z")

</div>

In your case, tasks within the role role1 should see {{var}} as role1, and role2 tasks should see {{var}} as role2. Can you put your examples up in a gist or pastebin? I am unable to reproduce this:

[https://gist.github.com/jimi1283/6439723](https://gist.github.com/jimi1283/6439723)

---

<div class="post-metadata">

**Author:** ![Adrien\_Montfort](https://avatars.discourse-cdn.com/v4/letter/a/65b543/32.png) [@Adrien\_Montfort](https://forum.ansible.com/u/Adrien_Montfort)\
**Post date:** [September 4, 2013, 5:27pm UTC](https://forum.ansible.com/t/variable-precedence-in-role/13418/3 "2013-09-04T17:27:44Z")

</div>

Hello,

Thanks for your help! I posted a reply comment to your gist. My example is pretty similar. I guess the difference lies in your test.yml vs my all.yml. Or would it be because I use a group instead of a host directly?

---

<div class="post-metadata">

**Author:** ![James\_Cammarata1](https://avatars.discourse-cdn.com/v4/letter/j/d26b3c/32.png) [@James\_Cammarata1](https://forum.ansible.com/u/James_Cammarata1)\
**Post date:** [September 4, 2013, 5:39pm UTC](https://forum.ansible.com/t/variable-precedence-in-role/13418/4 "2013-09-04T17:39:47Z")

</div>

That looks like it should work exactly like mine, the inventory source does not matter. Could you please verify the version of ansible-playbook you’re using?

---

<div class="post-metadata">

**Author:** ![Adrien\_Montfort](https://avatars.discourse-cdn.com/v4/letter/a/65b543/32.png) [@Adrien\_Montfort](https://forum.ansible.com/u/Adrien_Montfort)\
**Post date:** [September 5, 2013, 7:39am UTC](https://forum.ansible.com/t/variable-precedence-in-role/13418/5 "2013-09-05T07:39:15Z")

</div>

Hello,

I was on 1.3 but my repo had not been updated for 3 weeks.  
I pulled and everything works fine now ! My bad…  
Thanks for you help ! I’m glad that what I was trying is possible.

---

<div class="post-metadata">

**Author:** ![James\_Cammarata1](https://avatars.discourse-cdn.com/v4/letter/j/d26b3c/32.png) [@James\_Cammarata1](https://forum.ansible.com/u/James_Cammarata1)\
**Post date:** [September 5, 2013, 1:33pm UTC](https://forum.ansible.com/t/variable-precedence-in-role/13418/6 "2013-09-05T13:33:43Z")

</div>

No problem, glad it was an easy fix.
