From 440aaea5b13886037acdcee7b50be0f458cd0f56 Mon Sep 17 00:00:00 2001 From: Robert Kaussow Date: Sun, 10 Oct 2021 22:59:52 +0200 Subject: [PATCH] feat: add config option to exclude modules from native yaml check (#236) --- ansiblelater/rules/CheckNamedTask.py | 4 ++-- ansiblelater/rules/CheckNativeYaml.py | 13 +++++++++---- ansiblelater/settings.py | 3 +++ docs/content/configuration/defaults.md | 11 ++++++++--- docs/content/included_rules/_index.md | 2 +- 5 files changed, 23 insertions(+), 10 deletions(-) diff --git a/ansiblelater/rules/CheckNamedTask.py b/ansiblelater/rules/CheckNamedTask.py index 5b2f2ee..f100097 100644 --- a/ansiblelater/rules/CheckNamedTask.py +++ b/ansiblelater/rules/CheckNamedTask.py @@ -11,12 +11,12 @@ class CheckNamedTask(StandardBase): def check(self, candidate, settings): tasks, errors = self.get_normalized_tasks(candidate, settings) - nameless_tasks = settings["ansible"]["named-task"]["exclude"] + exclude_modules = settings["ansible"]["named-task"]["exclude"] if not errors: for task in tasks: module = task["action"]["__ansible_module__"] - if ("name" not in task or not task["name"]) and module not in nameless_tasks: + if ("name" not in task or not task["name"]) and module not in exclude_modules: errors.append( self.Error(task["__line__"], self.helptext.format(module=module)) ) diff --git a/ansiblelater/rules/CheckNativeYaml.py b/ansiblelater/rules/CheckNativeYaml.py index 2ae3eb0..75554f8 100644 --- a/ansiblelater/rules/CheckNativeYaml.py +++ b/ansiblelater/rules/CheckNativeYaml.py @@ -11,26 +11,31 @@ class CheckNativeYaml(StandardBase): def check(self, candidate, settings): tasks, errors = self.get_action_tasks(candidate, settings) + exclude_modules = settings["ansible"]["native-yaml"]["exclude"] if not errors: for task in tasks: normal_form, error = self.get_normalized_task(task, candidate, settings) + if error: errors.extend(error) break - action = normal_form["action"]["__ansible_module__"] + module = normal_form["action"]["__ansible_module__"] arguments = [ bytes(x, "utf-8").decode("utf8", "ignore") for x in normal_form["action"]["__ansible_arguments__"] ] + + if module in exclude_modules: + continue # Cope with `set_fact` where task["set_fact"] is None - if not task.get(action): + if not task.get(module): continue - if isinstance(task[action], dict): + if isinstance(task[module], dict): continue # strip additional newlines off task[action] - task_action = bytes(task[action].strip(), "utf-8").decode("utf8", "ignore") + task_action = bytes(task[module].strip(), "utf-8").decode("utf8", "ignore") if list(filter(lambda a: a != "\\", task_action.split())) != arguments: errors.append(self.Error(task["__line__"], self.helptext)) return self.Result(candidate.path, errors) diff --git a/ansiblelater/settings.py b/ansiblelater/settings.py index d60309b..ffc5f61 100644 --- a/ansiblelater/settings.py +++ b/ansiblelater/settings.py @@ -148,6 +148,9 @@ class Settings(object): "include_vars", ], }, + "native-yaml": { + "exclude": [], + }, }, "yamllint": { "empty-lines": { diff --git a/docs/content/configuration/defaults.md b/docs/content/configuration/defaults.md index 567cd59..cc6302f 100644 --- a/docs/content/configuration/defaults.md +++ b/docs/content/configuration/defaults.md @@ -28,9 +28,9 @@ ansible: - "yes" - "no" - # List of tasks that don't need to be named (ANSIBLE0006). - # You have to specify every single task type, globs or wildcard will not work! - named-task + # List of modules that don't need to be named (ANSIBLE0006). + # You must specify each individual module name, globs or wildcards do not work! + named-task: exclude: - "meta" - "debug" @@ -41,6 +41,11 @@ ansible: - "import_role" - "import_tasks" + # List of modules that are allowed to use the key=value format instead of the native YAML format (LINT0008). + # You must specify each individual module name, globs or wildcards do not work! + native-yaml: + exclude: [] + # Global logging configuration # If you would like to force colored output (e.g. non-tty) # set environment variable `PY_COLORS=1` diff --git a/docs/content/included_rules/_index.md b/docs/content/included_rules/_index.md index 984eedb..3047a48 100644 --- a/docs/content/included_rules/_index.md +++ b/docs/content/included_rules/_index.md @@ -13,7 +13,7 @@ Reviews are useless without some rules or standards to check against. ansible-la | CheckYamlColons | LINT0005 | YAML should use consistent number of spaces around colons. | {colons: {max-spaces-before: 0, max-spaces-after: 1}} | | CheckYamlFile | LINT0006 | Roles file should be in YAML format. | | | CheckYamlHasContent | LINT0007 | Files should contain useful content. | | -| CheckNativeYaml | LINT0008 | Use YAML format for tasks and handlers rather than key=value. | | +| CheckNativeYaml | LINT0008 | Use YAML format for tasks and handlers rather than key=value. | {native-yaml: {exclude: []}} | | CheckYamlDocumentEnd | LINT0009 | YAML should contain document end marker. | {document-end: {present: true}} | | CheckLineBetweenTasks | ANSIBLE0001 | Single tasks should be separated by an empty line. | | | CheckMetaMain | ANSIBLE0002 | Meta file should contain a basic subset of parameters. | author, description, min_ansible_version, platforms, dependencies |