Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Ansible error on shell command returning zero

Tags:

ansible

Ansible doesn't seem to be able to handle the result '0' for shell commands. This

- name: Check if swap exists
  shell: "swapon -s | grep -ci dev"
  register: swap_exists

Returns an error

"msg": "non-zero return code"

But when I replace "dev" with "type", which actually always occurs and gives a count of at least 1, then the command is successful and no error is thrown.

I also tried with command: instead of shell: - it doesn't give an error, but then the command is also not executed.

like image 350
user2066480 Avatar asked May 21 '18 00:05

user2066480


People also ask

What is non zero return code?

Non-zero return codes are normally used for failure cases with a 0 return code used for a successful command so this can give the impression the command has failed when it has not.

How do you control command failure in Ansible?

Ignoring failed commands By default Ansible stops executing tasks on a host when a task fails on that host. You can use ignore_errors to continue on in spite of the failure. The ignore_errors directive only works when the task is able to run and returns a value of 'failed'.

How do you handle non zero return codes in Ansible?

We can manipulate the exit status of the task by registering the return value to a variable and then use conditional to determine if the task fails or succeeds. To continue the playbook, in spite of the failure, we can use the ignore_errors option on the task.

What is Changed_when in Ansible?

Ansible changed_when property or parameters is defined to deal with the output of the specific task once a task is triggered on the remote node and based on the return code or the output, we can determine whether the task should be reported in the ansible statistic or need to use the trigger to handle the condition and ...


2 Answers

since you want to run a sequence of commands that involve pipe, ansible states you should use shell and not command, as you are doing.

So, the problem is the fact that grep returns 1 (didnt find a match on the swapon output), and ansible considers this a failure. Since you are well sure there is no issue, just add a ignore_errors: true and be done with it.

- name: Check if swap exists
  shell: "swapon -s | grep -ci non_existent_string"
  register: swap_exists
  ignore_errors: true

OR:

if you want to narrow it down to return codes 0 and 1, instruct ansible to not consider failures those 2 rcs:

- name: Check if swap exists
  shell: "swapon -s | grep -ci non_existent_string"
  register: swap_exists
  # ignore_errors: true
  failed_when: swap_exists.rc != 1 and swap_exists.rc != 0
like image 58
ilias-sp Avatar answered Oct 14 '22 09:10

ilias-sp


I found a better way. if you only need to know the record number this works:

- name: Check if swap exists
  shell: "swapon -s | grep -i dev|wc -l"
  register: swap_exists

Another way is to always use cat at the end of the pipe. See Ansible shell module returns error when grep results are empty

    - name: Check if swap exists
      shell: "swapon -s | grep -i dev|cat"
      register: swap_exists
like image 39
robin Avatar answered Oct 14 '22 10:10

robin