My usual ssh-agent holds a lot of keys. That is fine when I ssh to one host at a time. It is not fine when Ansible connects to about 90 hosts at once.
Every connection offered key after key. Some of those keys need confirmation before use, so I was clicking prompts. Some hosts gave up with Too many authentication failures. Some runs timed out. It was slow.
Yes, I should be running fewer keys here:
[15:24 dvl@pro05:~] % ssh-add -L | wc -l
7
That is a topic for another post.
The old workaround
Ansible runs as root in a jail I call ansible. My workaround was to log in without agent forwarding, then start a fresh agent in the jail holding only the Ansible key:
ssh -a ansible eval $(ssh-agent -s) ssh-add
That fixed Ansible, but it broke git push. That ssh-key is used only for ssh to the clients, and cannot be used for pushing to git. The Ansible configuration lives in a git checkout in that jail, and the key that pushes to the git server is on my laptop. With -a, that key never reached the jail.
I wanted both: Ansible using only its own key, and git push using only my git key.
The design: two small agents
The idea is two agents, each holding one key, each used by one thing:
| Ansible agent | git agent | |
|---|---|---|
| Runs on | the ansible jail | my laptop |
| Socket | ~/.ssh/agent/ansible.sock | ~/.ssh/git-agent.sock |
| Holds | the Ansible key | the git key |
| Used by | Ansible, via IdentityAgent | git push, via the forwarded SSH_AUTH_SOCK |
Two OpenSSH features make this work. Both take a socket path, not just yes/no (OpenSSH 8.2 and later):
- IdentityAgent tells ssh which agent to use, regardless of SSH_AUTH_SOCK.
- ForwardAgent can name a specific agent socket to forward, instead of whatever SSH_AUTH_SOCK points at.
The rule that keeps them apart: nothing ever exports SSH_AUTH_SOCK for the Ansible agent. Only ansible.cfg knows where it is.
The laptop side
On the laptop, a small script starts an agent holding only the git key, if one is not already running. It is safe to run any number of times:
#!/bin/sh
# Make sure the git-only ssh-agent is running and holds the git key.
# ~/.ssh/config forwards this socket (not the main agent) to the ansible jail,
# so the jail can git push without seeing any other keys.
SOCK="$HOME/.ssh/git-agent.sock"
KEY="$HOME/.ssh/id_ed25519"
umask 077
SSH_AUTH_SOCK="$SOCK" ssh-add -l >/dev/null 2>&1
case $? in
0) exit 0 ;; # agent running, key loaded
1) ;; # agent running, no keys yet
*) rm -f "$SOCK" # no agent, or a stale socket left by a reboot
ssh-agent -a "$SOCK" >/dev/null || exit 1 ;;
esac
SSH_AUTH_SOCK="$SOCK" ssh-add --apple-use-keychain "$KEY"
The trick is the exit status of ssh-add -l:
- 0 means keys are loaded
- 1 means the agent is running but empty
- 2 means there is no agent to talk to.
The laptop is a Mac, so --apple-use-keychain keeps the passphrase in the Keychain and I am not asked for it after a reboot.
In ~/.ssh/config, forward only that agent to the jail:
Host ansible
ForwardAgent ~/.ssh/git-agent.sock
I used to have alias ansible="ssh ansible". That became a zsh function, so the agent is always running before I connect:
function ansible { ~/bin/git-agent && command ssh ansible "$@" }
One zsh gotcha: if the alias is still defined when zsh reads ansible() { … }, the alias is expanded first and you get a function named ssh too. The function keyword form is not alias-expanded.
The jail side
In the jail, the same pattern, with the Ansible key and a different socket:
#!/bin/sh
# Make sure the ansible-only ssh-agent is running and holds the Ansible key.
# The agent stays in this jail: ansible.cfg points ssh at it with IdentityAgent,
# and SSH_AUTH_SOCK is left alone so git keeps using the forwarded laptop agent.
SOCK="$HOME/.ssh/agent/ansible.sock"
KEY="$HOME/.ssh/id_ed25519"
umask 077
mkdir -p "${SOCK%/*}" || exit 1
SSH_AUTH_SOCK="$SOCK" ssh-add -l >/dev/null 2>&1
case $? in
0) exit 0 ;;
1) ;;
*) rm -f "$SOCK"
ssh-agent -a "$SOCK" >/dev/null || exit 1 ;;
esac
SSH_AUTH_SOCK="$SOCK" ssh-add "$KEY"
It runs from root’s shell startup, only for interactive shells, because the key has a passphrase:
[ -t 0 ] && [ -x "$HOME/bin/ansible-agent" ] && "$HOME/bin/ansible-agent"
ssh-agent -a detaches, so the agent outlives my login. I type the passphrase once after the jail restarts, and never again until the next restart.
Both agents, seen from inside the jail:
# ssh-add -l 256 SHA256:... dvl@laptop (ED25519) # SSH_AUTH_SOCK=~/.ssh/agent/ansible.sock ssh-add -l 256 SHA256:... root@ansible (ED25519)
The first is the forwarded laptop agent: only the git key. The second is the jail agent: only the Ansible key.
Pointing Ansible at its agent
My first attempt at ansible.cfg was this:
[ssh_connection] ssh_args=-o ControlMaster=auto -o ControlPersist=60s -o IdentityAgent=~/.ssh/agent/ansible.sock -o IdentitiesOnly=yes -o ForwardAgent=no
Note that ssh_args replaces Ansible's defaults, so the ControlMaster options have to be repeated.
Every host failed:
# ansible all -m ping
[ERROR]: Task failed: Incorrect sudo password
...
foo | FAILED! => {
"changed": false,
"msg": "Task failed: Incorrect sudo password"
}
The ssh login worked. It was sudo that failed. My hosts authenticate sudo with pam_ssh_agent_auth, which asks the forwarded agent for a key it trusts. My old ansible.cfg had ForwardAgent=yes, which forwarded the agent holding the Ansible key. With ForwardAgent=no, there was nothing to ask. With ForwardAgent=yes, it would have forwarded my git agent, and the git key is not one sudo trusts.
The fix is to forward the Ansible agent itself:
[ssh_connection] ssh_args=-o ControlMaster=auto -o ControlPersist=60s -o IdentityAgent=~/.ssh/agent/ansible.sock -o IdentitiesOnly=yes -o ForwardAgent=~/.ssh/agent/ansible.sock
After that:
# ansible all -m ping
foo | SUCCESS => {
"changed": false,
"ping": "pong"
}
bar | SUCCESS => {
"changed": false,
"ping": "pong"
}
...
Every host, no prompts, no timeouts.
What I ended up with
- Ansible authenticates with one key, and forwards only that key, so sudo on the targets still works.
- git push from the jail uses my git key, and only that key reaches the jail.
- My main agent, with all its keys, never leaves the laptop.
One caveat: root in the jail (or on the jail host) can use any agent socket in it, including the forwarded git agent while I am logged in. File permissions cannot stop root. That was already true with full forwarding; now it is one key instead of all of them.