Q:

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 git@bitbucket.org: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? 

A:

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

