Introduction
Ansible-Lint is a powerful tool that helps Ansible users identify and correct issues in their playbooks and roles. It not only improves code quality but also ensures best practices are followed, making your automation processes more reliable and predictable.
In this article, we will explore various Ansible-Lint rules and provide insights into best practices to address the common issues flagged by these rules. We’ll cover rules related to module arguments, playbook structure, variable naming, and much more. We can list all the rules in Ansible Linter using the command ansible-lint -L.
Rule Categories
Ansible-Lint rules are organized into various categories, each addressing different aspects of the Ansible playbook and role development. Let’s dive into some of the most commonly encountered rule categories and explore them in detail.
Args
- Rule Name:
args
- Purpose: Validating module arguments. (syntax, experimental)
The “args” category focuses on validating the arguments provided to Ansible modules. It checks for correct syntax and identifies experimental features. It’s essential to use the right arguments for each module to ensure your tasks perform as intended.
Idioms
- Rule Name:
avoid-implicit
- Purpose: Avoid implicit behaviors (unpredictability)
The “idioms” category helps you avoid implicit or unpredictable behaviors in your playbooks. Implicit actions can lead to unexpected results, making your automation less reliable.
- Rule Name:
command-instead-of-module
- Purpose: Using command rather than module. (command-shell, idiom)
This rule encourages using modules over direct shell commands, as modules provide better control, error handling, and idempotence.
- Rule Name:
command-instead-of-shell
- Purpose: Use shell only when shell functionality is required. (autofix, command-shell, idiom)
Use the “command” module when shell functionality is necessary. This ensures better compatibility and maintainability.
- Rule Name:
partial-become
- Purpose: become_user should have a corresponding become at the play or task level. (autofix, unpredictability)
Ensure that when you specify “become_user,” there should also be a corresponding “become” at the play or task level for predictability and consistency.
Deprecated
- Rule Name:
deprecated-bare-vars
- Purpose: Using bare variables is deprecated. (deprecations)
The “deprecated” category focuses on identifying and discouraging the use of deprecated features and syntax. It’s essential to stay up to date with Ansible’s best practices.
- Rule Name:
deprecated-local-action
- Purpose: Do not use ‘
local_action,’ use ‘delegate_to: localhost’ (autofix, deprecations)
Avoid using “local_action” and use “delegate_to: localhost” for better clarity and consistency in your playbooks.
- Rule Name: deprecated-module
- Purpose: Deprecated module. (deprecations)
Identifies the use of deprecated modules in your playbooks. Deprecated modules can pose security r