Last active
July 25, 2022 12:50
-
-
Save halberom/4e9d05f684ebe5f0fea0 to your computer and use it in GitHub Desktop.
ansible - example of passing registered var containing json to custom module
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
- hosts: foo | |
tasks: | |
- local_action: raw stat /tmp/foo | |
register: foo | |
- test_lib: | |
command: 'foo' | |
variable: "{{ foo }}" | |
register: output | |
- debug: var=output |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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