Skip to content

Instantly share code, notes, and snippets.

@makmanalp
Last active June 18, 2021 15:06
Show Gist options
  • Star 8 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save makmanalp/a95aa39f4b3171baeb5b to your computer and use it in GitHub Desktop.
Save makmanalp/a95aa39f4b3171baeb5b to your computer and use it in GitHub Desktop.
SSH agent forwarding tester for ansible / vagrant
---
- hosts: all
sudo: no
tasks:
- shell: echo "Client= [$SSH_CLIENT] Sock= [$SSH_AUTH_SOCK]"
register: myecho
- debug: msg="{{myecho.stdout}}"
- shell: ssh-add -l
register: myecho
- debug: msg="{{myecho.stdout}}"
- shell: ssh -T -vvvv git@github.com
register: myecho
- debug: msg="{{myecho.stdout}}"
@makmanalp
Copy link
Author

Pretty handy if you want to set up ssh agent forwarding and you need to see if it worked. With vagrant, you can do:

  config.vm.provision "ansible" do |ansible|
      ansible.playbook = "sshtest.yml"
      ansible.verbose = "v"
      ansible.raw_ssh_args = ['-o ForwardAgent=yes']
  end

@makmanalp
Copy link
Author

More info about sudo and how it affects ssh agent forwarding: ansible/ansible#7235

@makmanalp
Copy link
Author

  • See if $SSH_AUTH_SOCK looks empty. If it is, that means it's not getting set up.
  • Try setting sudo=yes to see if that works if you run your playbook with sudo - you may need to pass sudo_flags=-HE If so, the above link to the github issue helps.
  • OSX is sometimes dumb and won't set up agent forwarding even when you see a key as added, in which case you may have to do ssh-add -K.

@smowden
Copy link

smowden commented Jun 1, 2015

ssh-add -k (it's lower case k by the way) solved all my problems, thanks for that @makmanalp!

@pun-ky
Copy link

pun-ky commented Oct 28, 2020

@smowdeb yep helped me too. same symptoms.

OSX is sometimes dumb and won't set up agent forwarding even when you see a key as added, in which case you may have to do ssh-add -K.

@mhitza
Copy link

mhitza commented Jun 18, 2021

Still didn't work for me, after setting up the -HE flag and become: false for the task that required the agent.

For me setting an explicit --ssh-extra-args '-o UserKnownHostsFile=/dev/null' for the ansible-playbook invocation solved the issue. Thanks to this SO answer https://stackoverflow.com/a/23704069

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment