salt.resources.ssh#
SSH resource module — exposes remote Linux/Unix machines as Salt Resources using the salt-ssh Shell transport layer.
Each ssh resource maps to one remote host reachable via SSH. Because
resources share a single loader per type, a minion managing 500 SSH hosts
uses one loader rather than 500 proxy processes, each with its own key pair.
This module uses salt.client.ssh.shell.Shell for raw command
execution (cmd_run, ping) and salt.client.ssh.Single with
the salt-thin bundle for grain collection (grains.items), giving the same
complete, accurate grain set that salt-ssh provides.
Configuration (via Pillar; top-level key defaults to resources, overridable
with minion option resource_pillar_key):
resources:
ssh:
hosts:
web-01:
host: 192.168.1.10
user: root
priv: /etc/salt/ssh_keys/web-01
web-02:
host: 192.168.1.11
user: admin
passwd: secretpassword
no_host_keys: true
Per-host connection parameters:
hostHostname or IP address of the remote machine (required).
userSSH login user (default:
root).portSSH port (default:
22).privPath to the SSH private key file. Mutually exclusive with
passwdbut both may be specified; whenprivis set Salt uses key-based option strings even ifpasswdis also set.passwdSSH password. Prefer key-based authentication for production.
priv_passwdPassphrase protecting the private key.
sudoRun commands as root via sudo (default:
False).timeoutSSH connection timeout in seconds (default:
30).identities_onlyPass
-o IdentitiesOnly=yesto prevent the SSH agent from offering unrelated keys (default:False).no_host_keysDisable host key checking entirely — sets both
StrictHostKeyChecking=noandUserKnownHostsFile=/dev/null(default:False).ignore_host_keysPass
-o StrictHostKeyChecking=nowithout discarding the known-hosts database (default:False).known_hosts_filePath to a custom
known_hostsfile for this host.ssh_optionsList of additional
-o Key=Valueoptions passed verbatim to thesshbinary.keepaliveEnable TCP keepalives (default:
True).keepalive_intervalServerAliveIntervalin seconds (default: from Salt opts or60).keepalive_count_maxServerAliveCountMax(default: from Salt opts or3).
- salt.resources.ssh.cmd_run(cmd, timeout=None)#
Execute a shell command on the current SSH resource.
This is the primary building block for execution modules that target SSH resources — analogous to
__proxy__["ssh_sample.cmd"]()in the proxy model. Execution module overrides for thesshresource type delegate their work here.Returns a dict with keys:
stdout— standard output from the remote commandstderr— standard error from the remote commandretcode— exit code (0 on success)
- Parameters:
- Return type:
CLI Example (via resource execution module):
salt -C 'T@ssh:web-01' ssh_cmd.run 'uptime'
- salt.resources.ssh.discover(opts)#
Return the list of SSH resource IDs managed by this minion.
The list is the set of keys under
hostsfor thesshtype under the configured resource pillar subtree. Adding or removing a host from that Pillar key and runningsaltutil.refresh_resourcesupdates the Master's Resource Registry without any process restart.
- salt.resources.ssh.grains()#
Return full Salt grains for the current SSH resource.
Runs
grains.itemson the remote host via the salt-thin bundle (the same mechanism used bysalt-ssh), giving us the complete, accurate grain set rather than a hand-crafted subset.Results are cached in
__context__per resource ID. Callgrains_refresh()to force re-collection.- Return type:
- salt.resources.ssh.grains_refresh()#
Invalidate the grains cache for the current SSH resource and re-collect.
- Return type:
- salt.resources.ssh.init(opts)#
Initialize the
sshresource type for this minion.Called once when the resource type is loaded, before any per-resource operations are dispatched. Reads host configs from the
sshentry under the pillar subtree selected byresource_pillar_key(seesalt.utils.resources.pillar_resources_tree()), caches them in__context__["ssh_resource"], and pre-resolves the SSH binary version so that_shell_opts()never has to run a subprocess during a job.- Parameters:
opts (dict) -- The Salt opts dict.
- salt.resources.ssh.ping()#
Return
Trueif the current SSH resource is reachable via SSH.Runs
echo pingon the remote host. A zero exit code and the expected output indicate that the SSH connection is healthy.