如何在Ansible的检查模式下运行shell命令?

18
在检查模式下,我想在服务器上显示当前提交。我使用Shell命令(git rev-parse HEAD)来注册变量,然后打印/调试它,但ansible会跳过检查模式中的Shell命令。
有没有任何方法可以将Shell命令标记为在检查模式下安全运行?
或者有任何ansible模块可以实现我想要的功能吗?我查看了git的模块,但似乎只能做检出。
任何输入将不胜感激。
2个回答

22
从 Ansible 2.2 开始,正确的方法是使用check_mode: no:
tasks:
  - name: this task will make changes to the system even in check mode
    command: /something/to/run --even-in-check-mode
    check_mode: no

6
“check_mode: no” 的意思是不要在检查模式下运行,但实际上应该在非检查模式下运行吗?那么,“no” 是什么意思呢? - Nick
@Nick "为了强制一个任务在检查模式下运行,即使playbook没有使用--check参数调用,需要设置check_mode: yes。" https://docs.ansible.com/ansible/latest/playbook_guide/playbooks_checkmode.html - took

12

找到了答案。您需要在任务中添加always_run: True


2
既然有更好的方法,我认为被接受的答案应该改为@jfrenetic的答案。 - k1eran
已经被弃用。我得到了这个,[DEPRECATION WARNING]: always_run is deprecated. Use check_mode = no instead.. This feature will be removed in version 2.4. Deprecation warnings can be disabled by setting deprecation_warnings=False in ansible.cfg. - Nikhil Wagh

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接