# Weird: Passing variables to roles

**URL:** <https://forum.ansible.com/t/weird-passing-variables-to-roles/21787>\
**Category:** Ansible Project\
**Tags:** windows\
**Created:** [June 15, 2016, 6:29pm UTC](https://forum.ansible.com/t/weird-passing-variables-to-roles/21787 "2016-06-15T18:29:16Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![Trond\_Hindenes](https://avatars.discourse-cdn.com/v4/letter/t/96bed5/32.png) [@Trond\_Hindenes](https://forum.ansible.com/u/Trond_Hindenes)\
**Post date:** [June 15, 2016, 6:29pm UTC](https://forum.ansible.com/t/weird-passing-variables-to-roles/21787/1 "2016-06-15T18:29:16Z")

</div>

Hi,  
I have this weird situation where a role doesn’t pick up configured variables, hopefully someone can help me out.  
This is parts of my playbook:

- name: Deploy Elasticsearch VMs  
hosts: localhost  
tags:
- deploy  
vars:  
os\_type: Windows  
public\_ip: yes  
use\_max\_datadisks: True  
create\_network\_security\_group: nsg\_eslogging  
create\_availability\_set: yes  
add\_to\_adhoc\_group: elasticsearch

roles:

- { role: customer\_deploy\_azurevm, vm\_name: customer-prod-es1}

- { role: customer\_deploy\_azurevm, vm\_name: customer-prod-es2}

- name: Deploy logstash VMs  
hosts: localhost  
tags:

- deploy  
vars:  
os\_type: Windows  
public\_ip: yes  
max\_data\_disk\_count: 2  
create\_network\_security\_group: nsg\_logstash  
create\_availability\_set: yes  
availability\_set\_name: as-customer-prod-logstash  
add\_to\_adhoc\_group: logstash  
roles:

- { role: customer\_deploy\_azurevm, vm\_name: customer-prod-ls1}

- { role: customer\_deploy\_azurevm, vm\_name: customer-prod-ls2}

The weird thing that happens is that the second play’s vms don’t get the correct availability set (availability\_set\_name), but they DO get the correct nsg (create\_network\_security\_group). The customer\_deploy\_azurevm roles works so that if “create\_availability\_set” is true and “availability\_set\_name” is not set, then an autogenerated availability set name will be used. This variable seems to “linger” so that the following to vms get the previous auto-generated availability set name (I can see this if I dump all vars before I do anything else in the role)

This seems completely weird to me. Am I doing something wrong, or is this a bug?

---

<div class="post-metadata">

**Author:** ![Trond\_Hindenes](https://avatars.discourse-cdn.com/v4/letter/t/96bed5/32.png) [@Trond\_Hindenes](https://forum.ansible.com/u/Trond_Hindenes)\
**Post date:** [June 15, 2016, 7:04pm UTC](https://forum.ansible.com/t/weird-passing-variables-to-roles/21787/2 "2016-06-15T19:04:55Z")

</div>

From what I can see, once a previously undefined variable has been set by a role, the calling playbook is unable to override it on subsequent calls.

---

<div class="post-metadata">

**Author:** ![j.r.hawkesworth](https://avatars.discourse-cdn.com/v4/letter/j/a3d4f5/32.png) [@j.r.hawkesworth](https://forum.ansible.com/u/j.r.hawkesworth)\
**Post date:** [June 16, 2016, 7:45am UTC](https://forum.ansible.com/t/weird-passing-variables-to-roles/21787/3 "2016-06-16T07:45:43Z")

</div>

That is very unexpected.

I pass vars to the same role a lot, although in my case I am usually doing it within the same play (as opposed to within a different play inside the same playbook), and I almost always pass the vars in as -e (extra vars), which from memory have highest precedence.

Looking at your playbook I just wonder if there’s something odd going on to do with the type of the vars. From memory yaml treats unquoted yes and no as boolean true/false values, so it could be that the booleans aren’t getting passed as you’d expect but the strings are.

Maybe it would be worth just experimenting and changing your role so it expects a string for create\_availability\_set, quoting your “yes” and seeing if that makes a difference.

Feels like a bug to me though if that is the case.

Jon

---

<div class="post-metadata">

**Author:** ![Trond\_Hindenes](https://avatars.discourse-cdn.com/v4/letter/t/96bed5/32.png) [@Trond\_Hindenes](https://forum.ansible.com/u/Trond_Hindenes)\
**Post date:** [June 16, 2016, 8:08pm UTC](https://forum.ansible.com/t/weird-passing-variables-to-roles/21787/4 "2016-06-16T20:08:17Z")

</div>

Thanks Jon, I’ll distill it a bit and see what I come up with.

---

<div class="post-metadata">

**Author:** ![Trond\_Hindenes](https://avatars.discourse-cdn.com/v4/letter/t/96bed5/32.png) [@Trond\_Hindenes](https://forum.ansible.com/u/Trond_Hindenes)\
**Post date:** [June 16, 2016, 8:28pm UTC](https://forum.ansible.com/t/weird-passing-variables-to-roles/21787/5 "2016-06-16T20:28:55Z")

</div>

Jon (et al), I’ve created this playbook to distill my problem, available here:

[https://drive.google.com/file/d/0B81YECbGAyfWaW5rbVRQNlpQT2c/view?usp=sharing](https://drive.google.com/file/d/0B81YECbGAyfWaW5rbVRQNlpQT2c/view?usp=sharing)

Same thing happens: Then the (previously undefined) variable “variable3” gets set using set\_fact within a role, subsequent calls to that role is unable to pass another value for that var. Hopefully I’m doing something wrong, I really need this to work 🙂

My output:

PLAY [test thing] \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*

TASK [myrole : print the thing] \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*  
ok: [localhost] =\> {  
“msg”: “Print the var var1”  
}

TASK [myrole : print the other thing (should skip)] \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*  
skipping: [localhost]

TASK [myrole : set the other var] \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*  
ok: [localhost]

TASK [myrole : print the third thing] \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*  
ok: [localhost] =\> {  
“msg”: “Print the third var var3”  
}

PLAY [test thing] \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*

TASK [myrole : print the thing] \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*  
ok: [localhost] =\> {  
“msg”: “Print the var var1”  
}

TASK [myrole : print the other thing (should skip)] \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*  
skipping: [localhost]

TASK [myrole : set the other var] \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*  
ok: [localhost]

TASK [myrole : print the third thing] \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*  
ok: [localhost] =\> {  
“msg”: “Print the third var var3”  
}

PLAY [test thing] \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*

TASK [myrole : print the thing] \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*  
ok: [localhost] =\> {  
“msg”: “Print the var var1”  
}

TASK [myrole : print the other thing (should skip)] \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*  
skipping: [localhost]

TASK [myrole : set the other var] \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*  
ok: [localhost]

TASK [myrole : print the third thing] \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*  
ok: [localhost] =\> {  
“msg”: “Print the third var var3”  
}

PLAY RECAP \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*  
localhost : ok=9 changed=0 unreachable=0 failed=0

---

<div class="post-metadata">

**Author:** ![Trond\_Hindenes](https://avatars.discourse-cdn.com/v4/letter/t/96bed5/32.png) [@Trond\_Hindenes](https://forum.ansible.com/u/Trond_Hindenes)\
**Post date:** [June 16, 2016, 8:53pm UTC](https://forum.ansible.com/t/weird-passing-variables-to-roles/21787/6 "2016-06-16T20:53:11Z")

</div>

I managed to at least get around the error:  
The trick is to use “internal” variables inside the role which never get set outside the role. So if I have 2 “external” vars where the role wiill set some 3rd variable depending on those two, the trick is to not re-use the name of one of the two “external” ones. I’ve posted a working example of this here:

[https://drive.google.com/file/d/0B81YECbGAyfWaW5rbVRQNlpQT2c/view?usp=sharing](https://drive.google.com/file/d/0B81YECbGAyfWaW5rbVRQNlpQT2c/view?usp=sharing)

It’s a little bit of extra work really having to watch out for this but at least I got it working. I’d still consider it a bug tho.

---

<div class="post-metadata">

**Author:** ![Christoph\_Wegener](https://avatars.discourse-cdn.com/v4/letter/c/57b2e6/32.png) [@Christoph\_Wegener](https://forum.ansible.com/u/Christoph_Wegener)\
**Post date:** [June 21, 2016, 2:46pm UTC](https://forum.ansible.com/t/weird-passing-variables-to-roles/21787/7 "2016-06-21T14:46:32Z")

</div>

The section about roles in the ‘Ansible: Up and Running’ book covers this topic quite well.  
The recommendation in the book is to always prefix var names inside the role with the name of the role, which I believe is for the exact reason of avoiding the scenario that you ran into.
