Variable precedence in Role

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

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

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?

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?

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.

No problem, glad it was an easy fix.