Cannot read group_variable from role task - Ansible 2.3.1.0 (works fine with 2.1) -

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

Paulo,

I Think the fix should be:

`

  • name: ‘Item’
    debug:
    var: item
    with_items: “{{ databases}}”
    `

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'
   debug:
     var: item.name
   with_items: '{{ databases }}'

I confirm this resolve my issue, thanks!

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.