feat: add rule CheckWhenFormat

This commit is contained in:
Robert Kaussow 2021-01-31 14:22:48 +01:00
parent 9e004825d8
commit f70f0ca1f1
No known key found for this signature in database
GPG Key ID: 65362AE74AF98B61
2 changed files with 31 additions and 1 deletions

View File

@ -0,0 +1,29 @@
from ansiblelater.standard import StandardBase
class CheckWhenFormat(StandardBase):
sid = "ANSIBLE0022"
description = "Don't use Jinja2 in when"
helptext = (
"`when` is a raw Jinja2 expression, redundant {{ }} "
"should be removed from variable(s)"
)
version = "0.2"
types = ["playbook", "task", "handler"]
def check(self, candidate, settings):
tasks, errors = self.get_normalized_tasks(candidate, settings)
if not errors:
for task in tasks:
if 'when' in task and not self._is_valid(task['when']):
errors.append(self.Error(task["__line__"], self.helptext))
return self.Result(candidate.path, errors)
@staticmethod
def _is_valid(when):
if not isinstance(when, str):
return True
return when.find('{{') == -1 and when.find('}}') == -1

View File

@ -29,10 +29,11 @@ Reviews are nothing without some rules or standards against which to review. ans
| CheckCompareToEmptyString | ANSIBLE0012 | Don't compare to "" - use `when: var` or `when: not var`. | |
| CheckCompareToLiteralBool | ANSIBLE0013 | Don't compare to True/False - use `when: var` or `when: not var`. | |
| CheckLiteralBoolFormat | ANSIBLE0014 | Literal bools should be written as `True/False` or `yes/no`. | forbidden values are `true false TRUE FALSE Yes No YES NO` |
| CheckBecomeUser | ANSIBLE0015 | `become` should be always used combined with `become_user`. | |
| CheckBecomeUser | ANSIBLE0015 | Become should be combined with become_user. | |
| CheckFilterSeparation | ANSIBLE0016 | Jinja2 filters should be separated with spaces. | |
| CheckCommandInsteadOfArgument | ANSIBLE0017 | Commands should not be used in place of module arguments. | |
| CheckFilePermissionMissing | ANSIBLE0018 | File permissions unset or incorrect. | |
| CheckFilePermissionOctal | ANSIBLE0019 | Octal file permissions must contain leading zero or be a string. | |
| CheckGitHasVersion | ANSIBLE0020 | Git checkouts should use explicit version. | |
| CheckMetaChangeFromDefault | ANSIBLE0021 | Roles meta/main.yml default values should be changed. | |
| CheckWhenFormat | ANSIBLE0022 | Don't use Jinja2 in `when`. | |