AnsiblePilot — Master Ansible Automation

AnsiblePilot is the leading resource for learning Ansible automation, DevOps, and infrastructure as code. Browse over 1,400 tutorials covering Ansible modules, playbooks, roles, collections, and real-world examples. Whether you are a beginner or an experienced engineer, our step-by-step guides help you automate Linux, Windows, cloud, containers, and network infrastructure.

Popular Topics

About Luca Berton

Luca Berton is an Ansible automation expert, author of 8 Ansible books published by Apress and Leanpub including "Ansible for VMware by Examples" and "Ansible for Kubernetes by Example", and creator of the Ansible Pilot YouTube channel. He shares practical automation knowledge through tutorials, books, and video courses to help IT professionals and DevOps engineers master infrastructure automation.

ansible.builtin.git Module: Clone & Checkout Git Repositories (Guide) — Video Tutorial

How to clone and checkout Git repositories with Ansible git module (ansible.builtin.git). Deploy code from GitHub, manage branches, tags, force updates.

Watch on YouTube · Read the written article

Tutorial summary

What you'll learn

  • How to checkout git repository via HTTPS?
  • Ansible checkout git repository
  • Parameters and Return Values
  • Demo
  • Conclusion
  • Advanced Git Module Usage
  • Clone a specific branch
  • Clone a specific tag
  • Clone with SSH key (private repos)
  • Shallow clone (faster, saves disk)
How to checkout git repository via HTTPS? I'm going to show you a live Playbook with some simple Ansible code. I'm Luca Berton and welcome to today's episode of Ansible Pilot Ansible checkout git repository Today we're talking about Ansible module git. The full name is ansible.builtin.git which means is part of the collection of modules "builtin" with ansible and shipped with it. This module is pretty stable and out for years. The purpose is to Deploy software (or files) from git checkouts in our managed hosts. If you would like to fetch via SSH please refer to: [Checkout git repository SSH - Ansible module git](/articles/checkout-git-repository-ssh-ansible-module-git) Parameters and Return Values The parameter list is pretty wide but I'll summarize the most useful. - **repo** _path_ - **dest** _string_ - update _boolean_ The only required parameters are "repo" and "dest". "repo" specifies the source repository URL. "dest" specify the destination path. The "update" retrieves new revisions from the already synched origin repository. - after _string_ The most interesting return value is "after" which contains the last commit after the update process. Demo Let's jump in a real-life playbook to checkout a git repository with Ansible ```yaml --- - name: git module Playbook hosts: all become: true tasks: - name: ensure git pkg installed ansible.builtin.yum: name: git state: present - name: checkout git repo ansible.builtin.git: repo: https://github.com/lucab85/ansible-pilot.git dest: /home/devops/ansible-pilot ``` [code with ❤️ in GitHub](https://github.com/lucab85/ansible-pilot/tree/master/checkout%20git%20repository) Conclusion Now you know how to checkout git repository via HTTPS with Ansible. Advanced Git Module Usage Clone a specific branch ```yaml - name: Clone develop branch ansible.builtin.git: repo: https://github.com/ansible/ansible.git dest: /opt/ansible version: develop ``` Clone a specific tag ```yaml - name: Clone release tag ansible.builtin.git: repo: https://github.com/ansible/ansible.git dest: /opt/ansible version: v2.17.0 ``` Clone with SSH key (private repos) ```yaml - name: Clone private repo via SSH ansible.builtin.git: repo: git@github.com:myorg/private-repo.git dest: /opt/private-repo key_file: /home/deploy/.ssh/deploy_key accept_hostkey: true ``` Shallow clone (faster, saves disk) ```yaml - name: Shallow clone (depth 1) ansible.builtin.git: repo: https://github.com/ansible/ansible.git dest: /opt/ansible depth: 1 ``` Force update (discard local changes) ```yaml - name: Force pull latest ansible.builtin.git: repo: https://github.com/myorg/myapp.git dest: /opt/myapp force: true version: main ``` Clone with authentication (HTTPS token) ```yaml - name: Clone with GitHub token ansible.builtin.git: repo: "https://{{ github_token }}@github.com/myorg/private-repo.g

About this tutorial

  • Author: Luca Berton
  • Difficulty: Advanced
  • Read time: 4 min
  • Category: installation

Topics covered

Related video tutorials