|
1 | 1 | #!/usr/bin/env python |
2 | 2 |
|
| 3 | +from __future__ import print_function |
| 4 | + |
| 5 | +import os |
| 6 | +import subprocess |
| 7 | +import re |
3 | 8 | import sys |
| 9 | + |
4 | 10 | from lib.ansible_base import AnsibleBaseRunner |
5 | 11 |
|
6 | 12 | __all__ = [ |
@@ -38,6 +44,52 @@ class AnsiblePlaybookRunner(AnsibleBaseRunner): |
38 | 44 | def __init__(self, *args, **kwargs): |
39 | 45 | super(AnsiblePlaybookRunner, self).__init__(*args, **kwargs) |
40 | 46 |
|
| 47 | + def handle_json_arg(self): |
| 48 | + os.environ['ANSIBLE_CALLBAKC_PLUGIN'] = 'json' |
| 49 | + self.stdout = subprocess.PIPE |
| 50 | + # TODO: --json is probably not be compatible with other options |
| 51 | + # like syntax-check, verbose, and possibly others |
| 52 | + |
| 53 | + def popen_call(self, p): |
| 54 | + """ |
| 55 | + :param subprocess.Popen p: |
| 56 | + :return: |
| 57 | + """ |
| 58 | + if not self.json_output: |
| 59 | + return super(AnsiblePlaybookRunner, self).popen_call(p) |
| 60 | + |
| 61 | + # lines that should go to stderr instead of stdout |
| 62 | + stderr_lines = re.compile( |
| 63 | + r'(' |
| 64 | + |
| 65 | + # these were identified in this closed PR: |
| 66 | + # https://github.com/ansible/ansible/pull/17448/files |
| 67 | + # see https://github.com/ansible/ansible/issues/17122 |
| 68 | + r"^\tto retry, use: --limit @.*$" |
| 69 | + r'|' |
| 70 | + r"^skipping playbook include '.*' due to conditional test failure$" |
| 71 | + r'|' |
| 72 | + r"^statically included: .*$" |
| 73 | + |
| 74 | + # other possibilities: |
| 75 | + # r'|' |
| 76 | + # r'^to see the full traceback, use -vvv$' |
| 77 | + # r'|' |
| 78 | + # r"^The full traceback was:$" |
| 79 | + # but then I would somehow have to include the traceback as well... |
| 80 | + |
| 81 | + r')' |
| 82 | + ) |
| 83 | + |
| 84 | + while p.poll() is None: |
| 85 | + line = p.stdout.readline() |
| 86 | + if re.match(stderr_lines, line): |
| 87 | + print(line, file=sys.stderr) |
| 88 | + else: |
| 89 | + print(line) |
| 90 | + |
| 91 | + return p.returncode |
| 92 | + |
41 | 93 |
|
42 | 94 | if __name__ == '__main__': |
43 | 95 | AnsiblePlaybookRunner(sys.argv).execute() |
0 commit comments