From 45d3cf8db1b3c994515fe8a28ec2cdd18d440ff4 Mon Sep 17 00:00:00 2001 From: Philippe SWARTVAGHER Date: Sat, 29 Nov 2025 21:09:19 +0100 Subject: [PATCH] cli: add option --same-sudo-password To tell all hosts in the inventory uses the same password, which will be prompted only once. --- src/pyinfra_cli/cli.py | 11 +++++++++++ tests/test_cli/test_cli.py | 1 + 2 files changed, 12 insertions(+) diff --git a/src/pyinfra_cli/cli.py b/src/pyinfra_cli/cli.py index f7bed397d..8b24f335d 100644 --- a/src/pyinfra_cli/cli.py +++ b/src/pyinfra_cli/cli.py @@ -2,6 +2,7 @@ import sys import warnings from fnmatch import fnmatch +from getpass import getpass from os import chdir as os_chdir, getcwd, path from typing import Iterable, List, Tuple, Union @@ -82,6 +83,12 @@ def _print_support(ctx, param, value): envvar="PYINFRA_YES", show_envvar=True, ) +@click.option( + "--same-sudo-password", + is_flag=True, + default=False, + help="All hosts have the same sudo password, so ask only once.", +) @click.option( "--limit", help="Restrict the target hosts by name and group name.", @@ -274,6 +281,7 @@ def _main( ssh_key, ssh_key_password: str, ssh_password: str, + same_sudo_password: bool, shell_executable, sudo: bool, sudo_user: str, @@ -344,6 +352,9 @@ def _main( ssh_password, ) + if same_sudo_password: + config.SUDO_PASSWORD = getpass("sudo password: ") + if yes is False: _set_fail_prompts(state, config) diff --git a/tests/test_cli/test_cli.py b/tests/test_cli/test_cli.py index f93a1d765..efdf4174a 100644 --- a/tests/test_cli/test_cli.py +++ b/tests/test_cli/test_cli.py @@ -170,6 +170,7 @@ def test_deploy_operation_direct(self): ssh_key=None, ssh_key_password=None, ssh_password=None, + same_sudo_password=False, sudo=False, sudo_user=None, use_sudo_password=False,