Demonstrates using the VS Code Ansible extension and ansible-lint to lint, validate, and fix playbooks for consistency and reliability before execution.
Use this file to discover all available pages before exploring further.
In this lesson we’ll demonstrate how to use the VS Code Ansible extension together with ansible-lint to validate syntax, indentation, and common logic issues before running playbooks. Treat this workflow as a final quality gate that improves reliability and maintainability of automation code.What you’ll learn:
How the VS Code Ansible extension provides real-time diagnostics and autocompletion.
How ansible-lint enforces best practices (FQCNs, naming, etc.).
A typical edit → lint → fix → run cycle for a small playbook.
Scenario
Imagine joining a DevOps automation team that has accumulated many playbooks written by different engineers. Your objective is to restore consistency, avoid regressions, and make playbooks easier to review. The combination of the Red Hat Ansible extension for VS Code and ansible-lint helps enforce those standards with minimal friction.This demo follows a straightforward workflow:
Create a working folder and a sample inventory/playbook.
Observe real-time validation in VS Code.
Introduce an intentional error to see diagnostic feedback.
Run ansible-lint and apply its suggestions.
Execute the validated playbook.
Getting started — create the project folder and files
student@control:~$ mkdir validationstudent@control:~$ cd validation/student@control:~/validation$ vim inventorystudent@control:~/validation$ vim ansible.cfg
Example minimal inventory (one host named servera):
servera
Minimal ansible.cfg to use the local inventory and enable privilege escalation:
Open the validation folder in VS Code and create playbook.yml.
Authoring the initial playbook
The Ansible extension in VS Code will provide autocompletion for hosts (from your inventory), modules, and module parameters. Below is a small playbook with a deliberate mistake in the debug task to trigger diagnostics.
Running ansible-lint from the editor
If you configure ansible-lint integration in VS Code (or run it from the terminal), linting will recommend best practices that don’t necessarily stop execution but improve consistency and readability—e.g., using fully qualified collection names (FQCNs) and consistent task naming.Apply simple lint feedback: use FQCNs and consistent task naming
The extension and ansible-lint will warn that dnff is not a known module—this usually indicates a misspelling or a missing collection. Correct it back to ansible.builtin.dnf.Final lint-clean playbook
After applying corrections and following lint suggestions (task names, FQCNs, newline at EOF), your playbook should be clean and readable:
Avoids ambiguity when multiple collections provide modules
Change dnf: to ansible.builtin.dnf:
Consistent task naming
Improves readability in logs and reports
Use Install httpd instead of mixed styles
Avoid unused vars or misleading messages
Prevents confusion and accidental errors
Ensure debug: uses msg: correctly
Ensure YAML structure is correct
Prevents runtime errors and invalid playbooks
Use proper indentation and module parameter blocks
Use the extension to jump to module documentation or use “Peek Definition” in VS Code—this gives immediate access to module options and examples without leaving the editor.
Tips and references
Click module names in VS Code to open module docs or press Peek Definition for inline summaries.
Hover over parameters to read short descriptions and expected types.
If you run ansible-lint locally, ensure it’s installed in the environment that VS Code uses (e.g., same Python interpreter or virtualenv).