Pass a dynamic dictionary as parameter

23 views
Skip to first unread message

Mumshad Mannambeth

unread,
Nov 30, 2016, 12:02:45 PM11/30/16
to Ansible Project
How can I create and pass a dynamic dictionary as a module parameter. I have a custom module that accepts parameters:

-
            name
: 'Custom module'
            custom_module
:
                 parameter1
: 'Some Value'
                 parameter2
:
                     
'{{ var1 }}' : '{{ value1 }}'
                     
'{{ var2 }}' : '{{ value2 }}'


I tried the below:
-
            name
: 'Custom module'
            custom_module
:
                 parameter1
: 'Some Value'
                 parameter2
: '{{ item }}'
           with_dict
:
                 
"{'{{var1}}': '{{value1}}'}"

But item is in key:value format {key: var1, value: value1} , instead I need it in format - { var1 : value1 }

How can I achieve this?

Ted Zlatanov

unread,
Nov 30, 2016, 12:24:41 PM11/30/16
to ansible...@googlegroups.com
On Wed, 30 Nov 2016 09:02:45 -0800 (PST) Mumshad Mannambeth <mmum...@gmail.com> wrote:

MM> with_dict:
MM> "{'{{var1}}': '{{value1}}'}"

MM> But item is in key:value format {key: var1, value: value1} , instead I need
MM> it in format - { var1 : value1 }

Just make the constructed parameters a list of maps:

#+begin_src ansible
#!/usr/bin/ansible-playbook

- hosts: localhost
tasks:
- set_fact:
var1: x
value1: 100
var2: y
value2: 200

- debug: var=item
with_items: "{{ [ { var1: value1 }, { var2: value2 } ] }}"
#+end_src

Output:

#+begin_src text
TASK [debug] *******************************************************************
ok: [localhost] => (item={u'x': 100}) => {
"item": {
"x": 100
}
}
ok: [localhost] => (item={u'y': 200}) => {
"item": {
"y": 200
}
}
#+end_src

Mumshad Mannambeth

unread,
Nov 30, 2016, 4:17:07 PM11/30/16
to Ansible Project, t...@lifelogs.com
Thank you very much!!
Reply all
Reply to author
Forward
0 new messages