I have created a custom ansible module in Python, and I'd like to use it this way:
e2e.py
from ansible.module_utils.basic import *
?
?
def func_1(module):
return module.params['var_1'], module.exit_json(changed=False, meta={'info': 'func_1'})
?
?
def func_2(module):
var_1 = module.params['var_1']
var_2, var_3 = module.params['var_2'], module.params['var_3']
?
# use var_1, var_2 and var_3 here and return something
?
return module.exit_json(changed=False, meta={'info': 'func_2'})
?
?
def main():
module = AnsibleModule(argument_spec={
'var_1': {'type': 'str'},
'var_2': {'type': 'str'},
'var_3': {'type': 'str'}
})
?
var_1, resp = func_1(module)
?
module.exit_json(changed=False, meta=resp)
module.exit_json(changed=False, meta=func_2(module))
?
?
if __name__ == '__main__':
main()
ansible-playbook
- hosts: localhost
connection: local
gather_facts: False
?
tasks:
- name: Func 1
e2e:
var_1: "var_1"
register: func_1_resp
- debug:
msg: "{{ func_1_resp }}"
?
- name: Func 2
e2e:
var_2: "var_2"
var_3: "var_3"
register: func_2_resp
- debug:
msg: "{{ func_2_resp }}"
The output I'm receiving is this one:
ok: [localhost] => {
"changed": false,
"invocation": {
"module_args": {
"var_1": null,
"var_2": "var_2",
"var_3": "var_3"
}
},
"meta": {
"info": "func_1"
}
}
TASK [debug] ***************************************************************************************************************************************************************************************************************************
task path: e2e.yml:18
ok: [localhost] => {
"msg": {
"changed": false,
"failed": false,
"meta": {
"info": "func_1"
}
}
}
META: ran handlers
META: ran handlers
PLAY RECAP *****************************************************************************************************************************************************************************************************************************
localhost : ok=4 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
In my playbook I have two tasks Func 1
and Func 2
where I want to use my e2e
module with different arguments (var_1
for the first task and var_2
& var_3
for the second task). Even if I were to provide var_1
to the second task's arguments list and vice-versa, the module.exit_json(changed=False, meta=func_2(module))
would never be called.
How can I run both func_1
and func_2
with different arguments?
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…