Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get the SHA of the checked out code with ansible git module?

I would like to store the currently checked out commit SHA-1 hash for the version of code with Ansible.

I want to set_fact of this version for use in another role.

like image 992
tread Avatar asked Mar 16 '16 09:03

tread


1 Answers

The git module in Ansible returns this information for you, you just need to register it in a variable (variable is gitresult in the example below).

- hosts: web
  tasks:
    - name: Checkout repo
      git:
        repo=https://github.com/michalgasek/www-discogs.git
        dest=/vagrant/checkout
        update=yes
        accept_hostkey=yes
      register: gitresult

    - debug: msg="SHA-1 before git update is {{ gitresult.before }}"
    - debug: msg="SHA-1 after git update is {{ gitresult.after }}"

Running :

PLAY ***************************************************************************

TASK [setup] *******************************************************************
ok: [192.168.2.201]

TASK [Checkout repo] ***********************************************************
ok: [192.168.2.201]

TASK [debug] *******************************************************************
ok: [192.168.2.201] => {
    "msg": "SHA-1 before git update is 87544e2ea1c8dec30e5fc68302caa262b10affda"
}

TASK [debug] *******************************************************************
ok: [192.168.2.201] => {
    "msg": "SHA-1 after git update is 87544e2ea1c8dec30e5fc68302caa262b10affda"
}

I hope it solves your problem.

like image 140
Michal Gasek Avatar answered Oct 13 '22 23:10

Michal Gasek