1.9 KiB
EX294 3.2: SSH keys for managed nodes
Make a key pair for the user who runs Ansible, put its public key on every managed node with a playbook, and trust the nodes' host keys properly instead of turning host key checking off.
Before you start
This VM is the control node. The lab creates the user greg (password
Lab-Pass-1), who has no SSH key yet, and installs ansible-core and
sshpass. Work as greg, in /home/greg/ansible: its ansible.cfg reads
the inventory inventory, logs in as devops, becomes root, and has
host_key_checking = false.
The managed nodes node1 (10.99.1.11) and node2 (10.99.1.12) are
AlmaLinux 10 containers reached over SSH by name. On each, devops has the
password Lab-Pass-1 and passwordless sudo, but no authorized keys; root
and ansible have the same password. For a root shell on a node,
sudo podman exec -it node1 bash.
Tasks
- As greg, create an Ed25519 key pair with no passphrase in the default
place,
~/.ssh/id_ed25519, mode 600. - Write
/home/greg/ansible/keys.yml, a playbook that installs greg's public key fordevopson every managed node, and run it (with-kthe first time). Thenssh devops@node1 truedoesn't ask for a password, and a second run ofkeys.ymlreportschanged=0. - Remove
host_key_checking = falsefromansible.cfg, delete greg's~/.ssh/known_hosts(SSH filled it in while checking was off), and put the nodes' host keys there withssh-keyscaninstead, so thatansible all -m pingworks with no prompt.
Hints
ssh-keygen -t ed25519 -N '' -f ~/.ssh/id_ed25519.ansible.posix.authorized_keyneedsansible-galaxy collection install ansible.posix;ansible.builtin.fileandlineinfiledo the same job.lookup('ansible.builtin.file', '/home/greg/.ssh/id_ed25519.pub')reads the key on the control node.ssh-keyscan node1 node2 >> ~/.ssh/known_hosts; the names inknown_hostsmust be the ones in the inventory.