online posts 16
ansible 101 for networking

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.

you edit, in the labplaybooks/legacy.yml

open a file in VS Code: Ctrl+P (Cmd+P on a Mac), type its name, Enter

given
Profileproduction, the strictest (set in .ansible-lint)
Rulefix the playbook, never # noqa or skip_list
1 explain

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.

2 show

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.

3 follow along

The first three fixes

  1. Run it and read the list:

    run in the terminal
    ansible-lint playbooks/legacy.yml
    
  2. name[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: fabric
    
  3. yaml[truthy]: gather_facts: no. YAML has many spellings of true and false; Ansible’s style is the plain ones:

    editplaybooks/legacy.ymlCtrl+P → legacy.yml
      gather_facts: false
    
  4. fqcn[action-core]: debug should be its fully-qualified collection name, so there’s never a doubt which module runs:

    editplaybooks/legacy.ymlCtrl+P → legacy.yml
      - ansible.builtin.debug:
    
  5. Run ansible-lint playbooks/legacy.yml again. Three fewer findings.

4 challenge

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.2

when it's right, you'll see

╭─ task 8.2 · ansible-lint
│  ✓ ansible-lint passes (0 problem(s))
╰─ 1/1 checks passed  verified