task 8.2 · chapter 8 of 8: run it like production · 25 min
ansible-lint
objective
Make the legacy playbook pass ansible-lint’s production profile, without silencing a single rule.
playbooks/legacy.ymlopen a file in VS Code: Ctrl+P (Cmd+P on a Mac), type its name, Enter
| Profile | production, the strictest (set in .ansible-lint) |
|---|---|
| Rule | fix the playbook, never # noqa or skip_list |
What does ansible-lint check?
ansible-lint reads playbooks and roles and reports anything that’s likely to break, confuse a reader or behave
differently than it looks: tasks without names, modules called by short names that could resolve to the wrong
collection, yes and no where Ansible means true and false, odd spacing.
Its profiles go from min to production. The lab uses production: what AAP’s own content checks expect.
The rule for this task is the rule for real life: fix the playbook, don’t silence the linter.
How do you read an ansible-lint result?
name[play]: All plays should be named.
playbooks/legacy.yml:6:3
fqcn[action-core]: Use FQCN for builtin module actions (debug).
playbooks/legacy.yml:15:5
Each finding is three things: the rule (fqcn[action-core]), what’s wrong, and where (file, line,
column). The rule name is searchable: ansible-lint -L lists them all, and the docs explain each one.
The first three fixes
Run it and read the list:
run in the terminalansible-lint playbooks/legacy.ymlname[play]: the play has no name. Give it one, as the first key:editplaybooks/legacy.ymlCtrl+P → legacy.yml- name: Show what each switch is running hosts: fabricyaml[truthy]:gather_facts: no. YAML has many spellings of true and false; Ansible’s style is the plain ones:editplaybooks/legacy.ymlCtrl+P → legacy.ymlgather_facts: falsefqcn[action-core]:debugshould be its fully-qualified collection name, so there’s never a doubt which module runs:editplaybooks/legacy.ymlCtrl+P → legacy.yml- ansible.builtin.debug:Run
ansible-lint playbooks/legacy.ymlagain. Three fewer findings.
The rest
Fix every remaining finding until ansible-lint says Passed. The playbook must do exactly what it did before:
ansible-playbook playbooks/legacy.yml still prints each switch’s version.
hint
Every task needs a name:. Variable names are lowercase (version, not Version), and Jinja2 wants a space
inside the braces: {{ inventory_hostname }}.
hint
This repo indents the items under tasks: by 4 spaces, not 2. If the linter complains about indentation, compare
with any other playbook.
show the solutionplaybooks/legacy.yml
or pull it into your lab: make solve T=8.2, then git diff --staged shows what changed
- name: Show what each switch is running
hosts: fabric
gather_facts: false
tasks:
- name: Ask for the software version
cisco.ios.ios_command:
commands:
- show version
register: version
- name: Show it
ansible.builtin.debug:
msg: "{{ inventory_hostname }}: {{ version.stdout_lines[0][0] }}"
5 check: run it, then prove it
$ ansible-lint playbooks/legacy.yml
$ make check T=8.2when it's right, you'll see
╭─ task 8.2 · ansible-lint
│ ✓ ansible-lint passes (0 problem(s))
╰─ 1/1 checks passed verified
background: part 8, ansible-lint