TExcelTAD
Home › Configuration Management: Ansible › Linting with ansible-lint

Linting with ansible-lint

ansible-lint checks playbooks, roles and collections for bugs, deprecated syntax and practices that cause trouble later: missing names, short module names, commands that always report changed, unquoted modes, latest package states, risky shell pipelines. It is maintained by the Ansible project and is the standard first gate in CI for Ansible repositories.

A playbook with typical problems

bash · try it yourself
mkdir lint && cd lint
cat > site.yml <<'YML'
---
- hosts: localhost
  gather_facts: false
  tasks:
    - name: install tools
      apt: name=jq state=latest
      become: true

    - name: Count log lines
      ansible.builtin.shell: cat /var/log/syslog | wc -l

    - name: Write config
      ansible.builtin.copy:
        dest: /tmp/app.conf
        content: "x=1\n"
        mode: 0644
YML
ansible-lint site.yml 2>&1
echo "exit code: $?"
WARNING  Listing 8 violation(s) that are fatal
Read documentation for instructions on how to ignore specific rule violations.

# Rule Violation Summary

  1 no-free-form profile:basic tags:syntax,risk
  1 name profile:basic tags:idiom
  1 yaml profile:basic tags:formatting,yaml
  1 name profile:basic tags:idiom
  1 package-latest profile:basic tags:idempotency
  1 risky-shell-pipe profile:basic tags:command-shell
  1 no-changed-when profile:basic tags:command-shell,idempotency
  1 fqcn profile:basic tags:formatting

Failed: 8 failure(s), 0 warning(s) in 1 files processed of 1 encountered. Last profile that met the validation criteria was 'min'.
name[play]: All plays should be named.
site.yml:2:3

name[casing]: All names should start with an uppercase letter.
site.yml:5:13 Task/Handler: install tools

no-free-form: Avoid using free-form when calling module actions. (apt)
site.yml:5 apt

package-latest: Package installs should not use latest.
site.yml:5 Task/Handler: install tools

fqcn[action-core]: Use FQCN for builtin module actions (apt).
site.yml:6:7 Use `ansible.builtin.apt` or `ansible.legacy.apt` instead.

no-changed-when: Commands should not change things if nothing needs doing.
site.yml:9 Task/Handler: Count log lines

risky-shell-pipe: Shells that use pipes should set the pipefail option.
site.yml:9 Task/Handler: Count log lines

yaml[octal-values]: Forbidden implicit octal value "0644"
site.yml:16

exit code: 2

ansible-lint prints a summary first (one line per rule, with the profile it belongs to), then each finding with its file and line. Read them as a checklist:

Rule Problem Fix
name[play] the play has no name - name: … on every play
name[casing] names should start with a capital letter Install tools
fqcn[action-core] short module name apt ansible.builtin.apt
no-free-form key=value arguments YAML dictionary
package-latest state: latest changes over time present, or a pinned version
risky-shell-pipe a pipe without pipefail hides failures set -o pipefail (and executable: /bin/bash), or no pipe at all
no-changed-when a command always reports changed changed_when
yaml[octal-values] mode: 0644 is read by YAML as a number mode: "0644"

The fixed version

bash · try it yourself
mkdir lint && cd lint
cat > site.yml <<'YML'
---
- name: Tools and config
  hosts: localhost
  gather_facts: false
  tasks:
    - name: Install tools
      ansible.builtin.apt:
        name: jq
        state: present
      become: true

    - name: Count log lines
      ansible.builtin.command: wc -l /var/log/syslog
      register: syslog_lines
      changed_when: false

    - name: Write config
      ansible.builtin.copy:
        dest: /tmp/app.conf
        content: "x=1\n"
        mode: "0644"
YML
ansible-lint site.yml 2>&1
echo "exit code: $?"

Passed: 0 failure(s), 0 warning(s) in 1 files processed of 1 encountered. Last profile that met the validation criteria was 'production'.
exit code: 0

Profiles and configuration

A shell task without any shell feature would also get command-instead-of-shell; here the pipe is a shell feature, and the fix below drops both the pipe and shell.

Rules are grouped into profiles, from lenient to strict: min, basic, moderate, safety, shared, production. The summary line tells you which profile your code already meets. Pick a target profile in .ansible-lint at the repository root and raise it over time:

yaml
# .ansible-lint
profile: production       # the strictest
exclude_paths:
  - .cache/
  - collections/          # vendored third-party code
warn_list:
  - experimental
skip_list: []              # prefer fixing over skipping

To silence one finding with a reason, add a comment on the line: # noqa: no-changed-when. Keep a reason in a comment next to it; an unexplained noqa is a bug report you wrote to yourself.

Autofix

ansible-lint --fix rewrites what it can fix safely (FQCNs, YAML formatting, name[casing], no-free-form and others) and leaves the rest. Commit before running it and review the diff:

bash · try it yourself
mkdir lint && cd lint
cat > site.yml <<'YML'
---
- name: Autofix demo
  hosts: localhost
  gather_facts: false
  tasks:
    - name: create a directory
      file: path=/tmp/demo state=directory mode="0755"
YML
ansible-lint --fix site.yml >/dev/null 2>&1
cat site.yml
---
- name: Autofix demo
  hosts: localhost
  gather_facts: false
  tasks:
    - name: Create a directory
      ansible.builtin.file:
        path: /tmp/demo
        state: directory
        mode: "0755"

In CI

yaml
# .github/workflows/ansible.yml (excerpt)
- uses: actions/setup-python@v6
  with: {python-version: "3.13"}
- run: pip install "ansible-core==2.21.5" "ansible-lint==26.10.1"
- run: ansible-galaxy collection install -r requirements.yml
- run: ansible-lint --profile production

The project also publishes a GitHub Action (ansible/ansible-lint). Pin ansible-lint's version: new releases add rules, and an unpinned linter breaks your build on a day you changed nothing. CI/CD Pipelines covers pipelines in general.

Test yourself

Which ansible-lint rule flags mode: 0644?

  1. fqcn
  2. yaml[octal-values]
  3. yaml[truthy]
  4. no-changed-when

What does ansible-lint --fix do?

  1. Runs the playbook
  2. Rewrites files to fix the findings it can fix safely
  3. Deletes failing tasks
  4. Updates ansible-lint

Why pin the ansible-lint version in CI?

  1. Newer versions are slower
  2. New releases add rules, so an unpinned linter can fail builds without any code change
  3. Galaxy requires it
  4. It is free only when pinned