Ansible Lint
Ansible Lint is a command-line tool that checks Ansible playbooks, roles, and collections for practices and behavior that could potentially be improved. It helps maintain code quality, enforce best practices, and identify common pitfalls in Ansible automation. The current version is 26.4.0, and it maintains an active release cadence, often with multiple updates per month to incorporate new rules and keep pace with Ansible Core development.
Warnings
- breaking `ansible-lint` versions `23.x` and newer require Python `3.10` or later. Users on older Python versions (e.g., 3.8, 3.9) will encounter installation or runtime errors and must upgrade their Python environment or use an older `ansible-lint` version.
- breaking Major `ansible-lint` releases (e.g., 6.x, 23.x) frequently introduce new rule IDs, rename existing ones, and modify the set of rules enabled by default. This can cause previously passing playbooks to fail lint checks or require updates to custom `.ansible-lint` configurations to suppress/enable specific rules.
- gotcha The schema and available options within the `.ansible-lint` configuration file are subject to change between versions. Using a configuration file from an older `ansible-lint` version with a newer installation can lead to ignored settings, warnings about unknown options, or unexpected linting behavior.
- gotcha While `ansible-lint` offers a Python API (e.g., `ansiblelint.app.App`, `ansiblelint.runner.Runner`), its public interface is not as strictly stable as its CLI. Direct programmatic usage should be thoroughly tested after each `ansible-lint` upgrade, as internal changes might affect custom integrations.
Install
-
pip install ansible-lint
Imports
- App
from ansiblelint.app import App
- Options
from ansiblelint.config import Options
- MatchError
from ansiblelint.errors import MatchError
Quickstart
import os
import tempfile
from pathlib import Path
from ansiblelint.app import App
from ansiblelint.config import Options
# Create a temporary playbook file for linting
playbook_content = """
---
- name: Example playbook with common linting issues
hosts: localhost
tasks:
- name: Using command module directly (LINT: no-shell-command)
ansible.builtin.command: echo "hello world"
- name: Insecure default permissions for file (LINT: risky-file-permissions)
ansible.builtin.file:
path: /tmp/testfile.txt
state: touch
mode: "0777" # Risky permissions
"""
temp_dir = Path(tempfile.mkdtemp())
playbook_path = temp_dir / "playbook.yml"
with open(playbook_path, "w") as f:
f.write(playbook_content)
try:
# Configure linting options
options = Options()
# Prevent App from configuring logging globally, for cleaner output in example
options.configure_logger = False
# Specify the file(s) to lint
options.lintables = [str(playbook_path)]
# Set up app and run lint
app = App(options)
matches = app.run()
print(f"Linting results for {playbook_path.name}:")
if matches:
for match in matches:
print(f"- [{match.rule_id}] {match.message} (File: {match.filename}, Line: {match.linenumber})")
else:
print("No linting issues found.")
except Exception as e:
print(f"An error occurred: {e}")
finally:
# Clean up the temporary directory and file
if temp_dir.exists():
for item in temp_dir.iterdir():
item.unlink()
temp_dir.rmdir()