Skip to content

Commit 5d53821

Browse files
Aap 41580 indirect host count wildcard query (ansible#15893)
* Support <collection_namespace>.<collection_name>.* indirect host query to match ANY module in the <collection_namespace>.<collection_name> * Add tests for new wildcard indirect host count * error checking of ansible event name * error checking of ansible event query
1 parent 39cd09c commit 5d53821

File tree

2 files changed

+244
-89
lines changed

2 files changed

+244
-89
lines changed

awx/main/tasks/host_indirect.py

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -45,22 +45,35 @@ def build_indirect_host_data(job: Job, job_event_queries: dict[str, dict[str, st
4545
facts_missing_logged = False
4646
unhashable_facts_logged = False
4747

48+
job_event_queries_fqcn = {}
49+
for query_k, query_v in job_event_queries.items():
50+
if len(parts := query_k.split('.')) != 3:
51+
logger.info(f"Skiping malformed query '{query_k}'. Expected to be of the form 'a.b.c'")
52+
continue
53+
if parts[2] != '*':
54+
continue
55+
job_event_queries_fqcn['.'.join(parts[0:2])] = query_v
56+
4857
for event in job.job_events.filter(event_data__isnull=False).iterator():
4958
if 'res' not in event.event_data:
5059
continue
5160

52-
if 'resolved_action' not in event.event_data or event.event_data['resolved_action'] not in job_event_queries.keys():
61+
if not (resolved_action := event.event_data.get('resolved_action', None)):
5362
continue
5463

55-
resolved_action = event.event_data['resolved_action']
64+
if len(resolved_action_parts := resolved_action.split('.')) != 3:
65+
logger.debug(f"Malformed invocation module name '{resolved_action}'. Expected to be of the form 'a.b.c'")
66+
continue
67+
68+
resolved_action_fqcn = '.'.join(resolved_action_parts[0:2])
5669

57-
# We expect a dict with a 'query' key for the resolved_action
58-
if 'query' not in job_event_queries[resolved_action]:
70+
# Match module invocation to collection queries
71+
# First match against fully qualified query names i.e. a.b.c
72+
# Then try and match against wildcard queries i.e. a.b.*
73+
if not (jq_str_for_event := job_event_queries.get(resolved_action, job_event_queries_fqcn.get(resolved_action_fqcn, {})).get('query')):
5974
continue
6075

6176
# Recall from cache, or process the jq expression, and loop over the jq results
62-
jq_str_for_event = job_event_queries[resolved_action]['query']
63-
6477
if jq_str_for_event not in compiled_jq_expressions:
6578
compiled_jq_expressions[resolved_action] = jq.compile(jq_str_for_event)
6679
compiled_jq = compiled_jq_expressions[resolved_action]

0 commit comments

Comments
 (0)