Ssh Operations

Execute commands and up/download files from the remote host.

Eg: pyinfra -> inventory-host.net <-> another-host.net

Facts used in these operations: files.File, files.FindInFile, server.Home.

ssh.command

Stateless operation

This operation will always execute commands and is not idempotent.

Execute commands on other servers over SSH.

ssh.command(hostname: 'str', command: 'str', user: 'str | None' = None, port=22,
     **kwargs,
)
  • hostname: the hostname to connect to

  • command: the command to execute

  • user: connect with this user

  • port: connect to this port

Example:

ssh.command(
    name="Create file by running echo from host one to host two",
    hostname="two.example.com",
    command="echo 'one was here' > /tmp/one.txt",
    user="vagrant",
)

Note

This operation also inherits all global arguments.

ssh.download

Download files from other servers using scp.

ssh.download(
     hostname: 'str',
     filename: 'str',
     local_filename: 'str | None' = None,
     force=False,
     port=22,
     user: 'str | None' = None,
     ssh_keyscan=False,
     **kwargs,
)
  • hostname: hostname to upload to

  • filename: file to download

  • local_filename: where to download the file to (defaults to filename)

  • force: always download the file, even if present locally

  • port: connect to this port

  • user: connect with this user

  • ssh_keyscan: execute ssh.keyscan before uploading the file

Note

This operation also inherits all global arguments.

ssh.keyscan

Check/add hosts to the ~/.ssh/known_hosts file.

ssh.keyscan(hostname: 'str', force=False, port=22,
     **kwargs,
)
  • hostname: hostname that should have a key in known_hosts

  • force: if the key already exists, remove and rescan

Example:

from pyinfra.operations import ssh
ssh.keyscan(
    name="Set add server two to known_hosts on one",
    hostname="two.example.com",
)

Note

This operation also inherits all global arguments.

ssh.upload

Stateless operation

This operation will always execute commands and is not idempotent.

Upload files to other servers using scp.

ssh.upload(
     hostname: 'str',
     filename: 'str',
     remote_filename: 'str | None' = None,
     port=22,
     user: 'str | None' = None,
     use_remote_sudo=False,
     ssh_keyscan=False,
     **kwargs,
)
  • hostname: hostname to upload to

  • filename: file to upload

  • remote_filename: where to upload the file to (defaults to filename)

  • port: connect to this port

  • user: connect with this user

  • use_remote_sudo: upload to a temporary location and move using sudo

  • ssh_keyscan: execute ssh.keyscan before uploading the file

Note

This operation also inherits all global arguments.