Python SSH Keys and Host Inventory
An unattended script needs a key with no passphrase, which sounds careless until you see what authorized_keys can restrict it to - a key that can run one command and nothing else. Then the other half: one inventory file that says what exists, so adding a host is one line rather than an edit to every script. Including the ssh_config mistake I made while writing this, which silently discards every specific setting.
Remote Hosts and CI/CD Guide 32 of 39 Intermediate
- Control nodeUbuntu 26.04 LTS
- Managed hostsRHEL 10.0
- Python3.14.4
- requests2.34.2
- TimeAbout 20 min
restrict in authorized_keys needs OpenSSH 7.2 or later and is the sensible default - it turns off everything and lets you add back what you need. paramiko.SSHConfig.from_path needs paramiko 2.7.
- paramiko5.0.0
- pytest9.1.1
- PyYAML6.0.3
- boto3 / botocore1.43.78
| Server Name | IP Address | OS | Roles | CPU | RAM | HDD |
|---|---|---|---|---|---|---|
| RUNNER01 | 192.168.0.27 | Ubuntu 26.04 LTS | Control node - every script in this path runs here | 2 Core | 4 GB | 50 GB |
| RHCSA-A01 | 192.168.0.31 | RHEL 10.0 | Managed host - reached over SSH from the control node | 2 Core | 4 GB | 50 GB |
| RHCSA-B01 | 192.168.0.33 | RHEL 10.0 | Second managed host - so an inventory has more than one row | 2 Core | 4 GB | 50 GB |
This guide includes
Use this before any unattended script logs into anything. This matters because a passphrase-less key sounds careless until you restrict what it may do - the same key with and without that restriction is two very different risks.
- making a key for one job, and checking the two files and their modes
- restricting what a key is allowed to do, then seeing the same key unrestricted
- writing an inventory with groups, defaults and overrides, and catching one that is wrong
- reading the ssh config file paramiko can share, including the trap in it
- loading a key with a passphrase or leaving it with an agent, then driving a real loop from the inventory
Before you start
- guide 31 - the connection this configures.
- guide 13 - the inventory format.
- guide 25 - a credential is a credential.
-
A key made for one job
-
The key on disk
-
What a key is allowed to do
-
And the same key with no restriction
-
An inventory, in one file
-
An inventory that is wrong
-
The config file ssh already reads, and the trap in it
-
And using it to connect
-
A key with a passphrase
-
Or let an agent hold it
-
The inventory driving a real loop
-
And a service that is not there