Browse Source

Changing conditional_set_fact from module to action_plugin since it does not need to access hosts to be effective and to reduce playbook output

Eric Wolinetz 7 years ago
parent
commit
6abf5d8933

+ 65 - 0
roles/lib_openshift/action_plugins/conditional_set_fact.py

@@ -0,0 +1,65 @@
+"""
+Ansible action plugin to help with setting facts conditionally based on other facts.
+"""
+
+from ansible.plugins.action import ActionBase
+
+
+DOCUMENTATION = '''
+---
+action_plugin: 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
+
+- name: Conditionally set fact falling back on default
+  conditional_set_fact:
+    fact1: not_defined_var | defined_variable
+
+'''
+
+
+# pylint: disable=too-few-public-methods
+class ActionModule(ActionBase):
+    """Action plugin to execute deprecated var checks."""
+
+    def run(self, tmp=None, task_vars=None):
+        result = super(ActionModule, self).run(tmp, task_vars)
+        result['changed'] = False
+
+        facts = self._task.args.get('facts', [])
+        var_list = self._task.args.get('vars', [])
+
+        local_facts = dict()
+
+        for param in var_list:
+            other_vars = var_list[param].replace(" ", "")
+
+            for other_var in other_vars.split('|'):
+                if other_var in facts:
+                    local_facts[param] = facts[other_var]
+                    break
+
+        if local_facts:
+            result['changed'] = True
+
+        result['ansible_facts'] = local_facts
+
+        return result

+ 0 - 74
roles/lib_openshift/library/conditional_set_fact.py

@@ -1,74 +0,0 @@
-#!/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
-
-- name: Conditionally set fact falling back on default
-  conditional_set_fact:
-    fact1: not_defined_var | 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_vars = module.params['vars'][param].replace(" ", "")
-
-        for other_var in other_vars.split('|'):
-            if other_var in module.params['facts']:
-                local_facts[param] = module.params['facts'][other_var]
-                if not is_changed:
-                    is_changed = True
-                break
-
-    return module.exit_json(changed=is_changed,  # noqa: F405
-                            ansible_facts=local_facts)
-
-
-def main():
-    """ main """
-    run_module()
-
-
-if __name__ == '__main__':
-    main()