Do Not Mark Something Changed When Defining a Variable Value

I have the following role in my Ansible boot book to determine the installed version of Packer and conditionally install it if it does not match the version of the 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 β€œmodified”:

screenshot

I would like to collect this fact without noting something that has changed, so that I can reliably find out at the end of the execution of my book if something really has changed.

Is there a better way to do what I do above?

+6
source share
1 answer

From Ansible docs :

Overriding the changed result New in version 1.3.

When a shell / command or other module is launched, it usually reports a status of "changed", based on whether it believes that this affects the state of the machine.

Sometimes you will know, based on the return or output code, that it did not make any changes and would like to discard the β€œchanged” result so that it does not appear in the report or does not call handlers:

 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 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 
+13
source

All Articles