Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Evaluating return code in ansible conditional

Tags:

ansible

I'm working on automating a task which needs to append the latest version of software to a file. I don't want to it do this multiple times for the same version.

It 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", ); 

the defaults main.yml would pass in something like

VERSION: 4.16.2 

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 

problem: the debug message is evaluating current_version.rc and showing me boolean values based on the grep commands output, but I cant re-use this in the when conditional to determine if the task should be run.

Edit: the 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 
like image 278
Bryan Avatar asked Dec 17 '15 17:12

Bryan


People also ask

What is conditions in Ansible?

Ansible supports conditional evaluations before executing a specific task on the target hosts. If the set condition is true, Ansible will go ahead and perform the task. If the condition is not true (unmet), Ansible will skip the specified task. To implement conditions in Ansible, we use the when keyword.

What is RC in Ansible output?

rc. Some modules execute command line utilities or are geared for executing commands directly (raw, shell, command, and so on), this field contains 'return code' of these utilities.


1 Answers

As nikobelia pointed out in the comments, grep returns an exit code of 1 when it doesn't match any lines. Ansible then interprets this (actually any status code other than 0 from a shell/command task) as an error and so promptly fails.

You can tell Ansible to ignore the response code from the shell/command task by using ignore_errors. Although with grep this will ignore actual errors (given by a return code of 2) so instead you might want to 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 
like image 83
ydaetskcoR Avatar answered Sep 26 '22 14:09

ydaetskcoR