Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Ansible: enforce pipefail

Earlier today, we experienced a nasty issue that was caused by the following shell pipe:

- name: get remote branches
  shell: git ls-remote -h [email protected]:orga/repo.git | sed 's_.*refs/heads/__g'
  register: branches_remote

The git command fails, but the return code of the entire pipe is 0. This is default bash/sh behavior.

To fix this, in sh/bash, you can set -o pipefail or set -e. Is it possible to do that in ansible, preferably globally for all my shell commands?

like image 716
Nico Schlömer Avatar asked Sep 27 '22 10:09

Nico Schlömer


1 Answers

In general you should try to use the shell commands as a last resort as they tend to be a bit brittle. If you need to use the shell module with any shell options, simply submit it as part of your command pipeline as shown below. The executable parameter forces the use of bash shell.

[user@ansible ~]$ ansible myhost -m shell -a "executable=/bin/bash set -o pipefail && false | echo hello there"
myhost | FAILED | rc=1 >>
hello there

[user@ansible ~]$ ansible myhost -m shell -a "executable=/bin/bash set -o pipefail && true | echo hello there"
myhost | success | rc=0 >>
hello there
like image 120
Dave Snigier Avatar answered Sep 30 '22 07:09

Dave Snigier