2021-01-30 15:52:48 +00:00
|
|
|
import re
|
|
|
|
|
|
|
|
from ansiblelater.standard import StandardBase
|
|
|
|
|
|
|
|
|
|
|
|
class CheckFilterSeparation(StandardBase):
|
|
|
|
|
|
|
|
sid = "ANSIBLE0016"
|
|
|
|
description = "Jinja2 filters should be separated with spaces"
|
|
|
|
helptext = "no suitable numbers of spaces (required: 1)"
|
|
|
|
version = "0.1"
|
2023-06-28 07:14:39 +00:00
|
|
|
types = ["playbook", "task", "handler", "rolevars", "hostvars", "groupvars"]
|
2021-01-30 15:52:48 +00:00
|
|
|
|
|
|
|
def check(self, candidate, settings):
|
|
|
|
yamllines, errors = self.get_normalized_yaml(candidate, settings)
|
|
|
|
|
|
|
|
matches = []
|
|
|
|
braces = re.compile("{{(.*?)}}")
|
2022-12-11 11:43:04 +00:00
|
|
|
filters = re.compile(r"(?<=\|)((\s{2,})*\S+)|(\S+(\s{2,})*)(?=\|)")
|
2021-01-30 15:52:48 +00:00
|
|
|
|
|
|
|
if not errors:
|
|
|
|
for i, line in yamllines:
|
|
|
|
match = braces.findall(line)
|
|
|
|
if match:
|
|
|
|
for item in match:
|
2022-08-01 06:28:10 +00:00
|
|
|
# replace potential regex in filters
|
2022-12-11 11:43:04 +00:00
|
|
|
item = re.sub(r"\(.+\)", "(dummy)", item)
|
2021-01-30 15:52:48 +00:00
|
|
|
matches.append((i, item))
|
|
|
|
|
2022-12-12 08:15:05 +00:00
|
|
|
for i, item in matches:
|
|
|
|
if filters.findall(item):
|
2021-01-30 15:52:48 +00:00
|
|
|
errors.append(self.Error(i, self.helptext))
|
|
|
|
return self.Result(candidate.path, errors)
|