Browse code

Fix setup subset (#74022) (#74047)

* fix error msg on bad subset
* added test
* handle more raised but not handled fact exceptions

(cherry picked from commit 4a82e2c4860b88aa2d5ab2fd1f7cd79636005b5f)

* Update fix_setup_bad_subset.yml

Co-authored-by: Rick Elrod <rick@elrod.me>

Brian Coca authored on 2021/04/05 15:34:26
Showing 4 changed files
1 1
new file mode 100644
... ...
@@ -0,0 +1,2 @@
0
+bugfixes:
1
+  - setup - fix error handling on bad subset given.
... ...
@@ -126,10 +126,10 @@ EXAMPLES = """
126 126
 # import module snippets
127 127
 from ..module_utils.basic import AnsibleModule
128 128
 
129
+from ansible.module_utils._text import to_text
130
+from ansible.module_utils.facts import ansible_collector, default_collectors
131
+from ansible.module_utils.facts.collector import CollectorNotFoundError, CycleFoundInFactDeps, UnresolvedFactDep
129 132
 from ansible.module_utils.facts.namespace import PrefixFactNamespace
130
-from ansible.module_utils.facts import ansible_collector
131
-
132
-from ansible.module_utils.facts import default_collectors
133 133
 
134 134
 
135 135
 def main():
... ...
@@ -162,13 +162,16 @@ def main():
162 162
     namespace = PrefixFactNamespace(namespace_name='ansible',
163 163
                                     prefix='ansible_')
164 164
 
165
-    fact_collector = \
166
-        ansible_collector.get_ansible_collector(all_collector_classes=all_collector_classes,
167
-                                                namespace=namespace,
168
-                                                filter_spec=filter_spec,
169
-                                                gather_subset=gather_subset,
170
-                                                gather_timeout=gather_timeout,
171
-                                                minimal_gather_subset=minimal_gather_subset)
165
+    try:
166
+        fact_collector = ansible_collector.get_ansible_collector(all_collector_classes=all_collector_classes,
167
+                                                                 namespace=namespace,
168
+                                                                 filter_spec=filter_spec,
169
+                                                                 gather_subset=gather_subset,
170
+                                                                 gather_timeout=gather_timeout,
171
+                                                                 minimal_gather_subset=minimal_gather_subset)
172
+    except (TypeError, CollectorNotFoundError, CycleFoundInFactDeps, UnresolvedFactDep) as e:
173
+        # bad subset given, collector, idk, deps declared but not found
174
+        module.fail_json(msg=to_text(e))
172 175
 
173 176
     facts_dict = fact_collector.collect(module=module)
174 177
 
... ...
@@ -16,3 +16,6 @@ ansible-playbook verify_merge_facts.yml -v "$@" -e 'ansible_facts_parallel: Fals
16 16
 
17 17
 # ensure we dont clobber facts in loop
18 18
 ansible-playbook prevent_clobbering.yml -v "$@"
19
+
20
+# ensure we dont fail module on bad subset
21
+ansible-playbook verify_subset.yml "$@"
19 22
new file mode 100644
... ...
@@ -0,0 +1,13 @@
0
+- hosts: localhost
1
+  gather_facts: false
2
+  tasks:
3
+    - name: bad subset used
4
+      setup: gather_subset=nonsense
5
+      register: bad_sub
6
+      ignore_errors: true
7
+
8
+    - name: verify we fail the right way
9
+      assert:
10
+        that:
11
+          - bad_sub is failed
12
+          - "'MODULE FAILURE' not in bad_sub['msg']"