From a21f599cad7a07cef02c2a9415514227a92b3ddd Mon Sep 17 00:00:00 2001 From: Robert Kaussow Date: Sun, 31 Jan 2021 22:24:33 +0100 Subject: [PATCH] feat: add rule CheckRelativeRolePaths --- ansiblelater/rules/CheckRelativeRolePaths.py | 34 ++++++++++++++++++++ docs/content/included_rules/_index.md | 1 + 2 files changed, 35 insertions(+) create mode 100644 ansiblelater/rules/CheckRelativeRolePaths.py diff --git a/ansiblelater/rules/CheckRelativeRolePaths.py b/ansiblelater/rules/CheckRelativeRolePaths.py new file mode 100644 index 0000000..399a6b9 --- /dev/null +++ b/ansiblelater/rules/CheckRelativeRolePaths.py @@ -0,0 +1,34 @@ +# Copyright (c) 2016, Tsukinowa Inc. +# Copyright (c) 2018, Ansible Project +from ansiblelater.standard import StandardBase + + +class CheckRelativeRolePaths(StandardBase): + + sid = "ANSIBLE0025" + description = "Don't use a relative path in a role" + helptext = "`copy` and `template` modules don't need relative path for `src`" + version = "0.2" + types = ["playbook", "task", "handler"] + + def check(self, candidate, settings): + tasks, errors = self.get_normalized_tasks(candidate, settings) + module_to_path_folder = { + "copy": "files", + "win_copy": "files", + "template": "templates", + "win_template": "win_templates", + } + + if not errors: + for task in tasks: + module = task["action"]["__ansible_module__"] + path_to_check = None + + if module in module_to_path_folder and "src" in task["action"]: + path_to_check = "../{}".format(module_to_path_folder[module]) + + if path_to_check and path_to_check in task["action"]["src"]: + errors.append(self.Error(task["__line__"], self.helptext)) + + return self.Result(candidate.path, errors) diff --git a/docs/content/included_rules/_index.md b/docs/content/included_rules/_index.md index 09cb2d2..1f8184a 100644 --- a/docs/content/included_rules/_index.md +++ b/docs/content/included_rules/_index.md @@ -39,3 +39,4 @@ Reviews are nothing without some rules or standards against which to review. ans | CheckWhenFormat | ANSIBLE0022 | Don't use Jinja2 in `when`. | | | CheckNestedJinja | ANSIBLE0023 | Don't use nested Jinja2 pattern. | | | CheckLocalAction | ANSIBLE0024 | Don't use local_action. | | +| CheckRelativeRolePaths | ANSIBLE0025 | Don't use a relative path in a role. | |