One of cool things that Ansible can do is to clone a git repository and that’s what we’ll be talking about in this article – how to clone a git repository with Ansible.
This enables us to have an automated solution for various setup processes(ie, downloading and setting up a dev environment on a remote location, need of download a script that needs to execute after the git repo is cloned, etc… ).
Now let’s go through the process and see what we need in order to achieve this.
- Ansible installed
- Git installed
- Python 3 or newer
- Sudo user access
Clone the Git repository with Ansible
The code template we’ll be using is the following:
--- - hosts: all become: true tasks: - name: Ensure that git is installed dnf: name: git state: present - name: Clone the Git repository git: repo: https://github.com/user/repo.git dest: /path/to/destination clone: yes
I run Fedora, so that’s why I’m using the dnf module in the code. You need to change to the packager manager you’re using or you can exclude this block of code entirely, it’s up to you.
One important note for the git repo destination path – Ansible will not create the directory and place the git files in the newly created folders as the git tool does automatically. It will throw an error that the destination is not empty and that it requires an empty directory.
So, in order to successfully clone a git repository with Ansible, we need to create a new empty directory and set the full path of the directory as a destination path in Ansible.
pic 1 – setup
Clone a Git repository with Ansible on localhost
Let’s first check out how to clone a git repository on a localhost. For an example, I’ll be cloning one of my github repos to my Fedora laptop. The code will look like this:
--- - hosts: localhost become: true tasks: - name: Ensure that git is installed dnf: name: git state: present - name: Clone the Git repository git: repo: https://github.com/markonisic/wp-deploy dest: /home/markon/Documents/Ansible_Git_Clone/GIT_REPO/ clone: yes
And when I run the playbook this should be the end result:
In the command I executed, I added arguments for the user and for Ansible to prompt for the sudo password.
And in the directory I created for the destination path, a git repo is cloned.
Clone a Git repository with Ansible to a remote destination
Now that we know how to clone a git repository with Ansible on a local machine, let’s see if it works on a remote machine, meaning we now want to clone a git repo with Ansible to a remote server.
The code I’ll be testing:
--- - hosts: all become: true tasks: - name: Ensure that git is installed dnf: name: git state: present - name: Clone the Git repository git: repo: https://github.com/markonisic/wp-deploy dest: /home/markon/git-repo clone: yes
The code is pretty similar. The difference here is that we need to create an inventory file and within the file to define our remote machine and add couple of more arguments on our Ansible command in order to use the inventory and execute the playbook remotely.
The command we need to run:
ansible-playbook -u markon -k -K -i inventory git_clone.yml
In the command I added the “-k” option to provide Ansible with the SSH password and with the “-i” to define the inventory file which Ansible needs to use and reads the IP address of the remote machine.
Again, after you execute the playbook, this should be end result.
Let’s review what we covered in this article – we discovered that we are able to clone a git repository with Ansible, allowing us to automate the process. We also that learned that Ansible is capable of cloning a git repo locally and remotely as well which gives multiple case scenarios to use this feature.
Thank your for taking time to read the article, hope you find it interesting.