Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove duplicate and templated task tags #297

Merged
Show file tree
Hide file tree
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
2 changes: 2 additions & 0 deletions .ansible-lint
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,6 @@ skip_list:
- '602'
- '208'
use_default_rules: true
rulesdir:
- ./rules/
verbosity: 0
63 changes: 63 additions & 0 deletions rules/tag.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
"""Implementation of TagRule."""

from __future__ import annotations

import re
from typing import TYPE_CHECKING

from ansiblelint.constants import LINE_NUMBER_KEY
from ansiblelint.file_utils import Lintable
from ansiblelint.rules import AnsibleLintRule, TransformMixin

if TYPE_CHECKING:
from ansiblelint.errors import MatchError
from ansiblelint.utils import Task


class TagRule(AnsibleLintRule, TransformMixin):
"""Rule for checking task tags."""

id = "tag"
description = (
"All task tags should have distinct names"
"and templates in tags should be avoided."
)
severity = "MEDIUM"
tags = ["idiom"]
_re_templated = re.compile(r"^.*\{\{.*\}\}.*$")
_ids = {
"tag[no-duplicate]": "Tasks should not duplicate tags.",
"tag[no-template]": "Tasks should not use Jinja templates in tags.",
}

def matchtask(
self,
task: Task,
file: Lintable | None = None,
) -> list[MatchError]:
results: list[MatchError] = []
if file and file.failed():
return results
tags = task.get("tags")
if tags:
if len(tags) != len(set(tags)):
results.append(
self.create_matcherror(
message="Tasks should not duplicate tags.",
lineno=task[LINE_NUMBER_KEY],
tag="tag[no-duplicate]",
filename=file,
),
)
for tag in tags:
if self._re_templated.match(tag):
results.append(
self.create_matcherror(
message="Tasks should not use Jinja templates in tags.",
lineno=task[LINE_NUMBER_KEY],
tag="tag[no-template]",
filename=file,
),
)
break
return results
4 changes: 2 additions & 2 deletions tasks/fix-cat2.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2733,7 +2733,6 @@
tags:
- RHEL-08-010830
- CAT2
- V-230330
- CCI-000366
- SRG-OS-000480-GPOS-00229
- SV-230330r858713_rule
Expand Down Expand Up @@ -5973,7 +5972,8 @@
- SV-230505r744020_rule
- V-230505
- firewall
- "{{ rhel8stig_firewall_service }}"
- iptables
- firewalld

- name: "MEDIUM | RHEL-08-040101 | PATCH | A firewall must be active on RHEL 8"
block:
Expand Down