1313
1414from osism import utils
1515
16+ # Regex pattern for extracting hosts from Ansible output
17+ HOST_PATTERN = re .compile (r"^(ok|changed|failed|skipping|unreachable):\s+\[([^\]]+)\]" )
18+
1619
1720class Config :
1821 broker_connection_retry_on_startup = True
@@ -106,14 +109,18 @@ def log_play_execution(
106109 # Get runtime version from YAML version file
107110 runtime_version = get_container_version (worker )
108111
112+ # Use provided hosts or empty list
113+ if hosts is None :
114+ hosts = []
115+
109116 execution_record = {
110117 "timestamp" : datetime .now (timezone .utc ).isoformat ().replace ("+00:00" , "Z" ),
111118 "request_id" : request_id ,
112119 "worker" : worker ,
113120 "worker_version" : runtime_version ,
114121 "environment" : environment ,
115122 "role" : role ,
116- "hosts" : hosts if isinstance ( hosts , list ) else [] ,
123+ "hosts" : hosts ,
117124 "arguments" : arguments if arguments else "" ,
118125 "result" : result ,
119126 }
@@ -145,6 +152,7 @@ def run_ansible_in_environment(
145152 auto_release_time = 3600 ,
146153):
147154 result = ""
155+ extracted_hosts = set () # Local set for host deduplication
148156
149157 if type (arguments ) == list :
150158 joined_arguments = " " .join (arguments )
@@ -183,7 +191,7 @@ def run_ansible_in_environment(
183191 worker = worker ,
184192 environment = environment ,
185193 role = role ,
186- hosts = None , # Host extraction would require inventory parsing
194+ hosts = None , # Hosts will be empty at start, filled at completion
187195 arguments = joined_arguments ,
188196 result = "started" ,
189197 )
@@ -272,6 +280,13 @@ def run_ansible_in_environment(
272280
273281 while p .poll () is None :
274282 line = p .stdout .readline ().decode ("utf-8" )
283+
284+ # Extract hosts from Ansible output
285+ match = HOST_PATTERN .match (line .strip ())
286+ if match :
287+ hostname = match .group (2 )
288+ extracted_hosts .add (hostname ) # Local set (automatic deduplication)
289+
275290 if publish :
276291 utils .push_task_output (request_id , line )
277292 result += line
@@ -284,7 +299,7 @@ def run_ansible_in_environment(
284299 worker = worker ,
285300 environment = environment ,
286301 role = role ,
287- hosts = None , # Host extraction would require inventory parsing
302+ hosts = sorted ( list ( extracted_hosts )) , # Direct pass of extracted hosts
288303 arguments = joined_arguments ,
289304 result = "success" if rc == 0 else "failure" ,
290305 )
0 commit comments