2021-01-30 15:52:48 +00:00
|
|
|
from collections import defaultdict
|
|
|
|
|
2024-01-25 20:09:05 +00:00
|
|
|
from ansiblelater.rule import RuleBase
|
2021-01-30 15:52:48 +00:00
|
|
|
|
|
|
|
|
2024-01-25 20:09:05 +00:00
|
|
|
class CheckUniqueNamedTask(RuleBase):
|
2021-01-30 15:52:48 +00:00
|
|
|
sid = "ANSIBLE0003"
|
|
|
|
description = "Tasks and handlers must be uniquely named within a single file"
|
|
|
|
helptext = "name '{name}' appears multiple times"
|
|
|
|
types = ["playbook", "task", "handler"]
|
|
|
|
|
|
|
|
def check(self, candidate, settings):
|
|
|
|
tasks, errors = self.get_normalized_tasks(candidate, settings)
|
|
|
|
|
|
|
|
namelines = defaultdict(list)
|
|
|
|
|
|
|
|
if not errors:
|
|
|
|
for task in tasks:
|
|
|
|
if "name" in task:
|
|
|
|
namelines[task["name"]].append(task["__line__"])
|
2023-11-10 13:50:48 +00:00
|
|
|
for name, lines in namelines.items():
|
2021-01-30 15:52:48 +00:00
|
|
|
if name and len(lines) > 1:
|
|
|
|
errors.append(self.Error(lines[-1], self.helptext.format(name=name)))
|
|
|
|
|
|
|
|
return self.Result(candidate.path, errors)
|