Stage 4 · Provision
Testing, AWX & CI/CD
ansible-lint & yamllint
Catching anti-patterns and enforcing style in CI.
Why Lint Ansible?
Linting catches anti-patterns, style issues, and potential bugs before they reach production. ansible-lint checks for best practices, deprecated modules, and common mistakes. yamllint validates YAML syntax and formatting.
ansible-lint
# Lint current directory
ansible-lint
# Lint specific playbook
ansible-lint site.yml
# Lint with specific rules
ansible-lint -R rules/ -r rules/ site.yml
# Output in parseable format
ansible-lint -f json site.yml
# Skip specific rules
ansible-lint --skip-rule yaml[line-length] site.ymlansible-lint checks playbooks, roles, and molecule scenarios for common issues. Fix warnings before they become problems.
# BAD: name is missing
- package:
name: nginx
state: present
# GOOD: name is present
- name: Install nginx
ansible.builtin.package:
name: nginx
state: present
# BAD: using shell for package management
- shell: apt-get install -y nginx
# GOOD: using declarative module
- name: Install nginx
ansible.builtin.package:
name: nginx
state: presentansible-lint flags missing names, shell usage instead of modules, and deprecated patterns. Each finding includes the rule that triggered it.
yamllint
# Lint a file
yamllint site.yml
# Lint with strict mode
yamllint -s site.yml
# Lint entire directory
yamllint -s .yamllint catches indentation errors, line length violations, and YAML syntax issues that ansible-lint may miss.
Lint Configuration
---
# Skip rules
skip_list:
- yaml[line-length]
- no-changed-when
# Warn on these (do not fail)
warn_list:
- experimental
- yaml[truthy]
# Enable offline mode for CI
offline: false
# Mock module results for CI
mock_modules:
- amazon.aws.ec2
- community.docker.docker_containerConfigure lint rules in .ansible-lint at the project root. Custom rules let you enforce your team's specific standards.
---
extends: default
rules:
line-length:
max: 200
level: warning
truthy:
allowed-values: ['true', 'false', 'yes', 'no']
comments:
min-spaces-from-content: 1
indentation:
spaces: 2
indent-sequences: trueyamllint configuration controls YAML formatting rules. Match your team's conventions and enforce them consistently.
CI Integration
name: Ansible Lint
on: [push, pull_request]
jobs:
lint:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: actions/setup-python@v5
with:
python-version: "3.11"
- run: pip install ansible-lint yamllint
- run: yamllint -s .
- run: ansible-lintRun linting on every push and pull request. Lint failures should block merges to main.
If your codebase has many lint findings, fix them in batches. Use skip_list to suppress known issues temporarily, then remove them as you fix each one.
Mark this lesson complete to store local progress and unlock a cleaner resume path the next time you visit.