简体   繁体   中英

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM