Skip to content

Commit 899f44c

Browse files
authored
Document pre-commit access to ansible community bundle (#3856)
1 parent dbcd960 commit 899f44c

6 files changed

Lines changed: 81 additions & 6 deletions

File tree

.config/dictionary.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -292,6 +292,7 @@ pyupgrade
292292
pyyaml
293293
redirections
294294
reexec
295+
reformatter
295296
regexes
296297
releasenotes
297298
relpath

docs/_autofix_rules.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,3 +8,4 @@
88
- [no-jinja-when](rules/no-jinja-when.md)
99
- [no-log-password](rules/no-log-password.md)
1010
- [partial-become](rules/partial-become.md)
11+
- [yaml](rules/yaml.md)

docs/configuring.md

Lines changed: 38 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,17 +57,54 @@ Keep in mind that this will override any existing file content.
5757

5858
## Pre-commit setup
5959

60-
To use Ansible-lint with [pre-commit], add the following to the
60+
To use Ansible-lint with the [pre-commit] tool, add the following to the
6161
`.pre-commit-config.yaml` file in your local repository.
6262

63+
Do not confuse the [pre-commit] tool with the git hook feature that has the same name.
64+
While the [pre-commit] tool can also make use of git hooks, it does not require
65+
them and it does not install them by default.
66+
67+
[pre-commit.ci] is a hosted service that can run pre-commit for you
68+
on each change but you can also run the tool yourself using the CI of your choice.
69+
6370
Change **rev:** to either a commit sha or tag of Ansible-lint that contains
6471
`.pre-commit-hooks.yaml`.
6572

6673
```yaml
74+
---
75+
ci:
76+
# This section is specific to pre-commit.ci, telling it to create a pull request
77+
# to update the linter version tag every month.
78+
autoupdate_schedule: monthly
79+
# If you have other Ansible collection dependencies (requirements.yml)
80+
# `pre-commit.ci` will not be able to install them because it runs in offline mode,
81+
# and you will need to tell it to skip the hook.
82+
# skip:
83+
# - ansible-lint
84+
repos:
6785
- repo: https://github.com/ansible/ansible-lint
6886
rev: ... # put latest release tag from https://github.com/ansible/ansible-lint/releases/
6987
hooks:
7088
- id: ansible-lint
89+
# Uncomment if you need the full Ansible community bundle instead of ansible-core:
90+
# additional_dependencies:
91+
# - ansible
7192
```
7293

94+
!!! warning
95+
96+
[pre-commit] always uses python virtual environments. If you happen to
97+
use the [Ansible package] instead of just [ansible-core] you might be surprised
98+
to see that pre-commit is not able to find these collections, even if
99+
your local Ansible does. This is because they are installed inside a location
100+
that is not available to the virtual environment in which pre-commit hook
101+
is installed. In this case, you might want to uncomment the commented lines
102+
from the hook definition above, so the bundle will be installed.
103+
104+
You should note that collection installed into `~/.ansible` are found by
105+
the hook.
106+
73107
[pre-commit]: https://pre-commit.com/
108+
[Ansible package]: https://pypi.org/project/ansible/
109+
[ansible-core]: https://pypi.org/project/ansible-core/
110+
[pre-commit.ci]: https://pre-commit.ci/

src/ansiblelint/rules/yaml.md

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,24 @@
11
# yaml
22

3-
This rule checks YAML syntax and is an implementation of `yamllint`.
3+
This rule checks YAML syntax by using [yamllint] but with few minor default
4+
configuration changes.
5+
6+
!!! warning
7+
8+
[Auto-fix](../autofix.md) functionality will change **inline comment indentation to one
9+
character instead of two**, which is the default of [yamllint]. The reason
10+
for this decision is for keeping reformatting compatibility
11+
with [prettier], which is the most popular reformatter.
12+
13+
```yaml title=".yamllint"
14+
rules:
15+
comments:
16+
min-spaces-from-content: 1 # prettier compatibility
17+
```
18+
19+
There is no need to create this yamllint config file, but if you also
20+
run yamllint yourself, you might want to create it to make it behave
21+
the same way as ansible-lint.
422

523
You can disable YAML syntax violations by adding `yaml` to the `skip_list` in
624
your Ansible-lint configuration as follows:
@@ -102,3 +120,5 @@ bar: ... # Correct comment indentation.
102120
[1.2.2]: https://yaml.org/spec/1.2.2/
103121
[yaml specification]: https://yaml.org/
104122
[guide]: https://docs.ansible.com/ansible/latest/reference_appendices/YAMLSyntax.html#yaml-basics
123+
[prettier]: https://prettier.io/
124+
[yamllint]: https://yamllint.readthedocs.io/en/stable/

src/ansiblelint/rules/yaml_rule.py

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,14 @@
33

44
import logging
55
import sys
6-
from collections.abc import Iterable
6+
from collections.abc import Iterable, MutableMapping, MutableSequence
77
from typing import TYPE_CHECKING
88

99
from yamllint.linter import run as run_yamllint
1010

1111
from ansiblelint.constants import LINE_NUMBER_KEY, SKIPPED_RULES_KEY
1212
from ansiblelint.file_utils import Lintable
13-
from ansiblelint.rules import AnsibleLintRule
13+
from ansiblelint.rules import AnsibleLintRule, TransformMixin
1414
from ansiblelint.yaml_utils import load_yamllint_config
1515

1616
if TYPE_CHECKING:
@@ -22,7 +22,7 @@
2222
_logger = logging.getLogger(__name__)
2323

2424

25-
class YamllintRule(AnsibleLintRule):
25+
class YamllintRule(AnsibleLintRule, TransformMixin):
2626
"""Violations reported by yamllint."""
2727

2828
id = "yaml"
@@ -92,6 +92,22 @@ def matchyaml(self, file: Lintable) -> list[MatchError]:
9292
)
9393
return matches
9494

95+
def transform(
96+
self: YamllintRule,
97+
match: MatchError,
98+
lintable: Lintable,
99+
data: MutableMapping[str, Any] | MutableSequence[Any] | str,
100+
) -> None:
101+
"""Transform yaml.
102+
103+
:param match: MatchError instance
104+
:param lintable: Lintable instance
105+
:param data: data to transform
106+
"""
107+
# This method does nothing because the YAML reformatting is implemented
108+
# in data dumper. Still presence of this method helps us with
109+
# documentation generation.
110+
95111

96112
def _combine_skip_rules(data: Any) -> set[str]:
97113
"""Return a consolidated list of skipped rules."""

tools/generate_docs.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
#!/bin/env python3
1+
#!python3
22
"""Script that tests rule markdown documentation."""
33
from __future__ import annotations
44

0 commit comments

Comments
 (0)