Skip to content

Instantly share code, notes, and snippets.

@sshnaidm
Created April 26, 2018 11:12
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sshnaidm/aa551578229b076b20740c000f40dfd9 to your computer and use it in GitHub Desktop.
Save sshnaidm/aa551578229b076b20740c000f40dfd9 to your computer and use it in GitHub Desktop.
Include pipefail in ansible shell command if there is pipe
from ansiblelint import AnsibleLintRule
def uncorrect_task(task, cmd):
if 'shell' not in task:
return False
if 'register' in task:
return False
if task.get('ignore_errors'):
return False
if isinstance(task['shell'], dict):
args = task['shell']['cmd'].split()
else:
args = task['shell'].split()
if not set(args).isdisjoint(cmd) and 'pipefail' not in args:
return True
return False
class ShellPipefail(AnsibleLintRule):
id = 'OOOQ0001'
shortdesc = 'Shell should have a pipefail'
description = 'Shell commands should have "set -o pipefail" if using PIPE'
tags = ['shell']
cmd = ["|", "timestamper_cmd"]
def matchplay(self, file, play):
ret = []
if play.get('block') and not play.get('ignore_errors'):
block = play['block']
for task in block:
if uncorrect_task(task, self.cmd):
ret.append((file, self.shortdesc))
else:
if uncorrect_task(play, self.cmd):
ret.append((file, self.shortdesc))
return ret
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment