我想要用Ansible存储当前已检出的提交SHA-1
哈希版本的代码。
我想要使用set_fact
该版本,在另一个角色中使用。
Ansible中的git
模块会为您返回此信息,您只需要在一个变量中注册它(在下面的示例中,该变量是gitresult
)。
- 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 }}"
运行中:
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"
}
我希望它能解决你的问题。
git log
命令中显示),但是git模块需要repo
参数,并且我的测试似乎证实如果无法访问,则会出现错误。 - AlanSEgit
模块需要repo
参数,则它将无法工作。但是您可以在上述用例中使用command
模块:command: git log --pretty=format:"%H" -n 1
或者command: git rev-parse HEAD
将给您SHA-1,只需register: gitresult
,您的SHA-1将可在{{ gitresult.std_out }}
中访问。 - Michal Gasekclone
和update
都设置为false将防止下载,这也是官方文档中建议的。 - AlanSE
git
模块进行检出。 - tread