@@ -198,14 +198,18 @@ def execute_command_with_sudo_retry(
198
198
) -> tuple [int , CommandOutput ]:
199
199
return_code , output = execute_command ()
200
200
201
+ # If we failed look for a sudo password prompt line and re-submit using the sudo password. Look
202
+ # at all lines here in case anything else gets printed, eg in:
203
+ # https://github.com/pyinfra-dev/pyinfra/issues/1292
201
204
if return_code != 0 and output and output .combined_lines :
202
- last_line = output .combined_lines [- 1 ].line
203
- if last_line .strip () == "sudo: a password is required" :
204
- # If we need a password, ask the user for it and attach to the host
205
- # internal connector data for use when executing future commands.
206
- sudo_password = getpass ("{0}sudo password: " .format (host .print_prefix ))
207
- host .connector_data ["prompted_sudo_password" ] = sudo_password
208
- return_code , output = execute_command ()
205
+ for line in reversed (output .combined_lines ):
206
+ if line .line .strip () == "sudo: a password is required" :
207
+ # If we need a password, ask the user for it and attach to the host
208
+ # internal connector data for use when executing future commands.
209
+ sudo_password = getpass ("{0}sudo password: " .format (host .print_prefix ))
210
+ host .connector_data ["prompted_sudo_password" ] = sudo_password
211
+ return_code , output = execute_command ()
212
+ break
209
213
210
214
return return_code , output
211
215
@@ -304,7 +308,7 @@ def make_unix_command(
304
308
_sudo = False ,
305
309
_sudo_user = None ,
306
310
_use_sudo_login = False ,
307
- _sudo_password = False ,
311
+ _sudo_password = "" ,
308
312
_sudo_askpass_path = None ,
309
313
_preserve_sudo_env = False ,
310
314
# Doas config
0 commit comments