yes, you can make it work with ssh forwarding
as long as the user that you become in the git clone is part of sudoers, so he doesn't need to use sudo to execute git
So, in addition to all the configs required for key forwarding, there is a trick that even mentioned in Ansible docs.
High level process is as follows:
enable agent forwarding in controlling machine
enable accepting agent key in target machine
create a user and add him (or her:) into sudoers group
use ansible's git module to clone the repo, become: your-sudoer-user
Also, to avoid any permissions denied on the host, just clone it into ~/something
You can always copy or symlink to anywhere you want
here is the link to where the playbook part to add user to sudoers is shown, it is basically a copy-paste: Ansible: create a user with sudo privileges
works like a charm
Also, make sure you add your SSH public key in the general settings of BitBucket, not in the per project. Otherwise your ssh key will only work on one specific repo. But if you add the ssh key in the bitbucket general settings, it will work on all your repos
below is the code that makes it work, the suduer user is "deployer"
# the tasks to CREATE A SUDOER GROUP
- name: Make sure we have a 'wheel' group
group:
name: wheel
state: present
become: yes
- name: Allow 'wheel' group to have passwordless sudo
lineinfile:
dest: /etc/sudoers
state: present
regexp: '^%wheel'
line: '%wheel ALL=(ALL) NOPASSWD: ALL'
validate: 'visudo -cf %s'
become: yes
- name: Add sudoers users to wheel group
user: name=deployer groups=wheel append=yes state=present createhome=yes
become: yes
# tasks to ADD REPO with Ansible's GIT MODULE
- name: Add Git Repo - BitBucket
git:
repo: 'git@bitbucket.org:<your_username>/<your_repo>.git'
dest: ~/code # note this destination, you will avoid permissions issues
accept_hostkey: yes # btw, this is for the ssh key forwarding
recursive: no
become: deployer # this guy (or gal) is a sudoer by now
# Extra "hack" to change permissions on files AND folders in one go, it has to do with the Capital X and what it applies to and what not. Also picked up from another stackoverflow
- name: Set perms on new Code repo to deployer:deployer dirs-0755 and files-0644
file:
path: ~/code
state: directory
owner: deployer
group: deployer
mode: u=rwX,g=rX,o=rX
recurse: yes
become: yes