2021-01-30 15:52:48 +00:00
|
|
|
from ansiblelater.standard import StandardBase
|
|
|
|
|
|
|
|
|
|
|
|
class CheckNativeYaml(StandardBase):
|
|
|
|
|
|
|
|
sid = "LINT0008"
|
|
|
|
description = "Use YAML format for tasks and handlers rather than key=value"
|
|
|
|
helptext = "task arguments appear to be in key value rather than YAML format"
|
|
|
|
version = "0.1"
|
|
|
|
types = ["playbook", "task", "handler"]
|
|
|
|
|
|
|
|
def check(self, candidate, settings):
|
|
|
|
tasks, errors = self.get_action_tasks(candidate, settings)
|
2021-10-10 20:59:52 +00:00
|
|
|
exclude_modules = settings["ansible"]["native-yaml"]["exclude"]
|
2021-01-30 15:52:48 +00:00
|
|
|
|
|
|
|
if not errors:
|
|
|
|
for task in tasks:
|
|
|
|
normal_form, error = self.get_normalized_task(task, candidate, settings)
|
2021-10-10 20:59:52 +00:00
|
|
|
|
2021-01-30 15:52:48 +00:00
|
|
|
if error:
|
|
|
|
errors.extend(error)
|
|
|
|
break
|
|
|
|
|
2021-10-10 20:59:52 +00:00
|
|
|
module = normal_form["action"]["__ansible_module__"]
|
2021-01-30 15:52:48 +00:00
|
|
|
arguments = [
|
2021-06-07 23:48:39 +00:00
|
|
|
bytes(x, "utf-8").decode("utf8", "ignore")
|
2021-01-30 15:52:48 +00:00
|
|
|
for x in normal_form["action"]["__ansible_arguments__"]
|
|
|
|
]
|
2021-10-10 20:59:52 +00:00
|
|
|
|
|
|
|
if module in exclude_modules:
|
|
|
|
continue
|
2021-01-30 15:52:48 +00:00
|
|
|
# Cope with `set_fact` where task["set_fact"] is None
|
2021-10-10 20:59:52 +00:00
|
|
|
if not task.get(module):
|
2021-01-30 15:52:48 +00:00
|
|
|
continue
|
2021-10-10 20:59:52 +00:00
|
|
|
if isinstance(task[module], dict):
|
2021-01-30 15:52:48 +00:00
|
|
|
continue
|
|
|
|
# strip additional newlines off task[action]
|
2021-10-10 20:59:52 +00:00
|
|
|
task_action = bytes(task[module].strip(), "utf-8").decode("utf8", "ignore")
|
2021-06-08 07:24:17 +00:00
|
|
|
if list(filter(lambda a: a != "\\", task_action.split())) != arguments:
|
2021-01-30 15:52:48 +00:00
|
|
|
errors.append(self.Error(task["__line__"], self.helptext))
|
|
|
|
return self.Result(candidate.path, errors)
|