Skip to content

Commit 54cf3ea

Browse files
authored
Document autofix ability for rules that supports it (#3791)
1 parent a02e008 commit 54cf3ea

13 files changed

+55
-61
lines changed

mkdocs.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,7 @@ plugins:
115115
- markdown-exec
116116
- gen-files:
117117
scripts:
118-
- src/ansiblelint/generate_docs.py
118+
- tools/generate_docs.py
119119
- material/search:
120120
separator: '[\s\-,:!=\[\]()"`/]+|\.(?!\d)|&[lg]t;|(?!\b)(?=[A-Z][a-z])'
121121
- material/social

src/ansiblelint/generate_docs.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,12 @@ def rules_as_md(rules: RulesCollection) -> str:
5858

5959
result += f"\n\n## {title}\n\n**{rule.shortdesc}**\n\n{description}"
6060

61+
# Safety net for preventing us from adding autofix to rules and
62+
# forgetting to mention it inside their documentation.
63+
if "autofix" in rule.tags and "autofix" not in rule.description:
64+
msg = f"Rule {rule.id} is invalid because it has 'autofix' tag but this ability is not documented in its description."
65+
raise RuntimeError(msg)
66+
6167
return result
6268

6369

src/ansiblelint/rules/command_instead_of_shell.md

Lines changed: 2 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -29,64 +29,6 @@ environment variable expansion or chaining multiple commands using pipes.
2929
changed_when: false
3030
```
3131
32-
## Auto-fixing capability
32+
!!! note
3333
34-
### Before autofix
35-
36-
```yaml
37-
---
38-
- name: Fixture
39-
hosts: localhost
40-
tasks:
41-
- name: Shell no pipe
42-
ansible.builtin.shell:
43-
cmd: echo hello
44-
changed_when: false
45-
46-
- name: Shell with jinja filter
47-
ansible.builtin.shell:
48-
cmd: echo {{ "hello" | upper }}
49-
changed_when: false
50-
51-
- name: Shell with jinja filter (fqcn)
52-
ansible.builtin.shell:
53-
cmd: echo {{ "hello" | upper }}
54-
changed_when: false
55-
56-
- name: Command with executable parameter
57-
ansible.builtin.shell:
58-
cmd: clear
59-
args:
60-
executable: /bin/bash
61-
changed_when: false
62-
```
63-
64-
### After autofix
65-
66-
```yaml
67-
---
68-
- name: Fixture
69-
hosts: localhost
70-
tasks:
71-
- name: Shell no pipe
72-
ansible.builtin.command:
73-
cmd: echo hello
74-
changed_when: false
75-
76-
- name: Shell with jinja filter
77-
ansible.builtin.command:
78-
cmd: echo {{ "hello" | upper }}
79-
changed_when: false
80-
81-
- name: Shell with jinja filter (fqcn)
82-
ansible.builtin.command:
83-
cmd: echo {{ "hello" | upper }}
84-
changed_when: false
85-
86-
- name: Command with executable parameter
87-
ansible.builtin.shell:
88-
cmd: clear
89-
args:
90-
executable: /bin/bash
91-
changed_when: false
92-
```
34+
This rule can be automatically fixed using [`--fix`](/autofix) option.

src/ansiblelint/rules/deprecated_local_action.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,3 +19,7 @@ This rule recommends using `delegate_to: localhost` instead of the
1919
ansible.builtin.debug:
2020
delegate_to: localhost # <-- recommended way to run on localhost
2121
```
22+
23+
!!! note
24+
25+
This rule can be automatically fixed using [`--fix`](/autofix) option.

src/ansiblelint/rules/fqcn.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,3 +87,7 @@ structure in a backward-compatible way by adding redirects like in
8787
# Use the FQCN for the builtin shell module.
8888
ansible.builtin.shell: ssh ssh_user@{{ ansible_ssh_host }}
8989
```
90+
91+
!!! note
92+
93+
This rule can be automatically fixed using [`--fix`](/autofix) option.

src/ansiblelint/rules/jinja.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,3 +53,7 @@ In its current form, this rule presents the following limitations:
5353
does not support tilde as a binary operator. Example: `{{ a ~ b }}`.
5454
- Jinja2 blocks that use dot notation with numbers are ignored because python
5555
and black do not allow it. Example: `{{ foo.0.bar }}`
56+
57+
!!! note
58+
59+
This rule can be automatically fixed using [`--fix`](/autofix) option.

src/ansiblelint/rules/key_order.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,3 +61,7 @@ we concluded that the block keys must be the last ones.
6161

6262
Another common practice was to put `tags` as the last property. Still, for the
6363
same reasons, we decided that they should not be put after block keys either.
64+
65+
!!! note
66+
67+
This rule can be automatically fixed using [`--fix`](/autofix) option.

src/ansiblelint/rules/name.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,3 +59,7 @@ your `enable_list` to activate it.
5959
- name: Create placeholder file
6060
ansible.builtin.command: touch /tmp/.placeholder
6161
```
62+
63+
!!! note
64+
65+
`name[casing]` can be automatically fixed using [`--fix`](/autofix) option.

src/ansiblelint/rules/no_free_form.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,3 +56,7 @@ This rule can produce messages as:
5656
executable: /bin/bash # <-- explicit is better
5757
changed_when: false
5858
```
59+
60+
!!! note
61+
62+
This rule can be automatically fixed using [`--fix`](/autofix) option.

src/ansiblelint/rules/no_jinja_when.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,3 +30,7 @@ anti-pattern and does not produce expected results.
3030
ansible.builtin.command: /sbin/shutdown -t now
3131
when: ansible_facts['os_family'] == "Debian" # <- Uses facts in a conditional statement.
3232
```
33+
34+
!!! note
35+
36+
This rule can be automatically fixed using [`--fix`](/autofix) option.

src/ansiblelint/rules/no_log_password.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,3 +43,7 @@ Explicitly adding `no_log: true` prevents accidentally exposing secrets.
4343
- wow
4444
no_log: true # <- Sets the no_log attribute to a non-false value.
4545
```
46+
47+
!!! note
48+
49+
This rule can be automatically fixed using [`--fix`](/autofix) option.

src/ansiblelint/rules/partial_become.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -120,3 +120,7 @@ This rule can produce the following messages:
120120
become: true # <- Activates privilege escalation.
121121
become_user: apache # <- Does not change the user because "become: true" is not set.
122122
```
123+
124+
!!! note
125+
126+
This rule can be automatically fixed using [`--fix`](/autofix) option.

tools/generate_docs.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
#!/bin/env python3
2+
"""Script that tests rule markdown documentation."""
3+
import subprocess
4+
5+
subprocess.run(
6+
"ansible-lint -L --format=md", # noqa: S607
7+
shell=True, # noqa: S602
8+
check=True,
9+
stdout=subprocess.DEVNULL,
10+
)

0 commit comments

Comments
 (0)