13
13
14
14
from osism import utils
15
15
16
+ # Regex pattern for extracting hosts from Ansible output
17
+ HOST_PATTERN = re .compile (r"^(ok|changed|failed|skipping|unreachable):\s+\[([^\]]+)\]" )
18
+
16
19
17
20
class Config :
18
21
broker_connection_retry_on_startup = True
@@ -106,14 +109,18 @@ def log_play_execution(
106
109
# Get runtime version from YAML version file
107
110
runtime_version = get_container_version (worker )
108
111
112
+ # Use provided hosts or empty list
113
+ if hosts is None :
114
+ hosts = []
115
+
109
116
execution_record = {
110
117
"timestamp" : datetime .now (timezone .utc ).isoformat ().replace ("+00:00" , "Z" ),
111
118
"request_id" : request_id ,
112
119
"worker" : worker ,
113
120
"worker_version" : runtime_version ,
114
121
"environment" : environment ,
115
122
"role" : role ,
116
- "hosts" : hosts if isinstance ( hosts , list ) else [] ,
123
+ "hosts" : hosts ,
117
124
"arguments" : arguments if arguments else "" ,
118
125
"result" : result ,
119
126
}
@@ -145,6 +152,7 @@ def run_ansible_in_environment(
145
152
auto_release_time = 3600 ,
146
153
):
147
154
result = ""
155
+ extracted_hosts = set () # Local set for host deduplication
148
156
149
157
if type (arguments ) == list :
150
158
joined_arguments = " " .join (arguments )
@@ -183,7 +191,7 @@ def run_ansible_in_environment(
183
191
worker = worker ,
184
192
environment = environment ,
185
193
role = role ,
186
- hosts = None , # Host extraction would require inventory parsing
194
+ hosts = None , # Hosts will be empty at start, filled at completion
187
195
arguments = joined_arguments ,
188
196
result = "started" ,
189
197
)
@@ -272,6 +280,13 @@ def run_ansible_in_environment(
272
280
273
281
while p .poll () is None :
274
282
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
+
275
290
if publish :
276
291
utils .push_task_output (request_id , line )
277
292
result += line
@@ -284,7 +299,7 @@ def run_ansible_in_environment(
284
299
worker = worker ,
285
300
environment = environment ,
286
301
role = role ,
287
- hosts = None , # Host extraction would require inventory parsing
302
+ hosts = sorted ( list ( extracted_hosts )) , # Direct pass of extracted hosts
288
303
arguments = joined_arguments ,
289
304
result = "success" if rc == 0 else "failure" ,
290
305
)
0 commit comments