How to clone a git repository with Ansible

Learn how you can use the Ansible automation tool to clone a git repository.
Captain Salem 3 min read
How to clone a git repository with Ansible


Ansible is an open-source configuration management and automation tool that allows for setting up and managing remote servers from a single device.

When configuring remote servers, you will encounter instances where you need to get files from a Git repository. These could be a software package from public repositories or configuration files on a private repo.

This tutorial will discuss how to use Ansible to clone a git repo remotely using Ansible playbooks.


To use Ansible, you must install it on your local machine. This Ansible instance acts as the control node for all remote hosts. Using the control node, you can create playbooks and tasks to execute on the specified remote machines.

To ensure you follow this tutorial correctly, ensure you have:

  1. SSH keys pairs. The public key must be available in the authorized_keys file in your remote hosts.
  2. A non-root user with sudo privileges.
  3. Write access to a directory to store your repo.

Setting up Ansible Inventory

Before proceeding further, we need to set up the Ansible inventory. The ansible inventory is a file that contains information about the remote servers we wish to automate with Ansible.

By default, the file is located in /etc/ansible/hosts. Create this file manually if it does not exit.

Using your favorite text editor, add the IP addresses of the remote hosts:

vim /etc/ansible/hosts

An example entry in the ansible inventory file is as:

The above inventory contains the IP addresses of two remote hosts which we will manage using Ansible.

Feel free to change the IP addresses to your needs.

Save the file, and we can proceed.

Setup SSH keys.

Next, we need to set up SSH key pairs and copy them to the remote hosts. This allows Ansible to login to the server without username and password.

To do this, use the ssh-keygen command.


The command above will interactively prompt you to create a public and private key.

The output is as:

 Generating public/private rsa key pair.
 Enter file in which to save the key (/home/centos/.ssh/id_rsa): 
 Enter passphrase (empty for no passphrase): 
 Enter same passphrase again: 
 Your identification has been saved in /home/centos/.ssh/id_rsa.
 Your public key has been saved in /home/centos/.ssh/
 The key fingerprint is:
 SHA256:nZIDW0iv688A4ZuFXsAtYkQlEbT4SvOlxb6i6a3/bso centos@centos8.linuxvmimages.local
 The key's randomart image is:
 +---[RSA 3072]----+
 | oB+. .     |
 | o + o o     |
 |. + = + o    |
 | o o.= = o .   |
 | o. ++= S o   |
 |..o.=* . o    |
 |. o+.o     |
 | +. o.o     |
 |.=+E*+..o    |

Once the key pairs are generated, you can upload them to the remote server.

Using the ssh-copy-id command, enter:

ssh-copy-id -i ~/.ssh/ debian@

NOTE: Replace the username and IP address with your credentials.

Once you execute the command above, you will be prompted for the SSH password for the specified user.

/usr/bin/ssh-copy-id: INFO: Source of key(s) to be installed: ""
/usr/bin/ssh-copy-id: INFO: attempting to log in with the new key(s), to filter out any that are already installed
/usr/bin/ssh-copy-id: INFO: 1 key(s) remain to be installed -- if you are prompted now it is to install the new keys
Welcome to Debian 10
debian@'s password:  
Number of key(s) added: 1

Now try logging into the machine, with:  "ssh 'debian@'"
and check to make sure that only the key(s) you wanted were added.

Clone a Git Repository

Now that you have the inventory file configured and the SSH keys ready, we can clone a git repo.

To do this, we need to create an Ansible playbook.

Using your text editor, create a YAML file

vim clone.yaml

Edit the file and add the following entries.

 - hosts: all
   - name: Clone a github repository
       dest: /home/debian/repos/
       clone: yes
       update: yes

In the playbook above, we started by defining a new task and gave it the name “Clone a GitHub repository.

Next, use the git module and specify the link to the GitHub repository.

We then proceed to define the destination for the repository. This is a local directory in the remote machine.

We set the attribute clone to yes to clone the repository and update it using the update attribute.

To run the playbook, use the command:

ansible-playbook clone.yaml

If the playbook fails due to SSH authentication, you can specify the username using the -u flag as:

ansible-playbook -u debian clone.yaml

Once the tasks have executed, you should have the repository cloned in the specified directory.

You can login to the remote host to verify it exist.


This tutorial discusses how to set up Ansible on your machine and use it to clone a git repository.

If you enjoy our content, please consider buying us a coffee to support our work:

More from GeekBits

Join us at GeekBits

Join our members and get a currated list of awesome articles each month.

Great! You’ve successfully signed up.

Welcome back! You've successfully signed in.

You've successfully subscribed to GeekBits.

Success! Check your email for magic link to sign-in.

Success! Your billing info has been updated.

Your billing was not updated.