ssh port forwarding with Ansible

A short article, a reminder in fact.

I had to use Ansible to deploy an application on a machine (say application-machine) where port forwarding is needed to access a GitLab instance which is not accessible from application-machine. Manually I would connect to application-machine with the following ssh command:

ssh myuser@application-machine -R2222:`GitLab`-instance-ip:22

Then the content of the ssh configuration on application-machine should have those settings for the GitLab-instance:

Host `GitLab`
    Hostname 127.0.0.1
    Port 2222

It's now possible to git clone a project using:

git clone git:gitlab:path_to_project.git

In order to make it possible with Ansible the port forwarding option need to be used when Ansible connect using ssh. This can be done using the inventory:

[application]
machine-application

[application:vars]
ansible_ssh_extra_args = "-R2222:`GitLab`-instance-ip:22"

Now I can use the Ansible module git on application-machine to clone the project.