Clone Public and Private Git Repositories with Ansible

Ansible is a potent automation tool that can be used for configuration management and application deployment, while Git is one of the most widely used version control systems currently in use.

We will examine multiple Ansible-based methods for cloning public and private git repository in this blog article.

Ansible is a potent tool for automating processes and managing infrastructure. It is notably helpful for automating repetitive processes and managing large-scale systems.

Managing code repositories is one of the most frequent chores in software development, and Git is one of the most widely used version control tools available today.

It is simple to manage code repositories across many servers by automating the cloning of git repositories using Ansible.

Various methods of cloning the Git Repository

  1. Using the Git Module
  2. Using the Shell Module
  3. Using the Command Module
  4. Cloning Private Git Repo

1. Using the Git Module

You may clone a Git repository using Ansible's integrated Git module. The simplest and clearest way to use Ansible to clone a Git repository is with this module.

Following relevant options must be provided in order to use the Git module -

  1. The repository's URL
  2. The destination path

Here is an example playbook that clones a repository using the Git module:

 1# Ansible tasks to clone public git repository
 2
 3---
 4- name: Clone Git repository
 5  hosts: webserver
 6  tasks:
 7    - name: Clone repository
 8      git:
 9        repo: https://github.com/example/repo.git
10        dest: /var/www/html/repo
11 

In this illustration, we've specified a playbook that will be executed on the host web server.

A single action from the playbook utilizes the Git module to copy the repository from the given URL to the /var/www/html/repo directory on the remote server.

2. Using the Shell Module

(Note - You must make sure that you have installed git onto remote server already)

Use Ansible's Shell module to clone a repository if you'd rather use a shell command. You can execute shell commands on distant servers with this module.

You must include the Shell module in your playbook and supply the required shell command in order to clone a Git repository using this module.

Here is an example playbook that clones a repository using the Shell module:

1# Ansible tasks to clone git repository using shell module
2
3---
4- name: Clone Git repository
5  hosts: webserver
6  tasks:
7    - name: Clone repository
8      shell: git clone https://github.com/example/repo.git /var/www/html/repo

In this illustration, we've specified a playbook that will be executed on the host web server.

A single action from the playbook utilizes the Shell module to execute the git clone command to copy the repository from the given URL to the /var/www/html/repo directory on the remote server.

3. Using the Command Module

(Note - You must make sure that you have installed git onto remote server already) Using the Command module in Ansible is another technique to copy a Git repository.

Using this module, you can issue commands to distant servers.

You must include the Command module in your playbook and supply the required command in order to clone a Git repository using this module.

Here is an illustration of a playbook that copies a repository using the Command module:

1---
2- name: Clone Git repository
3  hosts: webserver
4  tasks:
5    - name: Clone repository
6      command: git clone https://github.com/example/repo.git /var/www/html/repo 

In the above illustration, we've specified a playbook that will be executed on the host web server.

One job in the playbook utilizes the Command module to execute the git clone command, which copies the repository from the given URL to the /var/www/html/repo directory on the remote server.

4. Cloning Private Git Repo

Here is the Ansible task for Clone the Private GitHub Repository but you need to generate and setup SSH key (id_rsa).

1---
2## Clone the private repo on remote server
3- name: Clone a private Git repository
4  git:
5    repo: git@github.com:rahulwagh/jhooq.git
6    dest: /home/ubuntu/jhooq-repo
7    accept_hostkey: yes
8    key_file: /home/ubuntu/.ssh/id_rsa 

Here are the steps to generate the SSH key:

1. Generate an SSH key pair on remote server

First you need to create SSH Key pair on remote server. The SSH key pair contains -

  • Public Key - The public key will be added to your GitHub Account
  • Private Key - The private key will reside onto your remote server and will be used by ansible playbook for cloning the private Git Repository.

Use the following command to generate the SSH key pair -

1# Generate the ssh key pair 
2# Keys will be generated at - /home/ubuntu/.ssh/.id_rsa
3
4ssh-keygen 

Generate SSH key pair using ssh-keygen

2. Locate the Public RSA key

The default location would be - /home/ubuntu/.ssh/.id_rsa but it varies a lot based on the settings of your remote server.

Locate the SSH keys

3. Copy the content of id_rsa.pub key

Now copy the content of the id_rsa.pub because we need to copy this public key onto the GitHub account.

Copy the id_rsa.pub key

4. Adding id_rsa.pub key to GitHub account

  1. Goto GitHub account Settings

    Goto the GitHub Settings page

  2. Click on the SSH and GPG keys

    Click on the GitHub SSH GPG settings

  3. Click on the New SSH key

    Add new SSH key to GitHub

  4. Set a Title for the key and copy the content of the id_rsa.pub(public key)

    Save the SSH key in the GitHub

5. Ansible Playbook Run

Here is the successful playbook run which will clone the private GitHub Repository onto remote server.

Here is the anisble playbook run

Posts in this series