I have an Ansible task that checks out a git repo with SSH key forwarding, and it works. But with two problems that I'd like to fix.
First a quick recap of my configuration:
ansible_ssh_common_args: '-o ForwardAgent=yes -o PreferredAuthentications=publickey'in my inventoryForwardAgent yesin.ssh/configon the client- the
ssh-agenton the client properly loaded with all the relevant keys
(edited to add this point that I at first forgotten to mention)
- the configuration suggested here: SSH Agent Forwarding with Ansible
- name: Fix sudoers for git clone
tags: user
when: is_linux
lineinfile:
path: /etc/sudoers
state: present
regexp: '.*SSH_AUTH_SOCK$'
line: 'Defaults env_keep += "SSH_AUTH_SOCK"'
validate: '/usr/sbin/visudo -cf %s'
(end edit)
The task is straightforward:
- name: Clone project repositories
tags: repos
# become: true -- this breaks key forwarding
# become_user: fritz
git:
repo: 'git@bitbucket.org:{{ item.user }}/{{ item.name }}'
dest: '{{ workareas }}/{{ item.folder }}/{{ item.name }}'
accept_hostkey: yes
force: no
with_items: '{{ repositories }}'
First problem: it does work, but when I add become/become_user key forwarding is no more enabled: the keys installed on the bastion host are tried (and of course this fails).
It is not a big issue for me: without become Ansible creates the repo with the wrong ownership, but I fix it in a later task. It's more a thing of understanding how Ansible works.
The second problem is more important. For a number of reasons I have a high number of keys on my client: without a specific configuration the ssh client submits them in an unpredictable order and usually I get the server aborting with 'too many authentication failures'. Putting into .ssh/config the specific associations of keys to hosts fixes the problem:
Host bitbucket.org
IdentityFile ~/.ssh/fritz@Mistral-bitbucket
But this only works when running git directly from the client; with Ansible it doesn't work, it still tries all the keys. At the moment I'm lucky and the proper key is submitted early, but there's no warranty it will be always like this.
Basically the question is: is it possible to have key forwarding and the .ssh/config file enforced also on the bastion host?
Thanks.