{"id":6170,"date":"2025-03-03T11:18:41","date_gmt":"2025-03-03T11:18:41","guid":{"rendered":"https:\/\/linuxbuz.com\/?p=6170"},"modified":"2025-07-11T04:40:09","modified_gmt":"2025-07-11T04:40:09","slug":"clone-a-git-repository-using-ansible-git-module","status":"publish","type":"post","link":"https:\/\/linuxbuz.com\/devops\/clone-a-git-repository-using-ansible-git-module","title":{"rendered":"How to Clone a Git Repository Using Ansible Git Module"},"content":{"rendered":"<p style=\"text-align: justify;\">Cloning a Git repository using Ansible is a simple way to automate code deployments and version control tasks. The <a href=\"https:\/\/docs.ansible.com\/ansible\/latest\/collections\/ansible\/builtin\/git_module.html\" target=\"_blank\" rel=\"noopener\"><strong>Ansible git module<\/strong><\/a> allows you to clone, update, and manage repositories from Git services like GitHub, GitLab, or others. It is helpful when you must consistently deploy the same codebase on multiple servers.<\/p>\n<p style=\"text-align: justify;\"><em>In this guide, we will show you how to use the Ansible git module to clone a repository with practical examples.<\/em><\/p>\n<div class=\"su-box su-box-style-default\" id=\"\" style=\"border-color:#001a66;border-radius:3px;max-width:none\"><div class=\"su-box-title\" style=\"background-color:#004d99;color:#FFFFFF;border-top-left-radius:1px;border-top-right-radius:1px\">\ud83d\udca1 Recommended Reading<\/div><div class=\"su-box-content su-u-clearfix su-u-trim\" style=\"border-bottom-left-radius:1px;border-bottom-right-radius:1px\">\n\ud83d\udc49 <a href=\"https:\/\/linuxbuz.com\/linuxhowto\/how-to-get-free-cloud-vps-server-step-by-step-guide\" target=\"_blank\" rel=\"noopener\">How to Get a Free Cloud Server for Testing<\/a><br \/>\n<\/div><\/div>\n<div id=\"ez-toc-container\" class=\"ez-toc-v2_0_82_2 counter-hierarchy ez-toc-counter ez-toc-custom ez-toc-container-direction\">\n<div class=\"ez-toc-title-container\">\n<p class=\"ez-toc-title\" style=\"cursor:inherit\">Table of Contents<\/p>\n<span class=\"ez-toc-title-toggle\"><\/span><\/div>\n<nav><ul class='ez-toc-list ez-toc-list-level-1 ' ><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-1\" href=\"https:\/\/linuxbuz.com\/devops\/clone-a-git-repository-using-ansible-git-module\/#When_to_Use_Ansible_Git_Module\" >When to Use Ansible Git Module<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-2\" href=\"https:\/\/linuxbuz.com\/devops\/clone-a-git-repository-using-ansible-git-module\/#Basic_Syntax\" >Basic Syntax<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-3\" href=\"https:\/\/linuxbuz.com\/devops\/clone-a-git-repository-using-ansible-git-module\/#Cloning_a_Public_Git_Repository\" >Cloning a Public Git Repository<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-4\" href=\"https:\/\/linuxbuz.com\/devops\/clone-a-git-repository-using-ansible-git-module\/#Cloning_a_Private_Git_Repository_Using_SSH_Key\" >Cloning a Private Git Repository Using SSH Key<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-5\" href=\"https:\/\/linuxbuz.com\/devops\/clone-a-git-repository-using-ansible-git-module\/#Cloning_a_Private_Git_Repository_Using_a_Personal_Access_Token\" >Cloning a Private Git Repository Using a Personal Access Token<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-6\" href=\"https:\/\/linuxbuz.com\/devops\/clone-a-git-repository-using-ansible-git-module\/#Conclusion\" >Conclusion<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-7\" href=\"https:\/\/linuxbuz.com\/devops\/clone-a-git-repository-using-ansible-git-module\/#FAQs\" >FAQs<\/a><\/li><\/ul><\/nav><\/div>\n\n<h2><span class=\"ez-toc-section\" id=\"When_to_Use_Ansible_Git_Module\"><\/span>When to Use Ansible Git Module<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>The <strong>Ansible git module<\/strong> is useful in several situations:<\/p>\n<ul>\n<li style=\"text-align: justify;\">When your application&#8217;s source code is stored in a Git repository, you can use the <strong>git module<\/strong> to clone the latest version onto your servers.<\/li>\n<li style=\"text-align: justify;\">If your application configuration files are version-controlled in Git, you can easily deploy them across multiple servers using Ansible.<\/li>\n<li style=\"text-align: justify;\">You can automate the deployment of code changes from a Git repository to your servers as part of a CI\/CD pipeline.<\/li>\n<li style=\"text-align: justify;\">The module helps quickly restore code or configurations from a Git repository after data loss or corruption.<\/li>\n<\/ul>\n<h2><span class=\"ez-toc-section\" id=\"Basic_Syntax\"><\/span>Basic Syntax<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>The basic syntax of the <strong>git module<\/strong> in Ansible is as follows.<\/p>\n<pre style=\"background-color: #262626; color: white; border-radius: 7px; overflow: auto;\"><code>- name: Clone a Git repository\r\n  ansible.builtin.git:\r\n    repo: repository_url\r\n    dest: destination_path\r\n    version: branch_or_tag\r\n    force: yes|no\r\n    update: yes|no<\/code><\/pre>\n<p><strong>Here is what each parameter means.<\/strong><\/p>\n<ul>\n<li><strong>repo:<\/strong> The URL of the Git repository.<\/li>\n<li><strong>dest:<\/strong> The destination path where the repository should be cloned.<\/li>\n<li><strong>version:<\/strong> The specific branch or tag to check out.<\/li>\n<li><strong>force:<\/strong> Whether to force a checkout of the branch or tag.<\/li>\n<li><strong>update:<\/strong> Whether to update the repository if it already exists.<\/li>\n<\/ul>\n<h2><span class=\"ez-toc-section\" id=\"Cloning_a_Public_Git_Repository\"><\/span>Cloning a Public Git Repository<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p style=\"text-align: justify;\">Imagine you&#8217;re deploying a static website whose source code is hosted on a public <strong>GitHub repository.<\/strong> You need to clone the repository onto a web server to serve the site.<\/p>\n<p>Here\u2019s an <a href=\"https:\/\/linuxbuz.com\/devops\/ansible-playbooks-examples\" target=\"_blank\" rel=\"noopener\"><strong>example playbook<\/strong><\/a> to clone a public Git repository to your web server.<\/p>\n<pre style=\"background-color: #262626; color: white; border-radius: 7px; overflow: auto;\"><code>- name: Deploy Static Website\r\n  hosts: web_servers\r\n  tasks:\r\n    - name: Clone static website repository\r\n      ansible.builtin.git:\r\n        repo: 'https:\/\/github.com\/example\/static-website.git'\r\n        dest: '\/var\/www\/static-website'\r\n        version: 'main'\r\n        update: yes<\/code><\/pre>\n<p style=\"text-align: justify;\">This playbook clones the <strong>static-website<\/strong> repository from GitHub into the <strong>\/var\/www\/static-website<\/strong> directory on the web servers. It checks out the main branch and updates the repository if it already exists.<\/p>\n<h2><span class=\"ez-toc-section\" id=\"Cloning_a_Private_Git_Repository_Using_SSH_Key\"><\/span>Cloning a Private Git Repository Using SSH Key<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p style=\"text-align: justify;\">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 <a href=\"https:\/\/linuxbuz.com\/devops\/ansible-authorized_key-module\" target=\"_blank\" rel=\"noopener\"><strong>SSH keys<\/strong><\/a>.<\/p>\n<p>Here\u2019s an example of cloning a private Git repository using an SSH key.<\/p>\n<pre style=\"background-color: #262626; color: white; border-radius: 7px; overflow: auto;\"><code>- name: Deploy Microservice\r\n  hosts: app_servers\r\n  tasks:\r\n    - name: Ensure SSH key is present\r\n      ansible.builtin.copy:\r\n        src: '\/path\/to\/private\/key'\r\n        dest: '\/home\/{{ ansible_user }}\/.ssh\/id_rsa'\r\n        mode: '0600'\r\n    \r\n    - name: Clone private microservice repository\r\n      ansible.builtin.git:\r\n        repo: 'git@github.com:example\/private-repo.git'\r\n        dest: '\/opt\/microservice'\r\n        version: 'release-v1.0'\r\n        key_file: '\/home\/{{ ansible_user }}\/.ssh\/id_rsa'\r\n        update: yes\r\n        accept_hostkey: yes<\/code><\/pre>\n<p style=\"text-align: justify;\">In this scenario, the playbook uses the <a href=\"https:\/\/linuxbuz.com\/devops\/ansible-copy-module-examples\" target=\"_blank\" rel=\"noopener\"><strong>copy module<\/strong><\/a> to ensure that the necessary SSH key is present on the servers. It then clones the <strong>private-repo<\/strong> from <strong>GitHub<\/strong> into the <strong>\/opt\/microservice<\/strong> directory, checking out the <strong>release-v1.0<\/strong> branch. The <strong>key_file<\/strong> parameter specifies the SSH key to use for authentication.<\/p>\n<h2><span class=\"ez-toc-section\" id=\"Cloning_a_Private_Git_Repository_Using_a_Personal_Access_Token\"><\/span>Cloning a Private Git Repository Using a Personal Access Token<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p style=\"text-align: justify;\">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.<\/p>\n<p>Here&#8217;s an example playbook to clone a private Git repository using a PAT.<\/p>\n<pre style=\"background-color: #262626; color: white; border-radius: 7px; overflow: auto;\"><code>---\r\n- name: Clone Private Git Repository\r\n  hosts: web_servers\r\n  vars:\r\n    repo_url: \"https:\/\/github.com\/hitjethva\/private-repo.git\"\r\n    git_username: \"your_username\"\r\n    git_token: \"your_personal_access_token\"\r\n    dest_path: \"\/var\/www\/private-repo\"\r\n\r\n  tasks:\r\n    - name: Ensure destination directory exists\r\n      ansible.builtin.file:\r\n        path: \"{{ dest_path }}\"\r\n        state: directory\r\n\r\n    - name: Clone the private repository\r\n      ansible.builtin.git:\r\n        repo: \"{{ repo_url }}\"\r\n        dest: \"{{ dest_path }}\"\r\n        version: \"main\"\r\n        clone: yes\r\n        update: yes\r\n        force: yes\r\n        accept_hostkey: yes\r\n        credentials:\r\n          username: \"{{ git_username }}\"\r\n          password: \"{{ git_token }}\"\r\n\r\n    - name: Set appropriate permissions for the repository files\r\n      ansible.builtin.file:\r\n        path: \"{{ dest_path }}\"\r\n        owner: www-data\r\n        group: www-data\r\n        mode: '0755'\r\n        recurse: yes\r\n\r\n    - name: Notify team of successful clone\r\n      ansible.builtin.debug:\r\n        msg: \"The repository has been successfully cloned to {{ dest_path }}\"<\/code><\/pre>\n<p><strong>Here\u2019s a breakdown of each task.<\/strong><\/p>\n<ul>\n<li style=\"text-align: justify;\">First task creates the destination directory on the target servers if it doesn&#8217;t already exist using the <a href=\"https:\/\/linuxbuz.com\/devops\/ansible-file-module-examples\" target=\"_blank\" rel=\"noopener\"><strong>file module<\/strong><\/a>.<\/li>\n<li style=\"text-align: justify;\">Second task clone the repository. The credentials parameter securely passes the username and PAT for authentication.<\/li>\n<li style=\"text-align: justify;\">Third task sets the proper ownership and permissions on the cloned files to ensure they are accessible by the web server.<\/li>\n<li style=\"text-align: justify;\">Fourth task confirms that the repository was cloned successfully.<\/li>\n<\/ul>\n<h2><span class=\"ez-toc-section\" id=\"Conclusion\"><\/span>Conclusion<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p style=\"text-align: justify;\">The <strong>Ansible git module<\/strong> 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.<\/p>\n<h2><span class=\"ez-toc-section\" id=\"FAQs\"><\/span>FAQs<span class=\"ez-toc-section-end\"><\/span><\/h2>\n        <section class=\"sc_fs_faq sc_card \">\n            <div>\n\t\t\t\t<h6>1. How do I handle authentication when cloning a private repository?<\/h6>                <div>\n\t\t\t\t\t                    <p>\n\t\t\t\t\t\tYou can use the <strong>key_file<\/strong> parameter to specify an SSH key or provide credentials using an environment variable or Git credential store.                    <\/p>\n                <\/div>\n            <\/div>\n        <\/section>\n\t\t        <section class=\"sc_fs_faq sc_card \">\n            <div>\n\t\t\t\t<h6>2. How do I ensure the cloned repository is always up-to-date?<\/h6>                <div>\n\t\t\t\t\t                    <p>\n\t\t\t\t\t\tUse the <strong>force: yes<\/strong> parameter to make sure the repository is updated even if there are local changes.                    <\/p>\n                <\/div>\n            <\/div>\n        <\/section>\n\t\t        <section class=\"sc_fs_faq sc_card \">\n            <div>\n\t\t\t\t<h6>3. Can I set a timeout for the Git clone operation?<\/h6>                <div>\n\t\t\t\t\t                    <p>\n\t\t\t\t\t\tYes, use the <strong>clone_timeout<\/strong> parameter to set a timeout in seconds for the Git clone operation.                    <\/p>\n                <\/div>\n            <\/div>\n        <\/section>\n\t\t        <section class=\"sc_fs_faq sc_card \">\n            <div>\n\t\t\t\t<h6>4. Can I use the git module with a proxy?<\/h6>                <div>\n\t\t\t\t\t                    <p>\n\t\t\t\t\t\tYes, you can set proxy settings for Git by configuring them globally on the host or by using environment variables during the playbook run.                    <\/p>\n                <\/div>\n            <\/div>\n        <\/section>\n\t\t\n<script type=\"application\/ld+json\">\n    {\n\t\t\"@context\": \"https:\/\/schema.org\",\n\t\t\"@type\": \"FAQPage\",\n\t\t\"mainEntity\": [\n\t\t\t\t{\n\t\t\t\t\"@type\": \"Question\",\n\t\t\t\t\"name\": \"1. How do I handle authentication when cloning a private repository?\",\n\t\t\t\t\"acceptedAnswer\": {\n\t\t\t\t\t\"@type\": \"Answer\",\n\t\t\t\t\t\"text\": \"You can use the <strong>key_file<\/strong> parameter to specify an SSH key or provide credentials using an environment variable or Git credential store.\"\n\t\t\t\t\t\t\t\t\t}\n\t\t\t}\n\t\t\t,\t\t\t\t{\n\t\t\t\t\"@type\": \"Question\",\n\t\t\t\t\"name\": \"2. How do I ensure the cloned repository is always up-to-date?\",\n\t\t\t\t\"acceptedAnswer\": {\n\t\t\t\t\t\"@type\": \"Answer\",\n\t\t\t\t\t\"text\": \"Use the <strong>force: yes<\/strong> parameter to make sure the repository is updated even if there are local changes.\"\n\t\t\t\t\t\t\t\t\t}\n\t\t\t}\n\t\t\t,\t\t\t\t{\n\t\t\t\t\"@type\": \"Question\",\n\t\t\t\t\"name\": \"3. Can I set a timeout for the Git clone operation?\",\n\t\t\t\t\"acceptedAnswer\": {\n\t\t\t\t\t\"@type\": \"Answer\",\n\t\t\t\t\t\"text\": \"Yes, use the <strong>clone_timeout<\/strong> parameter to set a timeout in seconds for the Git clone operation.\"\n\t\t\t\t\t\t\t\t\t}\n\t\t\t}\n\t\t\t,\t\t\t\t{\n\t\t\t\t\"@type\": \"Question\",\n\t\t\t\t\"name\": \"4. Can I use the git module with a proxy?\",\n\t\t\t\t\"acceptedAnswer\": {\n\t\t\t\t\t\"@type\": \"Answer\",\n\t\t\t\t\t\"text\": \"Yes, you can set proxy settings for Git by configuring them globally on the host or by using environment variables during the playbook run.\"\n\t\t\t\t\t\t\t\t\t}\n\t\t\t}\n\t\t\t\t    ]\n}\n<\/script>\n\n","protected":false},"excerpt":{"rendered":"<p>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 &hellip; <\/p>\n","protected":false},"author":1,"featured_media":6171,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"om_disable_all_campaigns":false,"_monsterinsights_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0,"two_page_speed":[],"footnotes":""},"categories":[828],"tags":[],"class_list":["post-6170","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-devops"],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/linuxbuz.com\/wp-json\/wp\/v2\/posts\/6170","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/linuxbuz.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/linuxbuz.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/linuxbuz.com\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/linuxbuz.com\/wp-json\/wp\/v2\/comments?post=6170"}],"version-history":[{"count":0,"href":"https:\/\/linuxbuz.com\/wp-json\/wp\/v2\/posts\/6170\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/linuxbuz.com\/wp-json\/wp\/v2\/media\/6171"}],"wp:attachment":[{"href":"https:\/\/linuxbuz.com\/wp-json\/wp\/v2\/media?parent=6170"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/linuxbuz.com\/wp-json\/wp\/v2\/categories?post=6170"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/linuxbuz.com\/wp-json\/wp\/v2\/tags?post=6170"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}