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

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

问题描述

我在我的 Ansible 剧本中扮演以下角色来确定

我希望在不将某些内容标记为已更改的情况下收集此事实,以便在我的剧本执行结束时能够可靠地知道是否有任何内容实际上发生了变化.

有没有更好的方法来处理我上面正在做的事情?

解决方案

来自 Ansible 文档:

<块引用>

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

当 shell/命令或其他模块运行时,它通常会报告已更改"状态取决于它是否认为它影响了机器状态.

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

任务:- shell:/usr/bin/billybass --mode="带我去河边"注册:bass_resultchanged_when: "bass_result.rc != 2"# 这永远不会报告已更改"状态- 外壳:墙壁哔"changed_when: 假

在你的情况下你会想要:

---# 检测打包器版本- 名称:确定打包器版本外壳:/usr/local/bin/packer -v ||真的注册:packer_installed_versionchanged_when: 假- 名称:安装packer cli工具取消存档:源代码:https://releases.hashicorp.com/packer/{{ packer_version }}/packer_{{ packer_version }}_linux_amd64.zip目标:/usr/local/bin副本:没有时间:packer_installed_version.stdout != packer_version

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

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?

解决方案

From the Ansible docs:

Overriding The Changed Result New in version 1.3.

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

In your case you would want:

---
#  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天全站免登陆