确定变量的值时,请勿将某些内容标记为已更改 [英] Don't mark something as changed when determining the value of a variable

查看:62
本文介绍了确定变量的值时,请勿将某些内容标记为已更改的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在Ansible剧本中扮演以下角色,以确定 Packer 的安装版本,如果没有安装,则有条件地安装它. t匹配局部变量的版本:

I have the following role in my Ansible playbook to determine the installed version of Packer and conditionally install it if it doesn't match the version of a local variable:

---
#  detect packer version
 - name: determine packer version
   shell: /usr/local/bin/packer -v || true
   register: packer_installed_version
 - name: install packer cli tools
   unarchive:
       src: https://releases.hashicorp.com/packer/{{ packer_version }}/packer_{{ packer_version }}_linux_amd64.zip
       dest: /usr/local/bin
       copy: no
   when: packer_installed_version.stdout != packer_version

问题/烦恼是Ansible将这一步标记为已更改":

The problem/annoyance is that Ansible marks this step as having "changed":

我想收集这个事实而不将任何内容标记为已更改,因此我可以在剧本执行结束时可靠地知道实际上是否有任何更改.

I'd like gather this fact without marking something as changed so I can know reliably at the end of my playbook execution if anything has, in fact, changed.

是否有更好的方法来进行上述操作?

Is there a better way to go about what I'm doing above?

推荐答案

来自 Ansible文档:

覆盖1.3版中的新更改结果.

Overriding The Changed Result New in version 1.3.

运行shell/命令或其他模块时,通常会报告 根据是否认为它影响了计算机状态来改变"状态.

When a shell/command or other module runs it will typically report "changed" status based on whether it thinks it affected machine state.

有时您会根据返回码或输出知道 没有进行任何更改,并希望覆盖已更改"的结果 这样它就不会出现在报告输出中或不会导致 处理人员开火:

Sometimes you will know, based on the return code or output that it did not make any changes, and wish to override the "changed" result such that it does not appear in report output or does not cause handlers to fire:

tasks:

  - shell: /usr/bin/billybass --mode="take me to the river"
    register: bass_result
    changed_when: "bass_result.rc != 2"

  # this will never report 'changed' status
  - shell: wall 'beep'
    changed_when: False

您需要的是:

---
#  detect packer version
 - name: determine packer version
   shell: /usr/local/bin/packer -v || true
   register: packer_installed_version
   changed_when: False
 - name: install packer cli tools
   unarchive:
       src: https://releases.hashicorp.com/packer/{{ packer_version }}/packer_{{ packer_version }}_linux_amd64.zip
       dest: /usr/local/bin
       copy: no
   when: packer_installed_version.stdout != packer_version

这篇关于确定变量的值时,请勿将某些内容标记为已更改的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