Skip to content

Instantly share code, notes, and snippets.

@jpmens
Last active August 29, 2015 14:19
Show Gist options
  • Save jpmens/4b67010fc6cbe9ff42bc to your computer and use it in GitHub Desktop.
Save jpmens/4b67010fc6cbe9ff42bc to your computer and use it in GitHub Desktop.
Ansible complex module data?????
---
zones:
- { zone: one.de, policy: rfc5011 }
- { zone: thirteen.com, policy: default }
#!/usr/bin/python
# -*- coding: utf-8 -*-
import os
import ast
try:
import json
except ImportError:
import simplejson as json
# ==============================================================
# main
def main():
argument_spec = url_argument_spec()
argument_spec.update(
zlist = dict(required=True, type='str')
)
module = AnsibleModule(
argument_spec = argument_spec,
)
zlist = module.params['zlist']
#f = open('/tmp/j', 'w')
#f.write(zlist)
#f.close()
# >> [{'policy': 'rfc5011', 'zone': 'one.de'}, {'policy': 'default', 'zone': 'thirteen.com'}]
try:
data = json.loads(zlist) # zlist has single quotes ('param': 'value') which is wrong
except:
data = ast.literal_eval(zlist)
res = []
for z in data:
res.append(z['zone'].upper())
module.exit_json(changed=True, msg='OK', newdata=res)
# import module snippets
from ansible.module_utils.basic import *
from ansible.module_utils.urls import *
main()
---
- hosts:
- localhost
connection: local
vars_files:
- comdata-vars.yml
tasks:
- name: COMplex DATa
action: comdata
# zlist="{{ zones }}"
args:
zlist: "{{ zones }}"
# zlist: "{{ lookup('file', 'f.json') }}"
register: n
- action: debug var=n.newdata
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment