ansible-later/ansiblelater/rules/taskfiles.py

33 lines
1.0 KiB
Python
Raw Permalink Normal View History

2019-04-04 14:06:18 +00:00
"""Checks related to ansible task files."""
2018-12-19 10:19:07 +00:00
2019-04-04 14:06:18 +00:00
import re
2018-12-19 10:19:07 +00:00
from collections import defaultdict
from ansiblelater.command.candidates import Error
from ansiblelater.command.candidates import Result
2018-12-19 10:19:07 +00:00
from ansiblelater.utils.rulehelper import get_normalized_yaml
def check_line_between_tasks(candidate, settings):
options = defaultdict(dict)
options.update(remove_empty=False)
options.update(remove_markers=False)
lines, errors = get_normalized_yaml(candidate, settings, options)
description = "missing task separation (required: 1 empty line)"
task_regex = re.compile(r"-\sname:.*")
prevline = "#file_start_marker"
allowed_prevline = ["---", "tasks:", "pre_tasks:", "post_tasks:", "block:"]
if not errors:
for i, line in lines:
2018-12-19 10:19:07 +00:00
match = task_regex.search(line)
if match and prevline:
if not any(item in prevline for item in allowed_prevline):
errors.append(Error(i, description))
2018-12-19 10:19:07 +00:00
prevline = line.strip()
return Result(candidate.path, errors)