As part of running a playbook I’m setting up an agent and add a key to it:
- name: Start ssh agent
hosts: localhost
gather_facts: no
vars_files:
- common_vars.yml
tasks:
- name: Start the SSH agent
shell: |
ssh-agent -s
register: ssh_agent_result
- name: Extract the SSH Agent PID
set_fact:
ssh_agent_socket: "{{ ssh_agent_result.stdout | regex_search('(/tmp/ssh-[^;]+/agent.[0-9]+)') }}"
- name: Add the SSH private key to the agent (using the SSH Agent PID)
shell: ssh-add /home/max/projects/kw-infra-simplified/terraform/do-compose-private-instance-key.pem
environment:
SSH_AUTH_SOCK: "{{ ssh_agent_socket }}"
And then I use this SSH_AUTH_SOCK
in the next play:
- name: Ping instances
hosts: apps
gather_facts: no
vars:
ssh_agent_pid: "{{ hostvars['localhost'].ssh_agent_pid }}"
ssh_agent_socket: "{{ hostvars['localhost'].ssh_agent_socket }}"
ansible_ssh_user: ubuntu
environment:
SSH_AUTH_SOCK: "{{ ssh_agent_socket }}"
tasks:
- name: Debug the extracted SSH Agent PID
debug:
msg: "SSH_AUTH_SOCK is {{ ssh_agent_socket }}"
- name: Ping the apps instances
ping:
Which works just fine and outputs the debug message "SSH_AUTH_SOCK is {{ ssh_agent_socket }}"
with the correct path to the ssh-agent socket. However, it can’t connect to the remote host and when inspecting the output from the ssh:
ansible-playbook -vvvvv ping.yaml
I can see that the ssh doesn’t attempt to use the agent. What could be the reason for this? Is what I’m trying to do is even possible?