pyinfra.connectors.ssh module

Connect to hosts over SSH. This is the default connector and all targets default to this meaning you do not need to specify it - ie the following two commands are identical:

pyinfra my-host.net ...
pyinfra @ssh/my-host.net ...
class pyinfra.connectors.ssh.Meta

Bases: BaseConnectorMeta

class DataKeys

Bases: object

allow_agent = 'Allow using SSH agent'
config_file = 'Custom SSH config file'
connect_retries = 'Number of tries to connect via ssh'
connect_retry_max_delay = 'Upper bound for random delay between retries'
connect_retry_min_delay = 'Lower bound for random delay between retries'
forward_agent = 'Enable SSH forward agent'
hostname = 'SSH hostname'
key = 'Key file to use for authentication'
key_password = 'Key file password'
known_hosts_file = 'Custom SSH known hosts file'
look_for_keys = 'Allow looking up users keys'
paramiko_connect_kwargs = "Override keyword arguments passed into paramiko's `SSHClient.connect`"
password = 'Password to use for authentication'
port = 'SSH port'
strict_host_key_checking = 'Override strict host keys check setting'
user = 'User to SSH as'
handles_execution = True
keys_prefix = 'ssh'
pyinfra.connectors.ssh.check_can_rsync(host: Host)
pyinfra.connectors.ssh.connect(state: State, host: Host)

Connect to a single host. Returns the SSH client if successful. Stateless by design so can be run in parallel.

pyinfra.connectors.ssh.get_file(state: State, host: Host, remote_filename: str, filename_or_io, remote_temp_filename=None, sudo: bool = False, sudo_user=None, su_user=None, print_output: bool = False, print_input: bool = False, **command_kwargs)

Download a file from the remote host using SFTP. Supports download files with sudo by copying to a temporary directory with read permissions, downloading and then removing the copy.

pyinfra.connectors.ssh.make_names_data(hostname)
pyinfra.connectors.ssh.put_file(state: State, host: Host, filename_or_io, remote_filename, remote_temp_filename=None, sudo: bool = False, sudo_user=None, doas: bool = False, doas_user=None, su_user=None, print_output: bool = False, print_input: bool = False, **command_kwargs)

Upload file-ios to the specified host using SFTP. Supports uploading files with sudo by uploading to a temporary directory then moving & chowning.

pyinfra.connectors.ssh.rsync(state: State, host: Host, src: str, dest: str, flags, print_output: bool = False, print_input: bool = False, sudo: bool = False, sudo_user=None, **ignored_kwargs)
pyinfra.connectors.ssh.run_shell_command(state: State, host: Host, command, get_pty: bool = False, timeout=None, stdin=None, success_exit_codes=None, print_output: bool = False, print_input: bool = False, return_combined_output: bool = False, **command_kwargs)

Execute a command on the specified host.

Parameters
  • state (pyinfra.api.State obj) – state object for this command
  • hostname (string) – hostname of the target
  • command (string) – actual command to execute
  • sudo (boolean) – whether to wrap the command with sudo
  • sudo_user (string) – user to sudo to
  • get_pty (boolean) – whether to get a PTY before executing the command
  • env (dict) – environment variables to set
  • timeout (int) – timeout for this command to complete before erroring
Returns

(exit_code, stdout, stderr) stdout and stderr are both lists of strings from each buffer.

Return type

tuple