SSH

Native SSH integration in Polycrate CLI: list inventory.yml hosts, interactive shells, ad-hoc exec commands, port tunnels, file copy, and sshagentmount.

Polycrate integrates SSH natively based on your workspace inventory (inventory.yml) and the workspace SSH key (artifacts/secrets/id_rsa). You can connect to any managed host without juggling IP lists, ports, or -i key paths.

Prerequisites

Place inventory.yml in the workspace root. Polycrate evaluates both host-level variables and shared group variables under all.vars:

# inventory.yml
all:
  vars:
    ansible_user: deploy
    ansible_ssh_port: 22
  hosts:
    web-1:
      ansible_host: 192.0.2.10
    web-2:
      ansible_host: 192.0.2.11
    db-1:
      ansible_host: 192.0.2.20
      ansible_ssh_port: 2222

Overview of All SSH Commands

Command Purpose
polycrate ssh [hostname] Interactive SSH shell (without hostname: TUI picker)
polycrate ssh list Display all hosts from inventory.yml
polycrate ssh exec <host> <cmd...> Execute a single command non-interactively (propagates exit code)
polycrate ssh tunnel <host> <lp>:<rh>:<rp> Open local TCP port forwarding (-L)
polycrate ssh copy <source> <target> Transfer files or directories (-r)

Interactive Connection and Host List

# List available hosts
polycrate ssh list

# Connect directly
polycrate ssh web-1

# Interactive host picker (arrow keys + Enter)
polycrate ssh

# Retry automatically, e.g., after a server reboot
polycrate ssh web-1 --retry --retry-interval 3s --retry-timeout 2m

Ad-Hoc Commands (polycrate ssh exec)

# Single command
polycrate ssh exec web-1 systemctl status nginx

# Evaluate compound shell commands on the target host
polycrate ssh exec web-1 -- bash -c "df -h && free -m"

Port-Forwarding Tunnels (polycrate ssh tunnel)

# Expose PostgreSQL from db-1 locally on port 5432
polycrate ssh tunnel db-1 5432:localhost:5432

# Reach an internal service using web-1 as a jump host
polycrate ssh tunnel web-1 6379:10.0.0.5:6379

Press Ctrl+C to close the tunnel and immediately release the local port. The target server must have AllowTcpForwarding yes enabled in /etc/ssh/sshd_config.

Transferring Files (polycrate ssh copy)

# Download a file from a host
polycrate ssh copy web-1:/var/log/nginx/access.log ./

# Upload a file
polycrate ssh copy ./deploy.sh web-1:/tmp/

# Recursively copy a directory
polycrate ssh copy -r web-1:/etc/nginx/ ./nginx-backup/

SSH Key Source (config.sshkeysource)

By default, Polycrate uses the workspace-scoped key at artifacts/secrets/id_rsa (config.sshkeysource: workspace). If you prefer to use your personal SSH key instead, configure this in workspace.poly:

# workspace.poly
config:
  sshkeysource: user   # workspace (default) | user

Configure the corresponding local key path per user in ~/.polycrate/polycrate.yml:

# ~/.polycrate/polycrate.yml
ssh:
  user_key_path: "~/.ssh/id_ed25519"
  user_key_path_by_workspace:
    acme-production-1: "~/.ssh/acme_prod_ed25519"
    acme-staging-1: "~/.ssh/acme_staging_ed25519"

SSH Agent Mount in Containers (config.sshagentmount)

For security and portability reasons (e.g., macOS/OrbStack, CI environments), the host SSH agent socket (SSH_AUTH_SOCK) is not mounted into the container by default during polycrate run.

If a containerized action requires keys from your running host ssh-agent (such as hardware or FIDO keys), enable the mount explicitly:

  • Per invocation: polycrate run my-block install --ssh-agent-mount
  • Persistently in the workspace (workspace.poly):
# workspace.poly
config:
  sshagentmount: true

Direct SSH commands (polycrate ssh, exec, tunnel, copy) do not need a container mount — they pass SSH_AUTH_SOCK directly to the local SSH process.

Passphrase-Protected Keys and sudo (-K)

  • Passphrase-protected keys: Run with --ssh-use-passphrase (passphrase prompted interactively or read from an encrypted file at artifacts/secrets/ssh-passphrase.poly). Polycrate starts a temporary ssh-agent for this.
  • Ansible become / sudo password: Pass -K / --ask-become-pass to polycrate run:
polycrate run hardening install -K

The password is requested before the container starts, passed as ANSIBLE_BECOME_PASSWORD, and never stored in the snapshot.

Shell Autocompletion

Once shell completion is enabled (source <(polycrate completion zsh) or bash), polycrate ssh <TAB> automatically completes all hostnames from your inventory.yml.

See Also