# Programmatic access to variables defined inside a role not working

**URL:** <https://forum.ansible.com/t/programmatic-access-to-variables-defined-inside-a-role-not-working/15135>\
**Category:** Ansible Project\
**Created:** [May 1, 2014, 5:31pm UTC](https://forum.ansible.com/t/programmatic-access-to-variables-defined-inside-a-role-not-working/15135 "2014-05-01T17:31:13Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![edrozenberg](https://avatars.discourse-cdn.com/v4/letter/e/a6a055/32.png) [@edrozenberg](https://forum.ansible.com/u/edrozenberg)\
**Post date:** [May 1, 2014, 5:31pm UTC](https://forum.ansible.com/t/programmatic-access-to-variables-defined-inside-a-role-not-working/15135/1 "2014-05-01T17:31:13Z")

</div>

Hello,

I need to programmatically access variables defined with a role ( via myrole/vars/main.yaml)

I tried using the trick described in the Ansible FAQ - {{ hostvars[inventory\_hostname][myvar] }} but it appears variables defined within the role are not included in this hostvars namespace.

Would love to figure out a way to access my role variables programmatically.

Thanks in advance,  
–Ed

---

<div class="post-metadata">

**Author:** ![edrozenberg](https://avatars.discourse-cdn.com/v4/letter/e/a6a055/32.png) [@edrozenberg](https://forum.ansible.com/u/edrozenberg)\
**Post date:** [May 1, 2014, 5:47pm UTC](https://forum.ansible.com/t/programmatic-access-to-variables-defined-inside-a-role-not-working/15135/2 "2014-05-01T17:47:36Z")

</div>

I noticed there is a difference in how Ansible 1.5.5 handles this vs. Ansible 1.6 Git May 1, 2014.

Ansible 1.6 Git is now including role variables in the hostvars scope. I have one remaining issue  
in 1.6 which is if the variable being referenced itself references another variable, the ultimate value  
is not being properly assembled.

slack\_mir: ‘rsync://slackware.cs.utah.edu’

slack\_dir: ‘slackware/slackware64-{{ slack\_ver }}’

slack\_url: ‘{{ slack\_mir }}/{{ slack\_dir }}’

alpine\_sb\_url: ‘{{ slack\_url }}/source/n/alpine’

pkg: ‘alpine’

When I reference as below, the final variable is not being properly assembled  
as seen in the Ansible 1.6 error message below.

{{ hostvars[inventory\_hostname][pkg + ‘\_sb\_url’] }}

Ansible 1.5.5

---

<div class="post-metadata">

**Author:** ![Michael\_DeHaan1](https://avatars.discourse-cdn.com/v4/letter/m/5e9695/32.png) [@Michael\_DeHaan1](https://forum.ansible.com/u/Michael_DeHaan1)\
**Post date:** [May 1, 2014, 9:29pm UTC](https://forum.ansible.com/t/programmatic-access-to-variables-defined-inside-a-role-not-working/15135/3 "2014-05-01T21:29:59Z")

</div>

Does your task have an explicit name in the playbook?

(Can we see the playbook?)
