2021-01-30 15:52:48 +00:00
|
|
|
from collections import defaultdict
|
|
|
|
|
2024-01-25 20:40:15 +00:00
|
|
|
from ansiblelater.rule import RuleBase
|
2021-01-30 15:52:48 +00:00
|
|
|
|
|
|
|
|
2024-01-25 20:40:15 +00:00
|
|
|
class CheckUniqueNamedTask(RuleBase):
|
2024-01-27 18:56:35 +00:00
|
|
|
rid = "ANS103"
|
2021-01-30 15:52:48 +00:00
|
|
|
description = "Tasks and handlers must be uniquely named within a single file"
|
2024-01-25 20:40:15 +00:00
|
|
|
helptext = "name `{name}` appears multiple times"
|
2021-01-30 15:52:48 +00:00
|
|
|
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)
|