Ansible Git Module Examples-How To Use Git With Ansible-DecodingDevOps

Ansible Git Module Examples-How To Use Git With Ansible-DecodingDevOps

ansible git module is used to checkout or download the code from your github or bitbucket or gitlab account. With ansible git module we can check out the code OR we can clone the github repository into your system and we can archive the code in zip or any format. In this blog post i will describe different ansible git module examples or how to use git with ansible.

Checkout The Code From Github Using Ansible [OR]

Clone The Github Repository With Ansible

root@ip-172-31-37-35:~# cat git.yml


---
- hosts: localhost
  gather_facts: no
  vars:
    username: decodingdevops
    token: d6sdfshhghyjggj448t8tnt9h
    repo_name: devops

  tasks:
  - name: Checkout The Code From Github Using Ansible.
    git:
     repo: 'https://{{ token }}@github.com/{{ username }}/{{ repo_name }}.git'
     dest: /root/mycode

In ansible git module generally we have to pass two arguments those are repo and dest. Repo represents the your github repository url and dest represents the path in which directory/folder you want to checkout the code. In above example my github code will be downloaded into /root/mycode directory.

Here you can see three variables username, token and repo_name.

  • username is nothing but your github username
  • Here token is github Personal access token. we can generate github personal access token in our github account.
    • How to Generate Personal Access Token in Github
  • repo_name is nothing but your repository name in github

change above variable values with your github account values. After executing the playbook your github repository will be cloned into destination directory.

root@ip-172-31-37-35:~# ansible-playbook sda.yml



PLAY [localhost] ***********************************************************************************************************************************************************

TASK [Checkout The Code From Github Using Ansible.] ************************************************************************************************************************
changed: [localhost]

PLAY RECAP *****************************************************************************************************************************************************************
localhost                  : ok=1    changed=1    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0

root@ip-172-31-37-35:~# ls

mycode  git.yml

Note: Here i mentioned destination directory as mycode so entire your repository code will be downloaded into this folder. This will not create any directory with your repository name in destination directory. Generally if we clone the GitHub repository we will get entire repository as a directory. If you want to clone repository with the same name, enter destination directory same as repository name. In above example my github repository name is devops but i cloned as a mycode directory.

Leave a Reply

Your email address will not be published. Required fields are marked *