summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorBrian Coca <bcoca@users.noreply.github.com>2021-03-26 12:43:16 -0400
committerGitHub <noreply@github.com>2021-03-26 12:43:16 -0400
commit4a82e2c4860b88aa2d5ab2fd1f7cd79636005b5f (patch)
tree583e373251696a292260a89292a7d0d4dc9b3705
parentc66cff444c88b1f7b8d3f01cb6fec07fffe3d52e (diff)
downloadansible-4a82e2c4860b88aa2d5ab2fd1f7cd79636005b5f.tar.gz
Fix setup subset (#74022)
* fix error msg on bad subset * added test * handle more raised but not handled fact exceptions
-rw-r--r--changelogs/fragments/fix_setup_bad_subset.yml2
-rw-r--r--lib/ansible/modules/setup.py23
-rwxr-xr-xtest/integration/targets/gathering_facts/runme.sh3
-rw-r--r--test/integration/targets/gathering_facts/verify_subset.yml13
4 files changed, 31 insertions, 10 deletions
diff --git a/changelogs/fragments/fix_setup_bad_subset.yml b/changelogs/fragments/fix_setup_bad_subset.yml
new file mode 100644
index 0000000000..704d634e92
--- /dev/null
+++ b/changelogs/fragments/fix_setup_bad_subset.yml
@@ -0,0 +1,2 @@
+bugfixes:
+ - setup module, fix error handling on bad subset given
diff --git a/lib/ansible/modules/setup.py b/lib/ansible/modules/setup.py
index 0012564d93..0b7fad9f89 100644
--- a/lib/ansible/modules/setup.py
+++ b/lib/ansible/modules/setup.py
@@ -144,10 +144,10 @@ EXAMPLES = """
# import module snippets
from ..module_utils.basic import AnsibleModule
+from ansible.module_utils._text import to_text
+from ansible.module_utils.facts import ansible_collector, default_collectors
+from ansible.module_utils.facts.collector import CollectorNotFoundError, CycleFoundInFactDeps, UnresolvedFactDep
from ansible.module_utils.facts.namespace import PrefixFactNamespace
-from ansible.module_utils.facts import ansible_collector
-
-from ansible.module_utils.facts import default_collectors
def main():
@@ -180,13 +180,16 @@ def main():
namespace = PrefixFactNamespace(namespace_name='ansible',
prefix='ansible_')
- fact_collector = \
- ansible_collector.get_ansible_collector(all_collector_classes=all_collector_classes,
- namespace=namespace,
- filter_spec=filter_spec,
- gather_subset=gather_subset,
- gather_timeout=gather_timeout,
- minimal_gather_subset=minimal_gather_subset)
+ try:
+ fact_collector = ansible_collector.get_ansible_collector(all_collector_classes=all_collector_classes,
+ namespace=namespace,
+ filter_spec=filter_spec,
+ gather_subset=gather_subset,
+ gather_timeout=gather_timeout,
+ minimal_gather_subset=minimal_gather_subset)
+ except (TypeError, CollectorNotFoundError, CycleFoundInFactDeps, UnresolvedFactDep) as e:
+ # bad subset given, collector, idk, deps declared but not found
+ module.fail_json(msg=to_text(e))
facts_dict = fact_collector.collect(module=module)
diff --git a/test/integration/targets/gathering_facts/runme.sh b/test/integration/targets/gathering_facts/runme.sh
index 4635562710..9904c9e78e 100755
--- a/test/integration/targets/gathering_facts/runme.sh
+++ b/test/integration/targets/gathering_facts/runme.sh
@@ -16,3 +16,6 @@ ansible-playbook verify_merge_facts.yml -v "$@" -e 'ansible_facts_parallel: Fals
# ensure we dont clobber facts in loop
ansible-playbook prevent_clobbering.yml -v "$@"
+
+# ensure we dont fail module on bad subset
+ansible-playbook verify_subset.yml "$@"
diff --git a/test/integration/targets/gathering_facts/verify_subset.yml b/test/integration/targets/gathering_facts/verify_subset.yml
new file mode 100644
index 0000000000..89132756ea
--- /dev/null
+++ b/test/integration/targets/gathering_facts/verify_subset.yml
@@ -0,0 +1,13 @@
+- hosts: localhost
+ gather_facts: false
+ tasks:
+ - name: bad subset used
+ setup: gather_subset=nonsense
+ register: bad_sub
+ ignore_errors: true
+
+ - name: verify we fail the right way
+ assert:
+ that:
+ - bad_sub is failed
+ - "'MODULE FAILURE' not in bad_sub['msg']"