You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I created a little script to wrap ansible-review and post-process the output. This was harder than it might have been, for two reasons:
ansible-review splits the output for some things over two lines, like this:
WARN: Best practice "Playbooks should not contain logic (vars, tasks, handlers)" not met:
ansible/playbook.yml:10: [EXTRA0008] tasks should not be required in a play
.. but not others. Most parsing tools/code/logic etc... is simpler line-by-line, so this makes it more awkward. I ended up simplifying my life by joining them back together using sed:
WARN: Best practice "Same variable defined in siblings" not met:
ansible/group_vars/test.yml:Inventory is broken:
Attempted to read "/home/duncan/dev/tools/provisioning/ansible/ansible.log" as ini file:
/home/duncan/dev/tools/provisioning/ansible/ansible.log:1:
Expected key=value host variable assignment, got: 16:47:41,708
it goes off the rails after the filename, putting Inventory is broken where the line number would go - i.e. matching the regex, but putting the "wrong" values in.
I could use some extra code to detect these different messages, but in the case of couldn't classify & the #21 messages, they're basically problems with ansible-review (from my point of view), so I just filtered them out instead.
Here's the script, if you're interested:
"""Post process the output from Ansible Review.This script runs Ansible Review, captures the output, thenpost-processes it into a Github formated markdown Checklist, whichit then outputs to stdout.It uses the blessed module for output, so it has nice formattingin the console, but is pipe aware, so you don't get control charsin piped output."""importosimportreimportsubprocessfromblessedimportTerminalfromoperatorimportitemgetter# Get the output from ansible-review, and collapse the two-line output into one line:# when a line ends in `met:`, the next line is a continuation, so collapse into one line# Skip group_vars - this outputs a non-standard msg about inventory file being broken# see here: https://github.com/willthames/ansible-review/issues/21cmd=r"git ls-files ansible/. | grep -v group_vars | xargs ansible-review 2>/dev/null | sed -n '/met:/ {N; s/[\r\n]/ /g; p}'"try:
output=subprocess.check_output(
cmd,
stderr=subprocess.STDOUT,
shell=True
).split('\n')
exceptsubprocess.CalledProcessErrorase:
output=e.output.split('\n')
t=Terminal()
output_lines= []
classWarningLine:
regex=r'^WARN: (.*) not met: (.*?):(.*?): (.*)$'text, filename, line, rule=range(1, 5)
forlineinoutput:
# Convert the string line into an output_item dict, then add# it to the output_lines list.ifline.startswith('WARN: '):
# It's a warning itemmatch=re.search(WarningLine.regex, line)
ifmatchandlen(match.groups()) ==4:
output_lines.append({
'log_level': t.yellow('WARN'),
'file': match.group(WarningLine.filename),
'line': int(match.group(WarningLine.line)),
'text': match.group(WarningLine.text),
'rule': match.group(WarningLine.rule),
})
# The output from ansible-review is sorted by filename, but it's nicer# if we sort it by filename & line_nosorted_output=sorted(output_lines, key=itemgetter('file', 'line'))
## Print the output_lines list, to the console, in markdown format#print(t.dim('# ') +t.bold('Ansible Review Checklist'))
curr_file=''forlineinsorted_output:
ifline['file'] !=curr_file:
curr_file=line['file']
print(t.dim('\n## ') +t.bold(curr_file))
printt.dim('- []') +' {l[log_level]}: Line: {l[line]:>3}: {l[text]}, rule: {l[rule]}'.format(l=line)
and here's an example of the console output:
which can be piped to a file, then opened in a markdown editor that understands github formatted markdown (like Abricotine) and used as a checklist:
I did attempt to get ansible-review to run from source, and add this as a feature (where it would probably be simpler to implement) - but couldn't figure out how to do that: #20
The text was updated successfully, but these errors were encountered:
I created a little script to wrap
ansible-review
and post-process the output. This was harder than it might have been, for two reasons:ansible-review
splits the output for some things over two lines, like this:.. but not others. Most parsing tools/code/logic etc... is simpler line-by-line, so this makes it more awkward. I ended up simplifying my life by joining them back together using
sed
:$ git ls-files ansible/. | xargs ansible-review 2>/dev/null | sed -n '/met:/ {N; s/[\r\n]/ /g; p}'
This has a (mostly happy) side effect of removing several "non-standard" (see below) messages, like this:
However, the message output in #21 doesn't:
it goes off the rails after the filename, putting
Inventory is broken
where the line number would go - i.e. matching the regex, but putting the "wrong" values in.I could use some extra code to detect these different messages, but in the case of
couldn't classify
& the #21 messages, they're basically problems withansible-review
(from my point of view), so I just filtered them out instead.Here's the script, if you're interested:
and here's an example of the console output:
which can be piped to a file, then opened in a markdown editor that understands github formatted markdown (like Abricotine) and used as a checklist:
I did attempt to get
ansible-review
to run from source, and add this as a feature (where it would probably be simpler to implement) - but couldn't figure out how to do that: #20The text was updated successfully, but these errors were encountered: