
ansible-lint --profile=production est le filtre qualité le plus strict d’ansible-lint. Il vérifie ~80 règles : FQCN partout, pas de command: sans creates:, présence d’argument_specs.yml, gestion des secrets, naming conventions. Tout rôle destiné à publication doit passer ce profil.
Cette page configure ansible-lint, yamllint et pre-commit ensemble — la stack qualité 2026 pour un rôle Ansible.
Ce que vous allez apprendre
Section intitulée « Ce que vous allez apprendre »- Les 6 profils d’ansible-lint (min → production).
- Configurer
.ansible-lintavec exclude_paths, warn_list, mock_modules. - Configurer
.yamllintstrict (pas deyes/noambigus). - Pre-commit hooks pour bloquer les commits non conformes.
- Tester le linter en local et en CI.
Les 6 profils ansible-lint
Section intitulée « Les 6 profils ansible-lint »min → basic → moderate → safety → shared → productionChaque profil englobe les règles des précédents. Plus on monte, plus c’est strict.
| Profil | Cas d’usage |
|---|---|
min | Premier déchiffrage YAML/Ansible — quasi tout passe |
basic | Conventions de base (FQCN, naming) |
moderate | + idempotence, no_log, secret detection |
safety | + sécurité avancée (no shell sans creates, no_log password) |
shared | + qualité des rôles partagés (README, meta) |
production | + argument_specs, version pinning, exhaustivité |
Recommandation 2026 : moderate pendant le dev, production avant publication.
Le fichier .ansible-lint
Section intitulée « Le fichier .ansible-lint »---profile: production
exclude_paths: - .cache/ - .git/ - molecule/ - tests/
skip_list: [] # règles ignorées (avec justification)
warn_list: - experimental # règles expérimentales en warn - role-name # autorise les '-' dans noms (Galaxy)
use_default_rules: trueverbosity: 1
enable_list: - args - empty-string-compare - no-log-password - no-same-owner - yaml
mock_modules: - amazon.aws.ec2_instance # collections optionnelles non installées
mock_roles: - geerlingguy.dockerChamps clés :
profile: production: la base.exclude_paths: chemins à ignorer (cache, tests Molecule).skip_list: règles à skipper (à utiliser avec parcimonie + justification en commentaire).warn_list: règles en warn au lieu d’erreur.mock_modules/mock_roles: modules/rôles cités dans le code mais non installés (évite les warnings).
Le fichier .yamllint
Section intitulée « Le fichier .yamllint »---extends: default
rules: line-length: max: 160 level: warning
truthy: allowed-values: - "true" - "false" check-keys: false
comments: min-spaces-from-content: 1
comments-indentation: false document-start: enable empty-lines: max: 2
ignore: | .cache/ molecule/ .tox/Règle critique — truthy.allowed-values: [true, false] : interdit yes/no/on/off qui sont des strings en YAML 1.2 strict (et provoquent des comportements subtils).
Pre-commit hooks
Section intitulée « Pre-commit hooks »---repos: - repo: https://github.com/pre-commit/pre-commit-hooks rev: v4.6.0 hooks: - id: trailing-whitespace - id: end-of-file-fixer - id: check-yaml - id: check-added-large-files - id: detect-private-key - id: check-merge-conflict
- repo: https://github.com/adrienverge/yamllint.git rev: v1.35.1 hooks: - id: yamllint args: [-c, .yamllint]
- repo: https://github.com/ansible/ansible-lint rev: v25.1.0 hooks: - id: ansible-lint args: ["--profile=production"] additional_dependencies: - ansible-core>=2.16Installation :
pipx install pre-commitpre-commit install # active les hookspre-commit run --all-files # lance sur tout le repo (1ère fois)À partir de là, git commit déclenche automatiquement les linters. Si un hook échoue, le commit est bloqué — vous corrigez, vous re-stagez, vous re-commitez.
Lancer les linters manuellement
Section intitulée « Lancer les linters manuellement »# yamllintyamllint roles/
# ansible-lintansible-lint --profile=production roles/webserver/
# Pre-commit (équivalent au hook automatique)pre-commit run --all-files
# Pre-commit hook spécifiquepre-commit run ansible-lint --all-filesPièges courants en production
Section intitulée « Pièges courants en production »| Règle | Détecte | Fix |
|---|---|---|
fqcn[action-core] | Module sans FQCN (dnf: au lieu de ansible.builtin.dnf:) | Toujours FQCN |
args | Argument manquant requis | Ajouter le param |
no-changed-when | command: sans changed_when: | Ajouter changed_when: false ou creates: |
no-log-password | Variable *password* sans no_log: true | Ajouter no_log: true sur la tâche |
var-naming | Variable mal nommée | snake_case + préfixe rôle |
meta-no-info | meta/main.yml incomplet | Compléter galaxy_info: |
latest-version | state: latest sur paquet | Pinner avec state: present, version: X |
Justifier un skip avec commentaire
Section intitulée « Justifier un skip avec commentaire »- name: Lancer le script de migration legacy ansible.builtin.shell: /opt/migrate.sh args: creates: /var/lib/.migrated # noqa: command-instead-of-shell # Le script fait du redirect bash incompatible avec command:# noqa: <règle> désactive localement la règle. Toujours justifier en commentaire — sinon code review bloque.
Pratiquer dans le lab
Section intitulée « Pratiquer dans le lab »Cette page a un lab d’accompagnement : labs/tests/ansible-lint-production/ dans
stephrobert/ansible-training.
Le lab fournit .ansible-lint (production), .yamllint (strict), .pre-commit-config.yaml (hooks). 7 tests structure validés.
cd ~/Projets/ansible-training/labs/tests/ansible-lint-production/
cat .ansible-lintcat .yamllintcat .pre-commit-config.yamlpytest -v challenge/tests/Pour tester en réel :
ansible-lint --profile=production roles/yamllint roles/pre-commit run --all-filesÀ retenir
Section intitulée « À retenir »- 6 profils ansible-lint :
min→basic→moderate→safety→shared→production. productionmandatory pour publication Galaxy..yamllint: interdireyes/no(truthy.allowed-values: [true, false]).- Pre-commit hooks =
ansible-lint,yamllint,detect-private-key,check-yaml. # noqa: <règle>+ commentaire pour skip local justifié.