2021-01-30 15:52:48 +00:00
|
|
|
import re
|
|
|
|
|
2024-01-25 20:40:15 +00:00
|
|
|
from ansiblelater.rule import RuleBase
|
2021-01-30 15:52:48 +00:00
|
|
|
|
|
|
|
|
2024-01-25 20:40:15 +00:00
|
|
|
class CheckLiteralBoolFormat(RuleBase):
|
2024-01-27 17:32:29 +00:00
|
|
|
rid = "ANS114"
|
2021-10-07 20:59:39 +00:00
|
|
|
description = "Literal bools should be consistent"
|
|
|
|
helptext = "literal bools should be written as `{bools}`"
|
2021-01-30 15:52:48 +00:00
|
|
|
types = ["playbook", "task", "handler", "rolevars", "hostvars", "groupvars"]
|
|
|
|
|
|
|
|
def check(self, candidate, settings):
|
|
|
|
yamllines, errors = self.get_normalized_yaml(candidate, settings)
|
|
|
|
|
2021-10-07 20:59:39 +00:00
|
|
|
litera_bools = re.compile(r"(?:[=!]=|:)\s*(true|false|yes|no|on|off)\s*$", re.IGNORECASE)
|
|
|
|
allowed = settings["ansible"]["literal-bools"]
|
2021-01-30 15:52:48 +00:00
|
|
|
|
|
|
|
if not errors:
|
|
|
|
for i, line in yamllines:
|
2021-10-07 20:59:39 +00:00
|
|
|
matches = litera_bools.findall(line)
|
|
|
|
if any(m not in allowed for m in matches):
|
|
|
|
errors.append(self.Error(i, self.helptext.format(bools=", ".join(allowed))))
|
2021-01-30 15:52:48 +00:00
|
|
|
|
|
|
|
return self.Result(candidate.path, errors)
|