Skip to content

Instantly share code, notes, and snippets.

@uncreative
Created August 24, 2014 05:17
Show Gist options
  • Save uncreative/adfe926267d0ee5b3de3 to your computer and use it in GitHub Desktop.
Save uncreative/adfe926267d0ee5b3de3 to your computer and use it in GitHub Desktop.
module with dictionary parameter and variables within dictionary

$ ansible-playbook playtest.yml -v

PLAY [127.0.0.1] **************************************************************

GATHERING FACTS *************************************************************** ok: [127.0.0.1]

TASK: [hard coded thingy] *****************************************************

ok: [127.0.0.1] => {"changed": false, "msg": "

  • stringy_var(<type 'unicode'>) : asdf(<type 'unicode'>) ||
  • inty_var(<type 'unicode'>) : 3(<type 'int'>) ||
  • floaty_var(<type 'unicode'>) : 123.21(<type 'float'>) || "}
TASK: [thingy with vars] ******************************************************

ok: [127.0.0.1] => (item={'inty': 3, 'floaty': 123.21, 'stringy': 'asdf'}) => {"changed": false, "item": {"floaty": 123.21, "inty": 3, "stringy": "asdf"}, "msg": "

  • stringy_var(<type 'unicode'>) : asdf(<type 'unicode'>) ||
  • inty_var(<type 'unicode'>) : 3(<type 'unicode'>) ||
  • floaty_var(<type 'unicode'>) : 123.21(<type 'unicode'>) || "}
TASK: [thingy with vars] ******************************************************

ok: [127.0.0.1] => (item={'inty': 3, 'floaty': 123.21, 'stringy': 'asdf'}) => {"changed": false, "item": {"floaty": 123.21, "inty": 3, "stringy": "asdf"}, "msg": "

  • stringy_var(<type 'unicode'>) : asdf(<type 'unicode'>) ||
  • inty_var(<type 'unicode'>) : 3(<type 'unicode'>) ||
  • floaty_var(<type 'unicode'>) : 123.21(<type 'unicode'>) || "}

PLAY RECAP ******************************************************************** 127.0.0.1 : ok=4 changed=0 unreachable=0 failed=0

#!/usr/bin/env python
import sys
import os
def main():
module = AnsibleModule(
argument_spec=dict(
thingy=dict(required=True,),
),
)
params = module.params
thingy = module.params['thingy']
msg = ''
for k, v in thingy.items():
msg += " %s(%s) : %s(%s) || " % (str(k), type(k), str(v), type(v))
module.exit_json(changed=False, msg=msg)
# import module snippets
from ansible.module_utils.basic import *
from ansible.module_utils.splitter import *
main()
---
# run like this: ansible-playbook playtest.yml -v
- hosts: 127.0.0.1
connection: local
tasks:
- name: hard coded thingy
parametered_module.py:
thingy:
stringy_var: 'asdf'
inty_var: 3
floaty_var: 123.21
- name: thingy with vars
parametered_module.py:
thingy:
stringy_var: "{{ item['stringy'] }}"
inty_var: "{{ item['inty'] }}"
floaty_var: "{{ item['floaty'] }}"
with_items:
- {'stringy': 'asdf', 'inty': 3, 'floaty': 123.21}
- name: thingy with vars
parametered_module.py:
thingy:
stringy_var: "{{ item['stringy'] }}"
inty_var: "{{ item['inty'] | int }}"
floaty_var: "{{ item['floaty'] | float }}"
with_items:
- {'stringy': 'asdf', 'inty': 3, 'floaty': 123.21}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment