ansible-lint
ansible-lint checks playbooks for practices and behavior that could potentially be improved and can fix some of the most common ones for you
Top Related Projects
The strictest and most opinionated python linter ever!
It's not just a linter that annoys you!
flake8 is a python tool that glues together pycodestyle, pyflakes, mccabe, and third-party plugins to check the style and quality of some python code.
The uncompromising Python code formatter
A formatter for Python files
coala provides a unified command-line interface for linting and fixing all your code, regardless of the programming languages you use.
Quick Overview
Ansible-lint is a command-line tool for linting Ansible playbooks, roles, and collections. It helps identify and fix style issues, potential errors, and deviations from best practices in Ansible code. Ansible-lint can be integrated into CI/CD pipelines to ensure consistent code quality across Ansible projects.
Pros
- Improves code quality and consistency in Ansible projects
- Customizable rules and ability to ignore specific rules
- Integrates well with CI/CD pipelines and text editors
- Regularly updated to support new Ansible features and best practices
Cons
- Can be overly strict for some users, requiring configuration to ignore certain rules
- May produce false positives in complex playbooks or roles
- Learning curve for understanding and customizing rules
- Performance can be slow on large projects with many files
Code Examples
- Basic usage:
# Lint a single playbook
ansible-lint playbook.yml
# Lint multiple files or directories
ansible-lint playbook1.yml playbook2.yml roles/
- Ignoring specific rules:
# In .ansible-lint file
skip_list:
- name[casing]
- no-changed-when
- Using tags to run specific rule types:
# Run only rules tagged as 'formatting'
ansible-lint --tags formatting playbook.yml
- Integrating with pre-commit:
# In .pre-commit-config.yaml
- repo: https://github.com/ansible/ansible-lint
rev: v6.14.3
hooks:
- id: ansible-lint
Getting Started
To get started with ansible-lint:
- Install ansible-lint:
pip install ansible-lint
- Run ansible-lint on your Ansible files:
ansible-lint path/to/your/playbook.yml
-
Review the output and fix any reported issues in your Ansible code.
-
(Optional) Create an
.ansible-lint
configuration file in your project root to customize rules and behavior:
exclude_paths:
- .cache/
- .github/
skip_list:
- name[casing]
warn_list:
- experimental
- Integrate ansible-lint into your CI/CD pipeline or pre-commit hooks for automated checks.
Competitor Comparisons
The strictest and most opinionated python linter ever!
Pros of wemake-python-styleguide
- More comprehensive and stricter set of rules for Python code quality
- Highly customizable with over 900 configurable options
- Integrates well with various CI/CD tools and IDEs
Cons of wemake-python-styleguide
- Can be overwhelming for beginners due to its strict nature
- May require more initial setup and configuration time
- Some rules might be too opinionated for certain projects or teams
Code Comparison
wemake-python-styleguide:
# Enforces strict naming conventions
def calculate_total(items: List[Dict[str, Any]]) -> float:
return sum(item['price'] for item in items)
ansible-lint:
# Focuses on Ansible-specific best practices
- name: Install packages
apt:
name: "{{ item }}"
state: present
loop:
- nginx
- postgresql
While wemake-python-styleguide is designed for Python code quality and style enforcement, ansible-lint is specifically tailored for Ansible playbooks and roles. wemake-python-styleguide offers a more comprehensive set of rules for Python development, whereas ansible-lint focuses on Ansible-specific best practices and conventions. The choice between the two depends on the primary focus of your project: Python development or Ansible automation.
It's not just a linter that annoys you!
Pros of Pylint
- More general-purpose Python linter, suitable for a wide range of Python projects
- Highly customizable with extensive configuration options
- Larger community and ecosystem, with more plugins and integrations available
Cons of Pylint
- Can be slower on large codebases compared to Ansible-lint
- May produce more false positives, requiring more configuration to fine-tune
- Less specialized for Ansible-specific code and best practices
Code Comparison
Pylint configuration example:
[MASTER]
ignore=CVS
ignore-patterns=
persistent=yes
load-plugins=
jobs=1
unsafe-load-any-extension=no
extension-pkg-whitelist=
Ansible-lint configuration example:
exclude_paths:
- .cache/
- .github/
- test/fixtures/
- test/schemas/
parseable: true
quiet: false
verbosity: 1
While both tools use configuration files, Pylint typically uses a .pylintrc
file with Python-like syntax, whereas Ansible-lint uses a YAML-based configuration file. Ansible-lint's configuration is more focused on Ansible-specific settings, while Pylint's configuration covers a broader range of Python linting options.
flake8 is a python tool that glues together pycodestyle, pyflakes, mccabe, and third-party plugins to check the style and quality of some python code.
Pros of flake8
- Broader scope: Checks Python code style, syntax errors, and complexity
- Highly extensible with a large ecosystem of plugins
- Faster execution time for large codebases
Cons of flake8
- Less specialized for Ansible-specific linting rules
- Requires additional configuration for Ansible-specific checks
- May produce false positives for Ansible-specific code patterns
Code Comparison
flake8:
import sys
def example_function():
unused_variable = "This will trigger a warning"
print("Hello, World!")
if __name__ == "__main__":
example_function()
ansible-lint:
- name: Install packages
apt:
name: "{{ item }}"
state: present
loop:
- package1
- package2
Summary
flake8 is a general-purpose Python linter with a wide range of checks and a large plugin ecosystem. It's faster and more extensible but lacks Ansible-specific rules out of the box. ansible-lint is tailored for Ansible playbooks and roles, providing specialized checks for Ansible best practices and common pitfalls. While flake8 excels in Python code analysis, ansible-lint is the better choice for Ansible-specific linting tasks. The choice between the two depends on the primary focus of your project and the level of Ansible-specific linting required.
The uncompromising Python code formatter
Pros of Black
- Opinionated and deterministic formatting, reducing debates over code style
- Faster execution time for large codebases
- Wider language support (Python) compared to Ansible-specific linting
Cons of Black
- Less flexibility in formatting choices
- Focuses solely on code formatting, not linting or error detection
- May require more setup and configuration for non-Python projects
Code Comparison
Black (Python formatting):
def long_function_name(
var_one, var_two, var_three, var_four
):
print(f"{var_one} {var_two} {var_three} {var_four}")
Ansible-lint (YAML linting):
- name: Install packages
apt:
name:
- package1
- package2
state: present
Summary
Black is a Python code formatter that enforces a consistent style, while Ansible-lint is specifically designed for linting Ansible playbooks and roles. Black excels in providing uniform formatting across Python projects, potentially improving code readability and reducing style-related discussions. However, it lacks the specialized Ansible-specific checks that Ansible-lint offers.
Ansible-lint, on the other hand, provides targeted linting for Ansible files, helping users identify and fix common issues in their infrastructure-as-code. It offers more flexibility in terms of rule customization but is limited to the Ansible ecosystem.
The choice between these tools depends on the specific needs of your project: Black for Python code formatting or Ansible-lint for Ansible playbook and role linting.
A formatter for Python files
Pros of YAPF
- More versatile, can format Python code in general, not limited to Ansible
- Highly configurable with many style options
- Supports multiple styles (PEP8, Google, etc.) out of the box
Cons of YAPF
- Not specialized for Ansible, may miss Ansible-specific formatting nuances
- Requires separate installation and setup, not integrated into Ansible ecosystem
- May have a steeper learning curve for Ansible-specific users
Code Comparison
YAPF example:
def long_function_name(
var_one, var_two, var_three,
var_four):
print(var_one)
Ansible-lint example:
- name: Install packages
apt:
name:
- package1
- package2
state: present
Key Differences
- Ansible-lint focuses on linting and best practices for Ansible playbooks and roles
- YAPF is a general-purpose Python code formatter
- Ansible-lint provides Ansible-specific rules and checks
- YAPF offers more granular control over code formatting styles
- Ansible-lint integrates seamlessly with Ansible projects and CI/CD pipelines
Use Cases
- Choose Ansible-lint for Ansible-specific projects and maintaining consistent Ansible code quality
- Opt for YAPF when working on general Python projects or requiring flexible formatting options
Community and Ecosystem
- Ansible-lint has strong support within the Ansible community
- YAPF is widely used in the broader Python ecosystem
coala provides a unified command-line interface for linting and fixing all your code, regardless of the programming languages you use.
Pros of coala
- Supports multiple programming languages and file formats
- Highly customizable with a plugin system for adding new linters
- Provides a unified interface for various analysis tools
Cons of coala
- Steeper learning curve due to its extensive configuration options
- May require more setup time for complex projects
- Less specialized for Ansible-specific linting
Code Comparison
coala configuration example:
[all]
files = **/*.py
bears = PEP8Bear, PyUnusedCodeBear
use_spaces = True
ansible-lint configuration example:
exclude_paths:
- .cache/
- .github/
skip_list:
- '301'
- '303'
Key Differences
- Scope: coala is a generic linting framework, while ansible-lint focuses specifically on Ansible playbooks and roles.
- Language support: coala supports multiple languages, ansible-lint is Ansible-specific.
- Configuration: coala uses a custom configuration format, ansible-lint uses YAML.
- Integration: ansible-lint integrates seamlessly with Ansible projects, while coala requires additional setup for Ansible linting.
- Community: ansible-lint has a more focused community for Ansible development, while coala has a broader user base across different languages and frameworks.
Both tools aim to improve code quality, but they cater to different use cases. Choose coala for multi-language projects or when you need a highly customizable linting solution. Opt for ansible-lint when working specifically with Ansible projects for a more tailored and integrated experience.
Convert designs to code with AI
Introducing Visual Copilot: A new AI model to turn Figma designs to high quality code using your components.
Try Visual CopilotREADME
Ansible-lint
ansible-lint
checks playbooks for practices and behavior that could
potentially be improved. As a community-backed project ansible-lint supports
only the last two major versions of Ansible.
Visit the Ansible Lint docs site
Using ansible-lint as a GitHub Action
This action allows you to run ansible-lint
on your codebase without having to
install it yourself.
# .github/workflows/ansible-lint.yml
name: ansible-lint
on:
pull_request:
branches: ["main", "stable", "release/v*"]
jobs:
build:
name: Ansible Lint # Naming the build is important to use it as a status check
runs-on: ubuntu-24.04
steps:
- uses: actions/checkout@v4
- name: Run ansible-lint
uses: ansible/ansible-lint@main
# optional (see below):
with:
args: ""
setup_python: "true"
working_directory: ""
requirements_file: ""
All the arguments are optional and most users should not need them:
args
: Arguments to be passed to ansible-lint command.setup_python
: If python should be installed. Default istrue
.working_directory
: The directory where to run ansible-lint from. Default isgithub.workspace
. That might be needed if you want to lint only a subset of your repository.requirements_file
: Path to the requirements.yml file to install role and collection dependencies.
For more details, see ansible-lint-action.
Communication
Refer to the Talk to us section of the Contributing guide to find out how to get in touch with us.
You can also find more information in the Ansible communication guide.
Contributing
Please read Contribution guidelines if you wish to contribute.
Code of Conduct
Please see the Ansible Community Code of Conduct.
Licensing
The ansible-lint project is distributed as GPLv3 due to use of GPLv3 runtime
dependencies, like ansible
and yamllint
.
For historical reasons, its own code-base remains licensed under a more liberal MIT license and any contributions made are accepted as being made under original MIT license.
Authors
ansible-lint was created by Will Thames and is now maintained as part of the Ansible by Red Hat project.
Top Related Projects
The strictest and most opinionated python linter ever!
It's not just a linter that annoys you!
flake8 is a python tool that glues together pycodestyle, pyflakes, mccabe, and third-party plugins to check the style and quality of some python code.
The uncompromising Python code formatter
A formatter for Python files
coala provides a unified command-line interface for linting and fixing all your code, regardless of the programming languages you use.
Convert designs to code with AI
Introducing Visual Copilot: A new AI model to turn Figma designs to high quality code using your components.
Try Visual Copilot