Skip to content

Instantly share code, notes, and snippets.

@halberom
Last active July 25, 2022 12:50
Show Gist options
  • Save halberom/4e9d05f684ebe5f0fea0 to your computer and use it in GitHub Desktop.
Save halberom/4e9d05f684ebe5f0fea0 to your computer and use it in GitHub Desktop.
ansible - example of passing registered var containing json to custom module
PLAY [foo] ********************************************************************
GATHERING FACTS ***************************************************************
ok: [localhost]
TASK: [raw stat /tmp/foo] *****************************************************
ok: [localhost -> 127.0.0.1]
TASK: [test_lib ] *************************************************************
ok: [localhost]
TASK: [debug var=output] ******************************************************
ok: [localhost] => {
"output": {
"changed": false,
"invocation": {
"module_args": "",
"module_name": "test_lib"
},
"jsonstuff": true
}
}
- hosts: foo
tasks:
- local_action: raw stat /tmp/foo
register: foo
- test_lib:
command: 'foo'
variable: "{{ foo }}"
register: output
- debug: var=output
#!/usr/bin/env python
# -*- coding: utf-8 -*-
def do_something_funky(module):
#for key,value in module.params['variable']['invocation'].keys():
# do something here
if 'stat' in module.params['variable']['invocation']['module_args']:
return True
else:
return False
def main():
module = AnsibleModule(
argument_spec = dict(
command = dict(default=None, required=True),
variable = dict(default=None, required=True),
)
)
results = do_something_funky(module)
module.exit_json(changed=False, jsonstuff=results)
from ansible.module_utils.basic import *
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment