diff options
author | Eric Wolinetz <ewolinet@redhat.com> | 2017-08-23 09:56:34 -0500 |
---|---|---|
committer | Eric Wolinetz <ewolinet@redhat.com> | 2017-09-20 16:43:27 -0500 |
commit | b2fdc1cb5e38e39250af702d1e19ed691e4df7d5 (patch) | |
tree | b5e8368514f9ae29f6fefef1d5e7ae7d8c1ee95c /roles/openshift_sanitize_inventory/library/conditional_set_fact.py | |
parent | 457e0f80784820b9ff0fb6a7407c271731f5b1d9 (diff) | |
download | openshift-b2fdc1cb5e38e39250af702d1e19ed691e4df7d5.tar.gz openshift-b2fdc1cb5e38e39250af702d1e19ed691e4df7d5.tar.bz2 openshift-b2fdc1cb5e38e39250af702d1e19ed691e4df7d5.tar.xz openshift-b2fdc1cb5e38e39250af702d1e19ed691e4df7d5.zip |
Creating structure to warn for use of deprecated variables and set them in a single location before they are no longer honored
Diffstat (limited to 'roles/openshift_sanitize_inventory/library/conditional_set_fact.py')
-rw-r--r-- | roles/openshift_sanitize_inventory/library/conditional_set_fact.py | 68 |
1 files changed, 68 insertions, 0 deletions
diff --git a/roles/openshift_sanitize_inventory/library/conditional_set_fact.py b/roles/openshift_sanitize_inventory/library/conditional_set_fact.py new file mode 100644 index 000000000..f61801714 --- /dev/null +++ b/roles/openshift_sanitize_inventory/library/conditional_set_fact.py @@ -0,0 +1,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() |