summaryrefslogtreecommitdiffstats
path: root/roles/openshift_sanitize_inventory/library/conditional_set_fact.py
diff options
context:
space:
mode:
authorOpenShift Merge Robot <openshift-merge-robot@users.noreply.github.com>2017-09-22 09:23:52 -0700
committerGitHub <noreply@github.com>2017-09-22 09:23:52 -0700
commit9240e0d006198f910ea01d34d57f04f9ecd8e7e8 (patch)
treef95f8a2fb02155f7978467088b06a3580e25b0b9 /roles/openshift_sanitize_inventory/library/conditional_set_fact.py
parentfe2f31cb1ea499d2683e7e30679eef8ac69857ad (diff)
parentb2fdc1cb5e38e39250af702d1e19ed691e4df7d5 (diff)
downloadopenshift-9240e0d006198f910ea01d34d57f04f9ecd8e7e8.tar.gz
openshift-9240e0d006198f910ea01d34d57f04f9ecd8e7e8.tar.bz2
openshift-9240e0d006198f910ea01d34d57f04f9ecd8e7e8.tar.xz
openshift-9240e0d006198f910ea01d34d57f04f9ecd8e7e8.zip
Merge pull request #5176 from ewolinetz/deprecate_log_met_vars
Automatic merge from submit-queue Creating structure to warn for use of deprecated variables and set th… …em in a single location before they are no longer honored Implementation of https://trello.com/c/XKOGHSkP/460-3-deprecate-openshifthostedloggingmetrics-installer-variables Tried to implement this in a way such that we can easily and cleanly add/remove variables in the future for any role we choose. Addresses: https://bugzilla.redhat.com/show_bug.cgi?id=1449812 https://bugzilla.redhat.com/show_bug.cgi?id=1464349
Diffstat (limited to 'roles/openshift_sanitize_inventory/library/conditional_set_fact.py')
-rw-r--r--roles/openshift_sanitize_inventory/library/conditional_set_fact.py68
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()