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

Add ability to ignore jinja2 templates #1494

Merged
merged 1 commit into from Mar 25, 2021
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 4 additions & 0 deletions .pre-commit-config.yaml
Expand Up @@ -46,6 +46,10 @@ repos:
rev: v1.26.0
hooks:
- id: yamllint
exclude: >
(?x)^(
examples/other/some.j2.yaml
)$
files: \.(yaml|yml)$
types: [file, yaml]
entry: yamllint --strict
Expand Down
2 changes: 2 additions & 0 deletions examples/other/some.j2.yaml
@@ -0,0 +1,2 @@
# Used to validate that a templated YAML file does not confuse the linter
{% include 'port.j2' %}
6 changes: 6 additions & 0 deletions src/ansiblelint/config.py
Expand Up @@ -13,6 +13,8 @@

DEFAULT_KINDS = [
# Do not sort this list, order matters.
{"jinja2": "**/*.j2"}, # jinja2 templates are not always parsable as something else
{"jinja2": "**/*.j2.*"},
{"requirements": "**/meta/requirements.yml"}, # v1 only
# https://docs.ansible.com/ansible/latest/dev_guide/collections_galaxy_meta.html
{"galaxy": "**/galaxy.yml"}, # Galaxy collection meta
Expand Down Expand Up @@ -40,6 +42,10 @@
# These assignations are only for internal use and are only inspired by
# MIME/IANA model. Their purpose is to be able to process a file based on
# it type, including generic processing of text files using the prefix.
{
"text/jinja2": "**/*.j2"
}, # jinja2 templates are not always parsable as something else
{"text/jinja2": "**/*.j2.*"},
{"text/json": "**/*.json"}, # standardized
{"text/markdown": "**/*.md"}, # https://tools.ietf.org/html/rfc7763
{"text/rst": "**/*.rst"}, # https://en.wikipedia.org/wiki/ReStructuredText
Expand Down
8 changes: 6 additions & 2 deletions src/ansiblelint/rules/__init__.py
Expand Up @@ -89,7 +89,11 @@ def matchlines(self, file: "Lintable") -> List[MatchError]:
# https://github.com/ansible-community/ansible-lint/issues/744
def matchtasks(self, file: Lintable) -> List[MatchError]:
matches: List[MatchError] = []
if not self.matchtask or file.kind not in ['handlers', 'tasks', 'playbook']:
if (
not self.matchtask
or file.kind not in ['handlers', 'tasks', 'playbook']
or file.base_kind != 'text/yaml'
):
return matches

yaml = ansiblelint.utils.parse_yaml_linenumbers(file)
Expand Down Expand Up @@ -136,7 +140,7 @@ def _matchplay_linenumber(play, optional_linenumber):

def matchyaml(self, file: Lintable) -> List[MatchError]:
matches: List[MatchError] = []
if not self.matchplay:
if not self.matchplay or file.base_kind != 'text/yaml':
return matches

yaml = ansiblelint.utils.parse_yaml_linenumbers(file)
Expand Down
1 change: 1 addition & 0 deletions src/ansiblelint/utils.py
Expand Up @@ -737,6 +737,7 @@ def construct_mapping(node, deep=False):
loader.construct_mapping = construct_mapping
data = loader.get_single_data()
except (yaml.parser.ParserError, yaml.scanner.ScannerError) as e:
logging.exception(e)
raise SystemExit("Failed to parse YAML in %s: %s" % (lintable.path, str(e)))
return data

Expand Down
1 change: 1 addition & 0 deletions test/TestExamples.py
Expand Up @@ -64,3 +64,4 @@ def test_custom_kinds():
# .yaml-too is not a recognized extension and unless is manually defined
# in our .ansible-lint config, the test would not identify it as yaml file.
assert "Examining examples/other/some.yaml-too of type yaml" in result.stderr
assert "Examining examples/other/some.j2.yaml of type jinja2" in result.stderr
4 changes: 4 additions & 0 deletions test/TestUtils.py
Expand Up @@ -365,6 +365,10 @@ def test_is_playbook():
"tasks",
), # relative path involved
("galaxy.yml", "galaxy"),
("foo.j2.yml", "jinja2"),
("foo.yml.j2", "jinja2"),
("foo.j2.yaml", "jinja2"),
("foo.yaml.j2", "jinja2"),
),
)
def test_default_kinds(monkeypatch, path: str, kind: FileType) -> None:
Expand Down