From eef099149785bffb5857ae5d32e7cbd8fe93ad6a Mon Sep 17 00:00:00 2001 From: Sean Sullivan Date: Tue, 18 Jan 2022 04:53:44 -0500 Subject: [PATCH] Auto remove api empty (#259) * update references to ansible.tower to controller, update playbook to be user friendly * remove file * update pre commit * update errors and set default to warn * Lint fix and add changelog Co-authored-by: tompage1994@hotmail.co.uk --- changelogs/fragments/diff_empty_fix.yml | 4 ++++ examples/configure_controller.yml | 23 +++++++++++++++++++++++ plugins/lookup/controller_object_diff.py | 16 +++++++++++++++- 3 files changed, 42 insertions(+), 1 deletion(-) create mode 100644 changelogs/fragments/diff_empty_fix.yml diff --git a/changelogs/fragments/diff_empty_fix.yml b/changelogs/fragments/diff_empty_fix.yml new file mode 100644 index 000000000..6d0796c4d --- /dev/null +++ b/changelogs/fragments/diff_empty_fix.yml @@ -0,0 +1,4 @@ +--- +bugfixes: + - warn on default if the api list fed to controller_object_diff lookup is empty +... diff --git a/examples/configure_controller.yml b/examples/configure_controller.yml index 2d93a517d..7d437c77b 100644 --- a/examples/configure_controller.yml +++ b/examples/configure_controller.yml @@ -117,6 +117,29 @@ set_fact: controller_organization_id: "{{ lookup('awx.awx.controller_api', 'organizations', query_params={ 'name': 'Default' } ,host=controller_hostname, username=controller_username, password=controller_password, verify_ssl=false) }}" + - name: "Set empty lists for testing" + set_fact: + controller_api_results: [] + differential_test_items: [] + + - name: "Error out on empty list" + set_fact: + error_empty_diff: "{{ lookup('controller_object_diff', api_list=controller_api_results, compare_list=differential_test_items, warn_on_empty_api=false ) }}" + ignore_errors: true + register: error_results + + - name: "Warn out on empty list" + set_fact: + warn_empty_diff: "{{ lookup('controller_object_diff', api_list=controller_api_results, compare_list=differential_test_items ) }}" + register: warn_results + + - name: "Assert that the empty list error correctly" + assert: + that: + - error_empty_diff is not defined + - warn_empty_diff | length == 0 + - "'Unable to find items in api_list' in error_results.msg" + - name: Differential Testing include_tasks: "./tasks/differential.yml" loop: "{{ differential_items }}" diff --git a/plugins/lookup/controller_object_diff.py b/plugins/lookup/controller_object_diff.py index cb9418699..be962634b 100644 --- a/plugins/lookup/controller_object_diff.py +++ b/plugins/lookup/controller_object_diff.py @@ -36,6 +36,13 @@ - Include items in the original compare list in the output, and set state: present type: boolean default: True + warn_on_empty_api: + description: + - If the API list is empty, issue an ansible warning and return the empty list. + - This allows the module to be idempotent. + - Setting to false will make the lookup error and fail when there is an empty list. + type: boolean + default: True """ EXAMPLES = """ @@ -75,7 +82,7 @@ """ from ansible.plugins.lookup import LookupBase -from ansible.errors import AnsibleError +from ansible.errors import AnsibleError, AnsibleLookupError from ansible.module_utils._text import to_native from ansible.utils.display import Display @@ -95,6 +102,13 @@ def run(self, terms, variables=None, **kwargs): # Set Variables for user input api_list = self.get_option("api_list") compare_list = self.get_option("compare_list") + warn_on_empty_api = self.get_option("warn_on_empty_api") + if not api_list: + if warn_on_empty_api: + self._display.warning("Skipping, did not find items in api_list") + else: + raise AnsibleLookupError("Unable to find items in api_list") + return [api_list] # Set Keys to keep for each list. keys_to_keep = ["name", "organization"]