# Variable "role" in play is undefined

**URL:** <https://forum.ansible.com/t/variable-role-in-play-is-undefined/21244>\
**Category:** Ansible Project\
**Created:** [April 22, 2016, 2:00pm UTC](https://forum.ansible.com/t/variable-role-in-play-is-undefined/21244 "2016-04-22T14:00:15Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Jeff\_Hardy](https://avatars.discourse-cdn.com/v4/letter/j/ecae2f/32.png) [@Jeff\_Hardy](https://forum.ansible.com/u/Jeff_Hardy)\
**Post date:** [April 22, 2016, 2:00pm UTC](https://forum.ansible.com/t/variable-role-in-play-is-undefined/21244/1 "2016-04-22T14:00:15Z")

</div>

I have a static inventory setup with the hope of applying different roles to different hosts, but am finding that this playbook is not achieving what I would like:

- hosts: “{{target}}”  
pre\_tasks:
- debug: var=role  
roles:
- “{{role}}”  
tasks:
- debug: var=role  
post\_tasks:
- debug: var=role

Ultimately with an inventory of:

192.168.1.13 role=test

Executing this playbook:

ansible-playbook -i inventory testrole.yml -e “target=192.168.1.13”

ERROR! ‘role’ is undefined

This persists no matter where I attempt to define that role variable, in inventory, group\_vars, host\_vars, etc. If I comment out the roles stanza, all the debug tasks report the variable being interpolated correctly no matter how I do it. The only way I have found the role to be variable so far is to define it with --extra-args at the command line.

I this a bug, or am I running into a hard boundary of ansible?

---

<div class="post-metadata">

**Author:** ![Johannes\_Kastl](https://avatars.discourse-cdn.com/v4/letter/j/bbe5ce/32.png) [@Johannes\_Kastl](https://forum.ansible.com/u/Johannes_Kastl)\
**Post date:** [April 22, 2016, 6:18pm UTC](https://forum.ansible.com/t/variable-role-in-play-is-undefined/21244/2 "2016-04-22T18:18:03Z")

</div>

^^^^^^^^  
Maybe you need a different variable here, something like:  
hostvars[target]['role']

Johannes

---

<div class="post-metadata">

**Author:** ![Brian\_Coca](https://avatars.discourse-cdn.com/v4/letter/b/6de8d8/32.png) [@Brian\_Coca](https://forum.ansible.com/u/Brian_Coca)\
**Post date:** [April 22, 2016, 6:56pm UTC](https://forum.ansible.com/t/variable-role-in-play-is-undefined/21244/3 "2016-04-22T18:56:07Z")

</div>

Also i don’t recommend using vars that have same name as keywords.

---

<div class="post-metadata">

**Author:** ![Jeff\_Hardy](https://avatars.discourse-cdn.com/v4/letter/j/ecae2f/32.png) [@Jeff\_Hardy](https://forum.ansible.com/u/Jeff_Hardy)\
**Post date:** [April 22, 2016, 8:59pm UTC](https://forum.ansible.com/t/variable-role-in-play-is-undefined/21244/4 "2016-04-22T20:59:49Z")

</div>

I tried your suggestion, and also Brian’s following suggestion of a different variable, and it reports:

ERROR! ‘hostvars’ is undefined

And I can debug out all the hostvars in all the task calls with no issue as long as I am not trying to call the role.

pre\_tasks:

- debug: var=dorole
- debug: var=hostvars[inventory\_hostname][‘dorole’]

Maybe an order of processing issue?

---

<div class="post-metadata">

**Author:** ![Brian\_Coca](https://avatars.discourse-cdn.com/v4/letter/b/6de8d8/32.png) [@Brian\_Coca](https://forum.ansible.com/u/Brian_Coca)\
**Post date:** [April 22, 2016, 9:01pm UTC](https://forum.ansible.com/t/variable-role-in-play-is-undefined/21244/5 "2016-04-22T21:01:52Z")

</div>

Sorry, should have realized on first post:

you CANNOT define which role is loaded based on host variables, as the roles NEED to be the same for all hosts and get loaded BEFORE host variables are available.

---

<div class="post-metadata">

**Author:** ![Jeff\_Hardy](https://avatars.discourse-cdn.com/v4/letter/j/ecae2f/32.png) [@Jeff\_Hardy](https://forum.ansible.com/u/Jeff_Hardy)\
**Post date:** [April 22, 2016, 9:46pm UTC](https://forum.ansible.com/t/variable-role-in-play-is-undefined/21244/6 "2016-04-22T21:46:14Z")

</div>

It definitely seemed like I was bumping into something like that. Thank you for the info.
