feat: add config option to exclude modules from native yaml check (#236)

This commit is contained in:
Robert Kaussow 2021-10-10 22:59:52 +02:00 committed by GitHub
parent b0271ef29b
commit 440aaea5b1
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 24 additions and 11 deletions

View File

@ -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))
)

View File

@ -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__"]
]
# Cope with `set_fact` where task["set_fact"] is None
if not task.get(action):
if module in exclude_modules:
continue
if isinstance(task[action], dict):
# Cope with `set_fact` where task["set_fact"] is None
if not task.get(module):
continue
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)

View File

@ -148,6 +148,9 @@ class Settings(object):
"include_vars",
],
},
"native-yaml": {
"exclude": [],
},
},
"yamllint": {
"empty-lines": {

View File

@ -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`

View File

@ -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 |