with_dict expects a dict - when evaluating a variable in ansible 2.0+

1,558 views
Skip to first unread message

Florian B

unread,
May 31, 2016, 8:12:52 AM5/31/16
to Ansible Project
Hi everyone,

I'm trying to get this example to work with ansible 2.0+. It does work correctly with 1.9.

- hosts: localhost
  vars
:


    input
: "top"


   
base: &base
     
? a
     
? b


    top
: &top
     
<< : *base
     
? c
     
? d

    peak
: &peak
     
<< : *top
     
? e
     
? f

  tasks
:
   
- debug: var="{{ input|lower|regex_replace(' ','') }}"


   
- name: Loop
      command
: echo {{ item.key }}
      with_dict
: "{{ input|lower|regex_replace(' ','') }}"

In short, I want to merge the dicts base, top and peak and then loop over all key/value pairs in the corresponding dictionary, depending on the input another function produces (strings).

With ansible 2.0 I get the following error:
 % ansible --version
ansible 2.0.0.2 (stable-2.0.0.1 7de237c5a1) last updated 2016/03/15 11:07:49 (GMT +200)
  lib/ansible/modules/core: (detached HEAD ce6619bf5d) last updated 2016/03/15 11:07:51 (GMT +200)
  lib/ansible/modules/extras: (detached HEAD 29af26884e) last updated 2016/03/15 11:07:51 (GMT +200)
  config file = /home/fbuchmeier/.ansible.cfg
  configured module search path = Default w/o overrides 

 % ansible-playbook -c local with_dict.yml
 
[WARNING]: provided hosts list is empty, only localhost is available




PLAY
***************************************************************************


TASK
[setup] *******************************************************************
ok
: [localhost]


TASK
[debug] *******************************************************************
ok
: [localhost] => {
   
"top": {
       
"a": null,
       
"b": null,
       
"c": null,
       
"d": null
   
}
}


TASK
[Loop] ********************************************************************
fatal
: [localhost]: FAILED! => {"failed": true, "msg": "ERROR! with_dict expects a dict"}


PLAY RECAP
*********************************************************************
localhost                  
: ok=2    changed=0    unreachable=0    failed=1

In ansible 1.9 I get my desired result:
 % ansible --version
ansible 1.9.6 (detached HEAD 10a38a7652) last updated 2016/04/29 15:14:07 (GMT +200)
  lib/ansible/modules/core: (detached HEAD 6972d0a291) last updated 2016/04/29 15:14:13 (GMT +200)
  lib/ansible/modules/extras: (detached HEAD 2c073442b0) last updated 2016/04/29 15:14:13 (GMT +200)
  configured module search path = None
 
% ansible-playbook -i environments/localhost  -c local with_dict.yml


PLAY
[localhost] **************************************************************


GATHERING FACTS
***************************************************************
ok
: [localhost]


TASK
: [debug var="{{ input|lower|regex_replace(' ','') }}"] *******************
ok
: [localhost] => {
   
"var": {
       
"top": {
           
"a": null,
           
"b": null,
           
"c": null,
           
"d": null
       
}
   
}
}


TASK
: [Loop] ******************************************************************
changed
: [localhost] => (item={'key': 'a', 'value': None})
changed
: [localhost] => (item={'key': 'c', 'value': None})
changed
: [localhost] => (item={'key': 'b', 'value': None})
changed
: [localhost] => (item={'key': 'd', 'value': None})


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

Do you have any idea how I can get this to work?

Thank you very much,

Florian.

Florian B

unread,
Aug 1, 2016, 3:55:27 PM8/1/16
to Ansible Project
The solution is to address the variable like this (vars['variableName']):

    - name: Loop
      command
: echo {{ item.key }}

      with_dict
: "{{ vars[input|lower|regex_replace(' ','')] }}"

Reply all
Reply to author
Forward
0 new messages