Created
April 26, 2018 11:12
-
-
Save sshnaidm/aa551578229b076b20740c000f40dfd9 to your computer and use it in GitHub Desktop.
Include pipefail in ansible shell command if there is pipe
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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