Skip to content

Commit c23ae43

Browse files
AAP-34017 Add Ansible-lint chapter to Devtools doc
1 parent 4a56bad commit c23ae43

21 files changed

+2124
-15
lines changed

downstream/assemblies/devtools/assembly-devtools-lint.adoc

+22-6
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,28 @@ endif::[]
1111

1212
= Working with ansible-lint
1313

14-
:context: devtools-lint
15-
16-
// You must move roles into collections if you want to use them in {PlatformNameShort}.
17-
18-
Working with ansible-lint
19-
14+
:context: devtools-ansible-lint
15+
16+
include::devtools/ref-devtools-ansible-lint-about.adoc[leveloffset=+1]
17+
include::devtools/proc-debugging-playbook.adoc[leveloffset=+1]
18+
include::devtools/proc-devtools-ansible-lint-rules.adoc[leveloffset=+1]
19+
include::devtools/proc-devtools-ansible-lint-customize.adoc[leveloffset=+1]
20+
include::devtools/proc-devtools-ansible-lint-profiles.adoc[leveloffset=+1]
21+
// include::devtools/ref-devtools-ansible-lint-usage.adoc[leveloffset=+1]
22+
include::devtools/proc-devtools-ansible-lint-cli.adoc[leveloffset=+1]
23+
include::devtools/ref-devtools-ansible-lint-switches.adoc[leveloffset=+2]
24+
include::devtools/proc-devtools-ansible-lint-autofix.adoc[leveloffset=+3]
25+
include::devtools/proc-devtools-ansible-lint-exclude-rules.adoc[leveloffset=+2]
26+
include::devtools/proc-devtools-ansible-lint-ci.adoc[leveloffset=+1]
27+
// Include mute warnings?
28+
include::devtools/ref-devtools-ansible-lint-muting-warnings.adoc[leveloffset=+2]
29+
include::devtools/ref-devtools-ansible-lint-cache.adoc[leveloffset=+2]
30+
include::devtools/ref-devtools-ansible-lint-gradual-adoption.adoc[leveloffset=+2]
31+
include::devtools/proc-devtools-ansible-lint-vaults.adoc[leveloffset=+1]
32+
include::devtools/ref-devtools-ansible-lint-dependencies.adoc[leveloffset=+1]
33+
// Add a section about extending lint.
34+
// Not includoing the Specifying rules at runtime section because we don't encourage using custom rules.
35+
// proc-devtools-ansible-lint-all.adoc
2036
// include::devtools/proc-devtools-zzz.adoc[leveloffset=+1]
2137

2238
ifdef::parent-context-of-devtools-lint[:context: {parent-context-of-devtools-lint}]

downstream/assemblies/devtools/assembly-devtools-lint.html

+1,365
Large diffs are not rendered by default.

downstream/attributes/attributes.adoc

+2
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,8 @@
130130
:IDEcollection: Ansible IDE collection explorer
131131
:IDElanguage: Ansible IDE language server
132132
:VSCode: VS Code
133+
:LintName: Ansible Lint
134+
:LintCmd: ansible-lint
133135

134136
// Content Collections
135137
:CertifiedName: Ansible Certified Content Collections

downstream/modules/devtools/:wq

+31
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
[id="debugging-playbook_{context}"]
2+
3+
= Debugging with ansible-lint in {VSCode}
4+
5+
When the Ansible extension is running in {VSCode}, ansible-lint runs in the background.
6+
Diagnostics information for files whose language is identified as Ansible is displayed in the *Problems* tab of the terminal.
7+
8+
// == Error messages
9+
10+
The following playbook contains multiple errors:
11+
12+
----
13+
- name:
14+
hosts: localhost
15+
tasks:
16+
- name:
17+
ansible.builtin.ping:
18+
----
19+
20+
The errors are indicated with a wavy underline in {VSCode}.
21+
Hover your mouse over an error to view the details:
22+
23+
image::ansible-lint-errors.png[Popup message explaining a playbook error]
24+
25+
The errors are listed in the *Problems* tab of the {VSCode} terminal.
26+
Playbook files that contain errors are indicated with a number in the *Explorer* pane:
27+
28+
image::ansible-lint-errors-explorer.png[Playbook errors shown in Problems tab and explorer list]
29+
30+
`$[0].tasks[0].name None is not of type 'string'` indicates that the playbook does not have a label.
31+

