Manage Repositories via the GIT Ansible Module

Published:24 December 2021 - 7 min. read

A version control system is crucial in working with web-based repositories, and Git is the most popular one in the world today. But no one wants to get stuck in managing repositories manually all day. So how do you automate managing repositories? Before scouring the internet for answers, why not try the Git Ansible module?

Today’s sponsor is Tower, a beautiful, native Git client designed to make Git easy (and actually enjoyable!) to use. Tower is available for both Mac and Windows, and this week, you can get it at a very special price!

Ansible Git module allows you to work with Git repositories efficiently. And in this tutorial, you’ll learn how the Git Ansible module lets you clone or perform checkouts in different folders for your GitHub repositories.

Prerequisites

This tutorial comprises step-by-step instructions. If you’d like to follow along, be sure you have the following in place:

  • A remote Linux computer to test out the Git Ansible module. This tutorial uses Ubuntu 20.04.3 LTS as the remote node.
  • A GitHub account with one private and one public repository created in GitHub containing a hello.py Python file.
  • SSH key generated on the Ansible Node. The private key will remain on the Ansible remote node, but you’ll add the public key on GitHub for authentication.
  • An inventory file set up and one or more hosts already configured to run Ansible command and playbooks on. The remote Linux computer is called myserver, and this tutorial uses an inventory group called web.
  • Python v3.6 or later installed on your Ansible controller host and the remote node machine. This tutorial will be using Python v3.8.10 on a ubuntu machine.

Cloning Git Repository Using Ansible Ad Hoc Commands

Perhaps you’re looking into quickly cloning one repository on a remote host. If so, running ad hoc commands will suffice. Kick off this tutorial using the Git Ansible module to clone a repository with ad-hoc commands. Ad hoc commands are a quick and efficient way to run a single command on a remote host.

Log onto your Ansible controller and run the below ansible command to connect (-m ansible.builtin.git) to the host (web).

The command passes an argument (-a) that tells Ansible to clone (clone=yes) the content from the repository (repo) to a destination (dest=/tmp/clone_test) on the remote host.

From this point throughout the tutorial, make sure to replace the repository URL https://github.com/ShankyTheBlogger/ATA-testing.git in each code with your own.

ansible web -m ansible.builtin.git -a "repo=https://github.com/ShankyTheBlogger/ATA-testing.git clone=yes dest=/tmp/clone_test"

Once the command completes, you’ll see a CHANGED message, as shown below, that confirms Ansible successfully cloned the repository on the remote host.

Cloning the Git repository on the remote host inside the tmp directory folder.
Cloning the Git repository on the remote host inside the tmp directory folder.

Cloning Public and Private Git Repositories with Ansible Playbook

Running Git commands on the Git repository may be okay with an ad-hoc command. Still, executing multiple commands on a Git repository can be challenging. Instead of using ad-hoc commands, take advantage of the Ansible Git module with a playbook using the ansible-playbook command.

Assuming you’re already logged into the Ansible controller host:

1. Run the following commands to create a directory called ~/ansible_git_module. This directory will contain the playbook and all the required configuration files that you’ll use to invoke the Ansible Git module.

mkdir ~/ansible_git_module
cd ~/ansible_git_module

2. Next, open your favorite text editor, create a file called main.yml in the ~/ansible_git_module directory, and copy/paste the following YAML playbook contents.

The playbook below performs various tasks with the Git repository, such as cloning the public and private repository and confirming if the Git repository has been successfully cloned.

---
- name: Ansible GIT module demo
  hosts: web
# Defining the remote server where the ansible git  module will manage the objects
  remote_user: ubuntu # Using Remote user as ubuntu
  tasks:

# Clones the public repository in the clone folder inside the opt directory.
    - name: Clone a PUBLIC repo
      ansible.builtin.git:
        repo: https://github.com/ShankyTheBlogger/ATA-testing.git
        dest: /opt/clone
        separate_git_dir: /opt/clone.git
        clone: yes
        update: yes

# Clones the private repository in the private_clone folder inside the *opt* directory.
    - name: Clone a PRIVATE repo
      ansible.builtin.git:
        repo: [email protected]:ShankyTheBlogger/Privaterepo.git
        dest: /opt/private_clone
        clone: yes
        #    accept_hostkey: no
        key_file: /home/ubuntu/.ssh/id_rsa
        version: main

# Confirms if the public repository was successfully cloned in the specified folder.
    - name: Confirming if the Git repository has been succesfully cloned locally
      ansible.builtin.git:
         repo: https://github.com/ShankyTheBlogger/ATA-testing.git
         dest: /opt/clone
         clone: no
         update: no

    - name: Clone a repo with separate git directory Git to separate from current working tree.
      ansible.builtin.git:
        repo: https://github.com/ShankyTheBlogger/ATA-testing.git
        dest: /opt/different-directory.
        separate_git_dir: /opt/different-directory.git

3. Run the command below to invoke the playbook (main.yml). The playbook then executes the tasks (to clone, checkout repositories, and so on) on the remote node.

