How to checkout git repository via SSH?

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 HTTPS - 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
  • key_file path - SSH private key

The parameter list is pretty wide but I’ll summarize the most useful. The only required parameters are “repo” and “dest”. The “repo” parameter specifies the source repository URL, in our use case, in the SSH way. The “dest” parameter specifies the destination path. The “update” parameter retrieves new revisions from the already synched origin repository. The “key_file” parameter specifies the path in the filesystem where to store the SSH private key. Please note that the SSH private key is a path on the target host. Please note also that the SSH public key needs to be already shared in your Git server.

  • 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

  • git_ssh.yml
---
- name: git module Playbook
  hosts: all
  vars:
    repo: "[email protected]:lucab85/ansible-pilot.git"
    dest: "/home/devops/ansible-pilot"
    sshkey: "~/.ssh/id_rsa"
  tasks:
    - name: ensure git pkg installed
      ansible.builtin.yum:
        name: git
        state: present
        update_cache: true
      become: true

    - name: checkout git repo
      ansible.builtin.git:
        repo: "{{ repo }}"
        dest: "{{ dest }}"
        key_file: "{{ sshkey }}"

code with ❤️ in GitHub

Conclusion

Now you know how to checkout git repository via SSH with Ansible. Subscribe to the YouTube channel, Medium, and Website, X (formerly Twitter) to not miss the next episode of the Ansible Pilot.

Academy

Learn the Ansible automation technology with some real-life examples in my Udemy 300+ Lessons Video Course.

BUY the Complete Udemy 300+ Lessons Video Course

My book Ansible By Examples: 200+ Automation Examples For Linux and Windows System Administrator and DevOps

BUY the Complete PDF BOOK to easily Copy and Paste the 250+ Ansible code

Want to keep this project going? Please donate

Patreon Buy me a Pizza