Ansible task - clone private git without SSH forwarding

Viewed 8615

I am trying to create an Ansible playbook which would be run from our dev team computers and from CI/CD servers.

One of the tasks in the playbook is to get the source code of our project from a private git repository. Because the playbook has to run from CI/CD servers we can not use SSH forwarding.

What i came up with is to copy necessary SSH private key to remote host machine and then using the key clone the code from the private git repository.

However when trying this, the cloning task hangs. When trying to launch the command manually it asks for a passphrase for the SSH private key. SSH key uses no passphrase (blank).

Could anyone share their solution of this (probably very common) problem?

In case anyone needs, this is my current playbook:

- name: Create SSH directory
  file: path=/root/.ssh state=directory

- name: Copy SHH key for Git access
  copy:
    content: "{{ git_ssh_key }}"
    dest: /root/.ssh/id_rsa
    owner: root
    group: root
    mode: 0600

# Also tried this, but it also hangs
#- name: Start SSH agent and add SSH key
#  shell: eval `ssh-agent -s` && ssh-add

- name: Get new source from GIT
  git: 
    key_file: /root/.ssh/id_rsa
    repo: "git@gitlab.com:user/repo.git"
    dest: "{{ staging_dir }}"
    depth: 1
    accept_hostkey: yes
    clone: yes

I am using ansible 2.3.1.0, python version = 2.7.12

2 Answers
Related