Jun 28, 2021 3 min read

How to Clone a Git Repository with Ansible

Need to clone a Git repo on the remote node in your Ansible setup? Here's how to do that.
Table of Contents

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

To clone a git repo remotely using Ansible, you may add entries like this to your Playbook.

 - hosts: all
   - name: Clone a github repository
       repo: https://github.com/sqlite/sqlite.git
       dest: /home/debian/repos/
       clone: yes
       update: yes

Don't worry. I'll explain what those parameters mean and how to do it with a sample tutorial.

Cloning Git repositories with Ansible

I presume that you are already familiar with Ansible basics like inventory, playbooks etc. If not, you may follow our Ansible tutorial series.

Ansible Tutorials for RHCE EX294 Certification Exam
12 chapter Ansible tutorial series with hands-on examples cover all the necessary learnings to pass the RHCE Ex294 exam.


You must have Ansible installed 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.

If you choose to follow this tutorial, ensure you have:

  1. One control node and one remote host.
  2. SSH key pairs. The public key of control node must be available in the authorized_keys file in the remote hosts.
  3. A non-root user with sudo privileges on the remote hosts.
  4. Write access to a directory on the remote host to store the contents of the cloned repo.

Set up Ansible Inventory

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

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

Add the IP address of the remote host in this file:

vim /etc/ansible/hosts

It could look like this:

Save the file.

Cloning a Git Repository with Ansible playbook

Now that you have the inventory file configured and SSH keys in place to access the remote hosts from the control node, you can create the Ansible Playbook.

Using a text editor like Vim and create a YAML file.

vim clone.yaml

Edit the file and add the following entries.

 - hosts: all
   - name: Clone a github repository
       repo: https://github.com/sqlite/sqlite.git
       dest: /home/debian/repos/
       clone: yes
       update: yes

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

Next, you are using the git module to specify the link to the SQLite GitHub repository.

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

You 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 log in to the remote host to verify that the repository was cloned properly:

Verify cloned Git repo by Ansible

This should give you a decent idea about cloning a Git repository with Ansible. With this information, you should be able to use Git repos with Ansible based on your requirement.

If you have some questions or suggestions, please leave a comment below.

Join the conversation

Great! You’ve successfully signed up.
Welcome back! You've successfully signed in.
You've successfully subscribed to Linux Handbook.
Your link has expired.
Success! Check your email for magic link to sign-in.
Success! Your billing info has been updated.
Your billing was not updated.