Evaluation of return code in inaccessible conditional

I am working on automating a task that should add the latest software to a file. I do not want him to do this several times for the same version.

He looks at the following example file:

var software releases = new Array( "4.3.0", "4.4.0", "4.5.0", "4.7.0", "4.8.0", "4.11.0", "4.12.1", "4.14.0", "4.15.0", "4.16.0", ); 

by default main.yml will pass something like

 VERSION: 4.16.2 

the code

 - name: register version check shell: cat /root/versions.js | grep -q {{VERSION}} register: current_version - debug: msg="The registered variable output is {{ current_version.rc }}" - name: append to versions.js lineinfile: dest: /root/versions.js regexp: '^\);' insertbefore: '^#\);' line: " \"{{VERSION}}\",\n);" owner: root state: present when: current_version.rc == 1 

: The debugging message evaluates current_version.rc and shows me booleans based on the output of the grep commands, but I cannot reuse this in the when to determine whether the task should be executed.

Edit: output:

 PLAY [localhost] ************************************************************** GATHERING FACTS *************************************************************** ok: [localhost] TASK: [test | register version check] ***************************************** failed: [localhost] => {"changed": true, "cmd": "cat /root/versions.js | grep -q 3.19.2", "delta": "0:00:00.003570", "end": "2015-12-17 00:24:49.729078", "rc": 1, "start": "2015-12-17 00:24:49.725508", "warnings": []} FATAL: all hosts have already failed -- aborting PLAY RECAP ******************************************************************** to retry, use: --limit @/root/site.retry localhost : ok=1 changed=0 unreachable=0 failed=1 
+8
ansible
source share
1 answer

Like nikobelia mentioned in the comments, grep returns an exit code of 1 if it does not match the lines . Ansible then interprets this (virtually any status code other than 0 from the shell / command task) as an error and therefore quickly fails.

You can tell Ansible to ignore the response code from the shell / command task using ignore_errors . Although with grep this ignores the actual errors (set by return code 2), so you can use failed_when , like this

 - name: register version check shell: cat /root/versions.js | grep -q {{VERSION}} register: current_version failed_when: current_version.rc == 2 
+20
source share

All Articles