downstream/modules/devtools/proc-debugging-playbook.adoc

+5-2
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,11 @@
11
[id="debugging-playbook_{context}"]
22

3-
= Debugging your playbook
3+
= Debugging with ansible-lint in {VSCode}
44

5-
== Error messages
5+
When the Ansible extension is running in {VSCode}, ansible-lint runs in the background.
6+
Diagnostics information for files whose language is identified as Ansible is displayed in the *Problems* tab of the terminal.
7+
8+
// == Error messages
69

710
The following playbook contains multiple errors:
811

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
[id="devtools-ansible-lint-autofix_{context}"]
2+
3+
= Autofix
4+
5+
Ansible-lint provides an autofix feature that allows auto-fixing of some rule violations in Ansible files.
6+
7+
To reformat YAML files and run transform for the given rules, run the following command:
8+
9+
----
10+
ansible-lint --fix [<write_list>]
11+
----
12+
13+
Use the `write-list` to limit the rules that you want to auto-fix
14+
by passing a keywords `all` or `none` or a comma separated list of rule ids or rule tags.
15+
16+
* `ansible-lint --fix ["all"]` (default) runs all transforms.
17+
* `ansible-lint --fix ["none"]` disables all auto-fixes.
18+
* `ansible-lint --fix ["csv-list-of-rules"]` enables a subset of rule transforms by listing rules/tags here.
19+
20+
The following rules allow autofix functionality.
21+
22+
[options="header" cols="30,70"]
23+
|===
24+
|Options |Description
25+
|`command-instead-of-shell`
26+
|Use shell only when shell functionality is required.
27+
28+
|`deprecated-local-action`
29+
|Do not use `local_action`, use `delegate_to: localhost`.
30+
31+
|`fqcn`
32+
|Use FQCN for builtin actions.
33+
34+
|`jinja`
35+
|Rule that looks inside jinja2 templates.
36+
37+
|`key-order`
38+
|Ensure specific order of keys in mappings.
39+
40+
|`name`
41+
|Rule for checking task and play names.
42+
43+
|`no-free-form`
44+
|Rule for detecting discouraged free-form syntax for action modules.
45+
46+
|`no-jinja-when`
47+
|No Jinja2 in when.
48+
49+
|`no-log-password`
50+
|Password should not be logged.
51+
52+
|`partial-become`
53+
|`become_user` should have a corresponding `become` at the play or task level.
54+
55+
|`yaml`
56+
|Violations reported by yamllint.
57+
|===
58+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
[id="devtools-ansible-lint-ci_{context}"]
2+
3+
= Using ansible-lint in CI
4+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
[id="devtools-ansible-lint-cli_{context}"]
2+
3+
= Running ansible-lint from the command line
4+
5+
Run `ansible-lint --help` to display available commands and their options.
6+
7+
Ansible-lint recommends following the
8+
https://docs.ansible.com/ansible-core/devel/dev_guide/developing_collections_structure.html#collection-structure[collection
9+
structure layout] whether you plan to build a collection or not.
10+
11+
Following that layout assures the best integration with all ecosystem
12+
tools because it helps those tools better distinguish between random
13+
YAML files and files managed by Ansible. When you call `ansible-lint`
14+
without arguments, it uses internal heuristics to determine file types.
15+
16+
Pass the *roles* and *playbooks* that you want to lint as arguments to
17+
the `ansible-lint` command. For example, to lint
18+
`examples/playbooks/play.yml` and `examples/roles/bobbins`, use the
19+
following command:
20+
21+
----
22+
$ ansible-lint examples/playbooks/play.yml examples/roles/bobbins
23+
----
24+
25+
// == Running example playbooks
26+
//
27+
// Ansible-lint includes an `ansible-lint/examples` folder that contains
28+
// example playbooks with different rule violations and undesirable
29+
// characteristics. You can run `ansible-lint` on the example playbooks
30+
// to observe Ansible-lint in action, as follows:
31+
//
32+
// ----
33+
// $ ansible-lint --offline -p examples/playbooks/example.yml
34+
// ----
35+
36+
Ansible-lint also handles playbooks that include other playbooks, tasks,
37+
handlers, or roles.
38+
//
39+
// ----
40+
// $ ansible-lint --offline -q -p examples/playbooks/include.yml
41+
// ----
42+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,185 @@
1+
[id="devtools-ansible-lint-customize_{context}"]
2+
3+
= Customizing {LintName}
4+
5+
You can customize how {LintName} runs against automation content to suit your needs.
6+
You can ignore certain rules, enable opt-in rules, and control various other settings.
7+
8+
{LintName} loads configuration from the `.ansible-lint.yml` configuration file, or from a file that you specify in the command line.
9+
This file is not scaffolded when you create a playbook or collection project.
10+
You must create the file in the `.config` directory of your {VSCode} workspace.
11+
12+
Any configuration option that is passed from the command line will override the one specified inside the configuration file.
13+
14+
The example `.ansible-lint.yml` file below selects the `production` profile.
15+
The settings are explained in the comments.
16+
17+
----
18+
---
19+
# .ansible-lint
20+
21+
profile: production # min, basic, moderate,safety, shared, production, null
22+
23+
# Allows dumping of results in SARIF format
24+
# sarif_file: result.sarif
25+
26+
# exclude_paths included in this file are parsed relative to this file's location
27+
# and not relative to the CWD of execution. CLI arguments passed to the --exclude
28+
# option are parsed relative to the CWD of execution.
29+
exclude_paths:
30+
- .cache/ # implicit unless exclude_paths is defined in config
31+
- test/fixtures/formatting-before/
32+
- test/fixtures/formatting-prettier/
33+
# parseable: true
34+
# quiet: true
35+
# strict: true
36+
# verbosity: 1
37+
38+
# Mock modules or roles in order to pass ansible-playbook --syntax-check
39+
mock_modules:
40+
- zuul_return
41+
# note the foo.bar is invalid as being neither a module or a collection
42+
- fake_namespace.fake_collection.fake_module
43+
- fake_namespace.fake_collection.fake_module.fake_submodule
44+
mock_roles:
45+
- mocked_role
46+
- author.role_name # old standalone galaxy role
47+
- fake_namespace.fake_collection.fake_role # role within a collection
48+
49+
# Enable checking of loop variable prefixes in roles
50+
loop_var_prefix: "^(__|{role}_)"
51+
52+
# Enforce variable names to follow pattern below, in addition to Ansible own
53+
# requirements, like avoiding python identifiers. To disable add `var-naming`
54+
# to skip_list.
55+
var_naming_pattern: "^[a-z_][a-z0-9_]*$"
56+
57+
use_default_rules: true
58+
# Load custom rules from this specific folder
59+
# rulesdir:
60+
# - ./rule/directory/
61+
62+
# Ansible-lint is able to recognize and load skip rules stored inside
63+
# `.ansible-lint-ignore` (or `.config/ansible-lint-ignore.txt`) files.
64+
# To skip a rule just enter filename and tag, like "playbook.yml package-latest"
65+
# on a new line.
66+
# Optionally you can add comments after the tag, prefixed by "#".
67+
# When putting ignores inside the ignore file, they are marked as ignored, but
68+
# still visible, making it easier to address later.
69+
70+
# Ansible-lint does not automatically load rules that have the 'opt-in' tag.
71+
# You must enable opt-in rules by listing each rule 'id' below.
72+
enable_list:
73+
- args
74+
- empty-string-compare # opt-in
75+
- no-log-password # opt-in
76+
- no-same-owner # opt-in
77+
- name[prefix] # opt-in
78+
- galaxy-version-incorrect # opt-in
79+
# add yaml here if you want to avoid ignoring yaml checks when yamllint
80+
# library is missing. Normally its absence just skips using that rule.
81+
- yaml
82+
# Report only a subset of tags and fully ignore any others
83+
# tags:
84+
# - jinja[spacing]
85+
86+
# Ansible-lint does not fail on warnings from the rules or tags listed below
87+
warn_list:
88+
- skip_this_tag
89+
- experimental # experimental is included in the implicit list
90+
# - role-name
91+
# - yaml[document-start] # you can also use sub-rule matches
92+
93+
# Some rules can transform files to fix (or make it easier to fix) identified
94+
# errors. `ansible-lint --fix` will reformat YAML files and run these transforms.
95+
# By default it will run all transforms (effectively `write_list: ["all"]`).
96+
# You can disable running transforms by setting `write_list: ["none"]`.
97+
# Or only enable a subset of rule transforms by listing rules/tags here.
98+
# write_list:
99+
# - all
100+
101+
# Offline mode disables installation of requirements.yml and schema refreshing
102+
offline: true
103+
104+
# Define required Ansible's variables to satisfy syntax check
105+
extra_vars:
106+
foo: bar
107+
multiline_string_variable: |
108+
line1
109+
line2
110+
complex_variable: ":{;\t$()"
111+
112+
# Uncomment to enforce action validation with tasks, usually is not
113+
# needed as Ansible syntax check also covers it.
114+
# skip_action_validation: false
115+
116+
# List of additional kind:pattern to be added at the top of the default
117+
# match list, first match determines the file kind.
118+
kinds:
119+
# - playbook: "**/examples/*.{yml,yaml}"
120+
# - galaxy: "**/folder/galaxy.yml"
121+
# - tasks: "**/tasks/*.yml"
122+
# - vars: "**/vars/*.yml"
123+
# - meta: "**/meta/main.yml"
124+
- yaml: "**/*.yaml-too"
125+
126+
# List of additional collections to allow in only-builtins rule.
127+
# only_builtins_allow_collections:
128+
# - example_ns.example_collection
129+
130+
# List of additions modules to allow in only-builtins rule.
131+
# only_builtins_allow_modules:
132+
# - example_module
133+
134+
# Allow setting custom prefix for name[prefix] rule
135+
task_name_prefix: "{stem} | "
136+
# Complexity related settings
137+
138+
# Limit the depth of the nested blocks:
139+
# max_block_depth: 20
140+
----
141+
142+
The following example `.ansible-lint.yml` file excludes the comments.
143+
It selects the `production` profile and enables fixing of files.
144+
145+
----
146+
---
147+
profile: 'production'
148+
149+
loop_var_prefix: '^(__|{role}_)'
150+
var_naming_pattern: '^[a-z_][a-z0-9_]*$'
151+
use_default_rules: true
152+
offline: true
153+
skip_action_validation: false
154+
155+
kinds:
156+
- tasks: 'tasks/*.{yml,yaml}'
157+
- vars: 'vars/*.{yml,yaml}'
158+
- vars: 'defaults/*.{yml,yaml}'
159+
- meta: 'meta/main.{yml,yaml}'
160+
- yaml: '.ansible-lint'
161+
- yaml: '.github/workflows/*.{yml,yaml}'
162+
- yaml: '.pre-commit-config.yaml'
163+
- yaml: '.yamllint'
164+
- yaml: 'collections/requirements.yml'
165+
166+
task_name_prefix: '{stem} | '
167+
168+
exclude_paths:
169+
- '.git/'
170+
- 'files/'
171+
172+
max_block_depth: 20
173+
skip_list: []
174+
warn_list:
175+
- 'experimental'
176+
write_list:
177+
- 'all'
178+
----
179+
180+
To specify the configuration file, use the `-c <filename>` CLI flag with command line invocations of `{LintCmd}`:
181+
182+
----
183+
ansible-lint -c path/to/ansible-lint-dev.yml
184+
----
185+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
[id="devtools-ansible-lint-exclude-rules_{context}"]
2+
3+
4+
= Excluding ansible-lint rules
5+
6+
To exclude specific rules from being applied when linting a playbook, use the `-x` flag.
7+
This is useful if you want to temporarily disable certain rules without modifying your configuration file.
8+
9+
The following example lints a file but skips the checks for YAML formatting rules and tabs:
10+
11+
----
12+
ansible-lint -x no-tabs -x yaml <filename.yml>
13+
----
14+

0 commit comments

Comments
 (0)