ansible-playbook main.yml

Below, you can see that some tasks show a changed status, which indicates the remote host wasn’t in the proper state and was modified to run the command. In contrast, the tasks that show an ok status don’t require any changes.

Running the Ansible playbook to perform Git commands on Github by using ansible-playbook
Running the Ansible playbook to perform Git commands on Github by using ansible-playbook

4. Now run each command below to list all files in the opt directory. This command lets you verify if you’ve properly cloned the Git repositories and made a checkout in the specified folders defined in the main.yml playbook.

cd /opt
ls -lh

After running the commands, you’ll see an output similar to the one below. The output confirms the successful cloned Git repositories within the specified opt directory on the remote node.

Verifying cloned repositories
Verifying cloned repositories

Cloning a Different Branch Using SSH Git URL

Cloning Git repository using HTTP GitHub URL works fine, but is it secure? There is a way to connect your machine with GitHub and clone the repository securely, which is by using SSH Git URL.

Using the same Ansible Controller host, create the playbook named diff_branch.yaml and copy/paste the code below. The below playbook clones branch2 from the specified repository ([email protected]:ShankyTheBlogger/ATA-testing.git) as defined inside the version.

---
- name: Ansible GIT module demo
  hosts: web
# Defining the remote server where the ansible git  module will manage the objects
  remote_user: ubuntu   # Using Remote user as ubuntu
  tasks:

    - name: Clone branch2 from ATA-tetsing repository
      ansible.builtin.git:
        repo: [email protected]:ShankyTheBlogger/ATA-testing.git
        dest: /tmp/branch2-ata-testing
        clone: yes
        version: branch2

Now execute the playbook, which clones the branch2 branch.

ansibile-playbook diff_branch.yaml
Cloning branch2 from ATA-testing repository using Ansible playbook
Cloning branch2 from ATA-testing repository using Ansible playbook

Switching Branches with Git Checkout in Ansible Playbook

Having one branch if you’re the only one working on a small project is all good. But what if you’re working with other developers? You’ll need a way to switch between branches. Ansible Git module together with Git checkout is the solution!

Git checkout is to check out the desired status of your repositories, such as branches or particular files.

Create the Ansible playbook named checkout.yaml and copy/paste the code below. The below playbook will checkout the repository in the opt directory.

---
- name: Ansible checkout demo
  hosts: web
  remote_user: ubuntu   # Using Remote user as ubuntu
  tasks:  

# Checkouts the repository in the opt directory 
    - name: Git checkout
      ansible.builtin.git:
        repo: 'https://github.com/ShankyTheBlogger/ATA-testing.git'
        dest: /opt/checkout

# Confirms if the repository is checked out successfully in the opt directory 
    - name: Confirming if the Git repository has been succesfully checkout
      ansible.builtin.git:
        repo: https://github.com/ShankyTheBlogger/ATA-testing.git
        dest: /opt/checkout
        update: no

Now run the ls command below to list all files inside the opt directory.

ls -lh

As you can see below, the repository is checked out successfully in the opt directory.

Confirms if the repository is checked out successfully in the opt directory in the remote node
Confirms if the repository is checked out successfully in the opt directory in the remote node

Archiving a GitHub Repository

Perhaps you prefer to make the repository read-only to everyone. If so, archiving a repository is a good solution. But bear in mind that you can also unarchive your repository anytime. Archiving a repository saves all your repository’s resources to a ZIP file in a specified location.

Before archiving your repository, make sure you’ve changed its settings and consider closing all open issues and pull requests.

1. Create the Ansible playbook named archive.yaml and copy/paste the code below. The below playbook archives the specified repository to the tmp/archive directory.

---
- name: Ansible checkout demo
  hosts: web
  remote_user: ubuntu   # Using Remote user as ubuntu
  tasks:     

    - name: Create git archive from repository containing the tree structure for the source.
      ansible.builtin.git:
        repo: https://github.com/ShankyTheBlogger/ATA-testing.git
        dest: /tmp/archive
        archive: /tmp/archive/archive.zip

2. Now execute the playbook (archive.yaml), which archives a specific repository. ansible-playbook archive.yaml

ansible-playbook archive.yaml
Git archive using ansible-playbook
Git archive using ansible-playbook

3. Finally, change the directory to tmp/archive and run the ls command to verify if Ansible successfully archived the repository.

ls

As you can see below, the archived repository exists in the tmp/archive directory.

Verifying if the archived repository

Conclusion

In this tutorial, you’ve learned that the Git Ansible module is a great way to manage your Git repositories effectively using single Ansible command on remote hosts. With the Git Ansible module, you get to work with your Git repositories quickly and remotely.

Now, why not add more control on how you manage your repositories? Learn how to work with Ansible when and other conditionals.

Hate ads? Want to support the writer? Get many of our tutorials packaged as an ATA Guidebook.

Explore ATA Guidebooks

Looks like you're offline!