# Cannot read group\_variable from role task - Ansible 2.3.1.0 (works fine with 2.1) -

**URL:** <https://forum.ansible.com/t/cannot-read-group-variable-from-role-task-ansible-2-3-1-0-works-fine-with-2-1/24897>\
**Category:** Ansible Project\
**Created:** [July 5, 2017, 10:53am UTC](https://forum.ansible.com/t/cannot-read-group-variable-from-role-task-ansible-2-3-1-0-works-fine-with-2-1/24897 "2017-07-05T10:53:20Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Paolo\_Galizzi](https://avatars.discourse-cdn.com/v4/letter/p/67e7ee/32.png) [@Paolo\_Galizzi](https://forum.ansible.com/u/Paolo_Galizzi)\
**Post date:** [July 5, 2017, 10:53am UTC](https://forum.ansible.com/t/cannot-read-group-variable-from-role-task-ansible-2-3-1-0-works-fine-with-2-1/24897/1 "2017-07-05T10:53:20Z")

</div>

Hello,

I have some trouble on reusing a code wrote for Ansible 2.1.1.0 with Ansible 2.3.1.0.  
With Ansible 2.3 when I try reading, from a task of a role, a nested variable that comes from “group\_vars”, tells me that variable isn’t define.

I remove all unnecessary from my code and create an example code (following).

The structure is :  
`

- test.yml
- group\_vars:
- db.yml
- hosts:
- dev
- roles:
- dbtest:
- tasks:
- main.yml

`

Where “test.yml” is:

`

- name: test  
hosts: db  
gather\_facts: false  
roles:
- dbtest

`

“db.yml” under group\_vars is:  
`  
databases:

- name: dbName  
encoding: utf8

`

“dev” under “hosts” is :  
`  
[local-db]  
localhost

[db:children]  
local-db

`

“main.yml” under “roles/dbtest/tasks” is:

`

- name: ‘Print databases’  
debug:  
var: databases

- name: ‘Item’  
debug:  
var: item  
with\_items: databases

- name: ‘Item Name’  
debug:  
var: item.name  
with\_items: databases

`

When I run the code with Ansible 2.1.1.0 calling

`ansible-playbook -i hosts/dev test.yml`

,I got this output:

`

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

TASK [dbtest : Print databases] \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*  
ok: [localhost] =\> {  
“databases”: [  
{  
“encoding”: “utf8”,  
“name”: “dbName”  
}  
]  
}

TASK [dbtest : Item] \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*  
ok: [localhost] =\> (item={u’name’: u’dbName’, u’encoding’: u’utf8’}) =\> {  
“item”: {  
“encoding”: “utf8”,  
“name”: “dbName”  
}  
}

TASK [dbtest : Item Name] \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*  
ok: [localhost] =\> (item={u’name’: u’dbName’, u’encoding’: u’utf8’}) =\> {  
“item”: {  
“encoding”: “utf8”,  
“name”: “dbName”  
},  
“item.name”: “dbName”  
}

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

When I run with Ansible 2.3.1.0 the output is:

`  
PLAY [test] \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*

TASK [dbtest : Print databases] \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*  
ok: [localhost] =\> {  
“databases”: [  
{  
“encoding”: “utf8”,  
“name”: “dbName”  
}  
]  
}

TASK [dbtest : Item] \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*  
ok: [localhost] =\> (item=databases) =\> {  
“item”: “databases”  
}

TASK [dbtest : Item Name] \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*  
ok: [localhost] =\> (item=databases) =\> {  
“item”: “databases”,  
“item.name”: “VARIABLE IS NOT DEFINED!”  
}

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

As you can see, on the last step when I try to read “item.name” the old one works, the new one doesn’t see the variable.

I’d like to understand if is possible and how modify my code in order that will work also with Ansible 2.3 (considering that in my full code I widely use nested variable as describe above.

Tell me if you need more information and if you have any suggestion, thanks

Paolo

---

<div class="post-metadata">

**Author:** ![Rick\_Stokkingreef](https://avatars.discourse-cdn.com/v4/letter/r/a587f6/32.png) [@Rick\_Stokkingreef](https://forum.ansible.com/u/Rick_Stokkingreef)\
**Post date:** [July 6, 2017, 7:03am UTC](https://forum.ansible.com/t/cannot-read-group-variable-from-role-task-ansible-2-3-1-0-works-fine-with-2-1/24897/2 "2017-07-06T07:03:05Z")

</div>

Paulo,

I Think the fix should be:

`

- name: ‘Item’  
debug:  
var: item  
with\_items: “{{ databases}}”  
`

---

<div class="post-metadata">

**Author:** ![Kai\_Stian\_Olstad](https://avatars.discourse-cdn.com/v4/letter/k/e19adc/32.png) [@Kai\_Stian\_Olstad](https://forum.ansible.com/u/Kai_Stian_Olstad)\
**Post date:** [July 6, 2017, 9:22am UTC](https://forum.ansible.com/t/cannot-read-group-variable-from-role-task-ansible-2-3-1-0-works-fine-with-2-1/24897/3 "2017-07-06T09:22:42Z")

</div>

In earlier Ansible version "with\_items: databases" the databases was seen as a variable, in newer version it's seen as the literal string databases.

So in 2.3 you need to add the curly brackets but this syntax also works in previous version of Ansible.

- name: 'Item Name'  
&nbsp;&nbsp;&nbsp;debug:  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;var: item.name  
&nbsp;&nbsp;&nbsp;with\_items: '{{ databases }}'

---

<div class="post-metadata">

**Author:** ![Paolo\_Galizzi](https://avatars.discourse-cdn.com/v4/letter/p/67e7ee/32.png) [@Paolo\_Galizzi](https://forum.ansible.com/u/Paolo_Galizzi)\
**Post date:** [July 6, 2017, 11:07am UTC](https://forum.ansible.com/t/cannot-read-group-variable-from-role-task-ansible-2-3-1-0-works-fine-with-2-1/24897/4 "2017-07-06T11:07:15Z")

</div>

I confirm this resolve my issue, thanks!

---

<div class="post-metadata">

**Author:** ![Daniel\_JD](https://avatars.discourse-cdn.com/v4/letter/d/f19dbf/32.png) [@Daniel\_JD](https://forum.ansible.com/u/Daniel_JD)\
**Post date:** [July 8, 2017, 12:29pm UTC](https://forum.ansible.com/t/cannot-read-group-variable-from-role-task-ansible-2-3-1-0-works-fine-with-2-1/24897/5 "2017-07-08T12:29:18Z")

</div>

Good thing to remember:

Always use curly brackets if you want to access variables, except in in “when”- directives (failed\_when, success\_when, when). Conditionals are always processed by jinja2 templating.
