blob: f618017148e8c775bf9bc550ee8bd87b38446f65 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
|
#!/usr/bin/python
""" Ansible module to help with setting facts conditionally based on other facts """
from ansible.module_utils.basic import AnsibleModule
DOCUMENTATION = '''
---
module: conditional_set_fact
short_description: This will set a fact if the value is defined
description:
- "To avoid constant set_fact & when conditions for each var we can use this"
author:
- Eric Wolinetz ewolinet@redhat.com
'''
EXAMPLES = '''
- name: Conditionally set fact
conditional_set_fact:
fact1: not_defined_variable
- name: Conditionally set fact
conditional_set_fact:
fact1: not_defined_variable
fact2: defined_variable
'''
def run_module():
""" The body of the module, we check if the variable name specified as the value
for the key is defined. If it is then we use that value as for the original key """
module = AnsibleModule(
argument_spec=dict(
facts=dict(type='dict', required=True),
vars=dict(required=False, type='dict', default=[])
),
supports_check_mode=True
)
local_facts = dict()
is_changed = False
for param in module.params['vars']:
other_var = module.params['vars'][param]
if other_var in module.params['facts']:
local_facts[param] = module.params['facts'][other_var]
if not is_changed:
is_changed = True
return module.exit_json(changed=is_changed, # noqa: F405
ansible_facts=local_facts)
def main():
""" main """
run_module()
if __name__ == '__main__':
main()
|