Cloning a Git repository using Ansible is a simple way to automate code deployments and version control tasks. The Ansible git module allows you to clone, update, and manage repositories from Git services like GitHub, GitLab, or others. It is especially helpful when you must consistently deploy the same codebase on multiple servers.
In this guide, we will show you how to use the Ansible git module to clone a repository with practical examples.
Table of Contents
When to Use Ansible Git Module
The Ansible git module is useful in several situations:
- When your application’s source code is stored in a Git repository, you can use the git module to clone the latest version onto your servers.
- If your application configuration files are version-controlled in Git, you can easily deploy them across multiple servers using Ansible.
- You can automate the deployment of code changes from a Git repository to your servers as part of a CI/CD pipeline.
- The module helps quickly restore code or configurations from a Git repository after data loss or corruption.
Basic Syntax
The basic syntax of the git module in Ansible is as follows.
- name: Clone a Git repository
ansible.builtin.git:
repo: repository_url
dest: destination_path
version: branch_or_tag
force: yes|no
update: yes|no
Here is what each parameter means.
- repo: The URL of the Git repository.
- dest: The destination path where the repository should be cloned.
- version: The specific branch or tag to check out.
- force: Whether to force a checkout of the branch or tag.
- update: Whether to update the repository if it already exists.
Cloning a Public Git Repository
Imagine you’re deploying a static website whose source code is hosted on a public GitHub repository. You need to clone the repository onto a web server to serve the site.
Here’s an example playbook to clone a public Git repository to your web server.
- name: Deploy Static Website
hosts: web_servers
tasks:
- name: Clone static website repository
ansible.builtin.git:
repo: 'https://github.com/example/static-website.git'
dest: '/var/www/static-website'
version: 'main'
update: yes
This playbook clones the static-website repository from GitHub into the /var/www/static-website directory on the web servers. It checks out the main branch and updates the repository if it already exists.
Cloning a Private Git Repository Using SSH Key
Imagine you are deploying a microservice whose source code is stored in a private Git repository. The deployment process requires pulling the latest code and setting up the environment on multiple servers. Access to the repository is controlled via SSH keys.
Here’s an example playbook to clone a private Git repository using an SSH key.
- name: Deploy Microservice
hosts: app_servers
tasks:
- name: Ensure SSH key is present
ansible.builtin.copy:
src: '/path/to/private/key'
dest: '/home/{{ ansible_user }}/.ssh/id_rsa'
mode: '0600'
- name: Clone private microservice repository
ansible.builtin.git:
repo: 'git@github.com:example/private-repo.git'
dest: '/opt/microservice'
version: 'release-v1.0'
key_file: '/home/{{ ansible_user }}/.ssh/id_rsa'
update: yes
accept_hostkey: yes
In this scenario, the playbook first ensures that the necessary SSH key is present on the servers. It then clones the private-repo from GitHub into the /opt/microservice directory, checking out the release-v1.0 branch. The key_file parameter specifies the SSH key to use for authentication.
Cloning a Private Git Repository Using a Personal Access Token
Imagine you want to deploy an application whose source code is in a private Git repository. The team uses personal access tokens for authentication instead of SSH keys.
Here’s an example playbook to clone a private Git repository using a PAT.
---
- name: Clone Private Git Repository
hosts: web_servers
vars:
repo_url: "https://github.com/hitjethva/private-repo.git"
git_username: "your_username"
git_token: "your_personal_access_token"
dest_path: "/var/www/private-repo"
tasks:
- name: Ensure destination directory exists
ansible.builtin.file:
path: "{{ dest_path }}"
state: directory
- name: Clone the private repository
ansible.builtin.git:
repo: "{{ repo_url }}"
dest: "{{ dest_path }}"
version: "main"
clone: yes
update: yes
force: yes
accept_hostkey: yes
credentials:
username: "{{ git_username }}"
password: "{{ git_token }}"
- name: Set appropriate permissions for the repository files
ansible.builtin.file:
path: "{{ dest_path }}"
owner: www-data
group: www-data
mode: '0755'
recurse: yes
- name: Notify team of successful clone
ansible.builtin.debug:
msg: "The repository has been successfully cloned to {{ dest_path }}"
Here’s a breakdown of each task.
- First task creates the destination directory on the target servers if it doesn’t already exist.
- Second task uses the git module to clone the repository. The credentials parameter securely passes the username and PAT for authentication.
- Third task sets the proper ownership and permissions on the cloned files to ensure they are accessible by the web server.
- Fourth task confirms that the repository was cloned successfully.
Conclusion
The Ansible git module provides a flexible and efficient way to manage Git repositories across multiple servers. With this module, you can specify branches, manage updates, and handle authentication without manual intervention. This is ideal for automating repetitive tasks and ensuring consistent environments.
FAQs
1. How do I handle authentication when cloning a private repository?
You can use the key_file parameter to specify an SSH key or provide credentials using an environment variable or Git credential store.
2. How do I ensure the cloned repository is always up-to-date?
Use the force: yes parameter to make sure the repository is updated even if there are local changes.
3. Can I set a timeout for the Git clone operation?
Yes, use the clone_timeout parameter to set a timeout in seconds for the Git clone operation.
4. Can I use the git module with a proxy?
Yes, you can set proxy settings for Git by configuring them globally on the host or by using environment variables during the playbook run.