Ansible is a system configuration management and provisioning tool, with an emphasis on simplicity. Automating all aspects of software deployment through Ansible provides significant benefits to the enterprise.
The “ansible playbook to copy files from git” is a command-line tool that allows users to manage repositories via the GIT Ansible Module. This can be used for cloning, pushing, and pulling.
Working with web-based repositories necessitates the use of a version control system, and Git is the most widely used nowadays. But no one wants to spend all day manually maintaining repositories. So, how can you automate repository management? Why not test the Git Ansible module before searching the internet for answers?
The Ansible Git module makes it easy to interact with Git repositories. You’ll also learn how to use the Git Ansible module to clone or execute checkouts in other directories for your GitHub repositories in this tutorial.
This tutorial includes detailed instructions. If you want to join in, make sure you have the following items:
Related: Ansible Setup Guide (Ubuntu, RHEL, CentOS, macOS)
- To test the Git Ansible module, you’ll need a remote Linux PC. The remote node in this tutorial is Ubuntu 20.04.3 LTS.
- A GitHub account with one private and one public repository containing a hello.py Python file was established.
- The Ansible Node generates an SSH key. The secret key will stay on the Ansible remote node, but you’ll use GitHub to authenticate using the public key.
- An inventory file has been created, and one or more hosts have been set up to perform Ansible commands and playbooks. Myserver is the name of the remote Linux machine, and web is the inventory group for this lesson.
- On both the Ansible controller host and the remote node system, Python v3.6 or later is required. On an Ubuntu system, Python v3.8.10 will be used in this course.
How Do You Install Python 3.6? Related:
Using Ansible Ad Hoc Commands to Clone a Git Repository
Perhaps you want to swiftly clone a repository to a distant server. If that’s the case, ad hoc commands will suffice. Begin this lesson by cloning a repository using ad-hoc instructions using the Git Ansible module. Ad hoc commands allow you to perform a single command on a remote computer quickly and efficiently.
To connect to the host, log into your Ansible controller and perform the ansible command below (-m ansible.builtin.git) (web).
The -a parameter instructs Ansible to clone (clone=yes) the content from the repository (repo) to the remote host’s destination (dest=/tmp/clone test).
Make careful to change the repository URL https://github.com/ShankyTheBlogger/ATA-testing.git in each code throughout the guide with your own.
web -m ansible “repo=https://github.com/ShankyTheBlogger/ATA-testing.git clone=yes dest=/tmp/clone test” ansible.builtin.git -a
Following the completion of the operation, you’ll receive a CHANGED notification, as shown below, confirming that Ansible successfully cloned the repository on the remote system.
Cloning the Git repository to the tmp directory folder on the remote computer.
Ansible Playbook for Cloning Public and Private Git Repositories
Using an ad-hoc command to run Git commands on the Git repository may be acceptable. Nonetheless, running several tasks on a Git repository might be difficult. Use the Ansible Git module with a playbook using the ansible-playbook command instead of using ad-hoc commands.
If you’re already logged onto the Ansible controller host, do the following:
1. Create a directory named /ansible git module using the following instructions. This directory will hold the playbook as well as all of the necessary configuration files for using the Ansible Git module.
/ansible git module/mkdir /ansible git module/cd
2. Open your preferred text editor, create a file named main.yml in the /ansible git module directory, and copy/paste the contents of the following YAML playbook.
The script below conducts a variety of actions with the Git repository, including cloning the public and private repositories and validating cloning success.
—- – Demo of the Ansible GIT module web hosts # Specifies the remote server on which the ansible git module will handle the items remote user: ubuntu # Ubuntu tasks using a remote user: # In the opt directory, clones the public repository in the clone folder. – name: Clone ansible.builtin from a PUBLIC repo. git: dest: /opt/clone separate git dir: /opt/clone.git clone: yes repo: https://github.com/ShankyTheBlogger/ATA-testing.git Yes, it has been updated. # Clones the private repository in the *opt* directory’s private clone folder. – name: 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 primary version # If the public repository was successfully cloned in the provided folder, this value is true. – name: Ansible.builtin.git: repo: https://github.com/ShankyTheBlogger/ATA-testing.git dest: /opt/clone no update: no clone – name: Clone a repository using a different Git directory from the current working tree. ansible.builtin separate git dir: /opt/different-directory.git repo: https://github.com/ShankyTheBlogger/ATA-testing.git dest: /opt/different-directory.git
3. Invoke the playbook using the command below (main.yml). The tasks (cloning, checkout repositories, and so on) are then executed on the remote node via the playbook.
main.yml for ansible-playbook
Some tasks have a changed status, indicating that the remote host wasn’t in the right condition and had to be adjusted to perform the command. Tasks with an ok status, on the other hand, do not need any adjustments.
Using ansible-playbook to run an Ansible playbook to conduct Git operations on Github
4. Now execute each of the commands below to get a list of all the files in the opt directory. This command verifies that you’ve successfully cloned the Git repositories and performed a checkout in the directories provided in the main.yml playbook.
After executing the instructions, you should get something like this. The result indicates the remote node’s successfully cloned Git repositories in the provided chosen directory.
Checking for cloned repositories
Using SSH Git URL to Clone a Different Branch
Cloning a Git repository using an HTTP GitHub URL is simple, but is it secure? SSH Git URL is a safe method to connect your workstation to GitHub and clone the repository.
Create the diff branch.yaml playbook using the same Ansible Controller host and copy/paste the code below. The playbook below clones branch2 from the repository indicated in the version ([email protected]:ShankyTheBlogger/ATA-testing.git).
# Defining the remote server where the Ansible GIT module will handle the items remote user: ubuntu # Using Remote user as ubuntu tasks: – name: Clone branch2 from ansible.builtin.git’s ATA-tetsing repository: repo: [email protected]:ShankyTheBlogger/ATA-testing.git dest: /tmp/branch2-ata-testing version: branch2 clone: yes
Now run the playbook, which will clone the branch2.
diff branch.yaml ansibile-playbook
Using the Ansible playbook, clone branch2 from the ATA-testing repository.
Ansible Playbook Branch Switching with Git Checkout
If you’re the only one working on a small project, having only one branch is OK. What if you’re collaborating with other programmers? Switching between branches will be necessary. The answer is the Ansible Git module combined with Git checkout!
Checking out the desired state of your repositories, such as branches or specific files, is done using Git checkout.
Related: [Step-by-Step] How to Check Out a Remote Git Branch
Copy and paste the code below into the checkout.yaml Ansible playbook. The playbook below will checkout the opt directory repository.
# remote user: ubuntu —- – name: Ansible checkout demo hosts: web Ubuntu tasks using a remote user: # Name: Git checkout ansible.builtin.git: repo: ‘https://github.com/ShankyTheBlogger/ATA-testing.git’ dest: /opt/checkout # Confirms that the repository was successfully checked out in the opt directory – name: Checking whether ansible.builtin.git has been successfully checkout: repo: https://github.com/ShankyTheBlogger/ATA-testing.git dest: /opt/checkout update: no
Run the ls command to get a list of all the files in the opt directory.
The repository is successfully checked out in the opt directory, as seen below.
Checks whether the repository has been successfully checked out in the remote node’s opt directory.
GitHub Repository Archiving
You could choose to make the repository read-only for all users. If this is the case, archiving a repository is a viable option. Keep in mind, though, that you may unarchive your repository at any moment. When you archive a repository, all of its resources are saved to a ZIP file at a given location.
Make sure you’ve adjusted your repository’s settings before archiving it, and consider closing any existing problems or pull requests.
1. Create the archive.yaml Ansible playbook and copy/paste the code below. The playbook that follows copies the selected repository to the tmp/archive directory.
# remote user: ubuntu —- – name: Ansible checkout demo hosts: web Ubuntu tasks using a remote user: – name: Create a git archive from a repository containing the source tree structure. ansible.builtin.git: dest: /tmp/archive archive: /tmp/archive/archive.zip
2. Now run the archive.yaml playbook, which archives a specified repository. archive.yaml ansible-playbook
ansible-playbook git repository
3. Finally, navigate to tmp/archive and use the ls command to see whether Ansible archived the repository properly.
The archived repository is located in the tmp/archive directory, as shown below.
Checking whether the archived repository exists
In this article, you learnt how to use the Git Ansible module to manage your Git repositories on remote systems with a single Ansible command. You may work with your Git repositories fast and remotely with the Git Ansible module.
Why not give yourself more control over how you handle your repositories now? Learn how to use when and other conditionals in Ansible.
The “ansible git clone branch” is a command-line tool that allows users to manage repositories via the GIT Ansible Module. This tool can be used by developers and system administrators.
- ansible git module examples
- ansible git module username/password
- ansible git module push
- ansible git pull example
- ansible git commit and push