summaryrefslogtreecommitdiffstats
path: root/examples/rules
diff options
context:
space:
mode:
Diffstat (limited to 'examples/rules')
-rw-r--r--examples/rules/TaskHasTag.py37
1 files changed, 37 insertions, 0 deletions
diff --git a/examples/rules/TaskHasTag.py b/examples/rules/TaskHasTag.py
new file mode 100644
index 0000000..58dfa7c
--- /dev/null
+++ b/examples/rules/TaskHasTag.py
@@ -0,0 +1,37 @@
+"""Example implementation of a rule requiring tasks to have tags set."""
+from ansiblelint.rules import AnsibleLintRule
+
+
+class TaskHasTag(AnsibleLintRule):
+ """Tasks must have tag."""
+
+ id = 'EXAMPLE001'
+ shortdesc = 'Tasks must have tag'
+ description = 'Tasks must have tag'
+ tags = ['productivity', 'tags']
+
+ def matchtask(self, file, task):
+ """Task matching method."""
+ # The meta files don't have tags
+ if file['type'] in ["meta", "playbooks"]:
+ return False
+
+ if isinstance(task, str):
+ return False
+
+ # If the task include another task or make the playbook fail
+ # Don't force to have a tag
+ if not set(task.keys()).isdisjoint(['include', 'fail']):
+ return False
+
+ if not set(task.keys()).isdisjoint(['include_tasks', 'fail']):
+ return False
+
+ if not set(task.keys()).isdisjoint(['import_tasks', 'fail']):
+ return False
+
+ # Task should have tags
+ if 'tags' not in task:
+ return True
+
+ return False