Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 13 additions & 9 deletions docs/custom-rules.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,15 +34,17 @@ Each rule definition should also invoke one of the following methods:
The following is an example rule that uses the `match` method:

```python
from typing import Union
from ansiblelint.rules import AnsibleLintRule


class DeprecatedVariableRule(AnsibleLintRule):
"""Deprecated variable declarations."""

id = 'EXAMPLE002'
description = 'Check for lines that have old style ${var} ' + \
'declarations'
tags = { 'deprecations' }
tags = ['deprecations']

def match(self, line: str) -> Union[bool, str]:
return '${' in line
Expand All @@ -51,32 +53,34 @@ class DeprecatedVariableRule(AnsibleLintRule):
The following is an example rule that uses the `matchtask` method:

```python
from typing import TYPE_CHECKING, Any, Dict, Union
from __future__ import annotations
from typing import TYPE_CHECKING, Union

import ansiblelint.utils
from ansiblelint.rules import AnsibleLintRule

if TYPE_CHECKING:
from ansiblelint.file_utils import Lintable
from ansiblelint.utils import Task


class TaskHasTag(AnsibleLintRule):
"""Tasks must have tag."""

id = 'EXAMPLE001'
description = 'Tasks must have tag'
tags = ['productivity']

def matchtask(self, task: Task, file: 'Lintable' | None = None) -> Union[bool,str]:
def matchtask(self,
task: Task,
file: Lintable
| None = None) -> Union[bool, str]:
# 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']):
if not set(task.keys()).isdisjoint(['include', 'fail']):
return False

# Task should have tags
if not task.has_key('tags'):
return True

if not task.get("tags"):
return True
return False
```

Expand Down