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

72 views
Skip to first unread message

Paolo Galizzi

unread,
Jul 5, 2017, 6:54:11 PM7/5/17
to Ansible Project
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

Kai Stian Olstad

unread,
Jul 6, 2017, 5:23:03 AM7/6/17
to ansible...@googlegroups.com
On 05.07.2017 12:53, Paolo Galizzi wrote:
> - name: 'Item'
> debug:
> var: item
> with_items: databases
>
>
> - name: 'Item Name'
> debug:
> var: item.name
> 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 }}'


--
Kai Stian Olstad

Paolo Galizzi

unread,
Jul 6, 2017, 1:11:24 PM7/6/17
to Ansible Project, ansible-pr...@olstad.com
I confirm this resolve my issue, thanks!

Rick Stokkingreef

unread,
Jul 6, 2017, 1:11:24 PM7/6/17
to Ansible Project
Paulo,

I Think the fix should be:
- name: 'Item'
  debug
:
   
var: item
  with_items
: "{{ databases}}"



Op donderdag 6 juli 2017 00:54:11 UTC+2 schreef Paolo Galizzi:

Daniel JD

unread,
Jul 8, 2017, 8:29:18 AM7/8/17
to Ansible Project, ansible-pr...@olstad.com
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.
Reply all
Reply to author
Forward
0 new messages