Visual Studio Code (VS Code) is the most popular editor for Ansible development, thanks to Red Hat's official Ansible extension that provides syntax highlighting, auto-completion, linting, and AI-powered code generation through Ansible Lightspeed.

Why VS Code for Ansible?

While you can write Ansible playbooks in any text editor, VS Code offers specific advantages:

  • Ansible-aware syntax highlighting that distinguishes modules, parameters, and Jinja2 expressions
  • Real-time ansible-lint integration that catches errors as you type
  • Intelligent auto-completion for module names, parameters, and values
  • Ansible Lightspeed AI that generates task code from natural language descriptions
  • Integrated terminal for running playbooks without leaving the editor
  • YAML schema validation specific to Ansible playbook structure

Prerequisites

Before setting up VS Code for Ansible:

  • VS Code 1.70.1 or later (download from code.visualstudio.com)
  • Python 3.9+ installed on your system
  • Ansible installed (pip install ansible or system package)
  • ansible-lint recommended (pip install ansible-lint)

Installing the Red Hat Ansible Extension

Step-by-Step Installation

  1. Open VS Code and click the Extensions icon in the sidebar (or press Ctrl+Shift+X / Cmd+Shift+X)
  2. Search for "Ansible" in the Extensions marketplace
  3. Find the extension published by Red Hat (verify the publisher name)
  4. Click Install

Ansible extension on Visual Studio Code

Post-Installation Verification

After installing, verify it works:

  1. Create a new file with .yml extension (e.g., playbook.yml)
  2. Click the language indicator in the bottom-right status bar
  3. Select "Ansible" from the language list (not "YAML")
  4. Start typing — you should see Ansible-specific syntax highlighting

The language indicator is critical: VS Code must recognize the file as "Ansible" (not plain "YAML") for all features to work.

Essential Configuration

Add these to your settings.json (Ctrl+, → Open Settings JSON):

{
  "ansible.python.interpreterPath": "/usr/bin/python3",
  "ansible.validation.enabled": true,
  "ansible.validation.lint.enabled": true,
  "ansible.completion.provideRedirectModules": true,
  "ansible.completion.provideModuleOptionAliases": true,
  "files.associations": {
    "**/playbooks/*.yml": "ansible",
    "**/roles/**/tasks/*.yml": "ansible",
    "**/roles/**/handlers/*.yml": "ansible",
    "**/inventory/**/*.yml": "ansible"
  },
  "editor.tabSize": 2,
  "editor.insertSpaces": true,
  "[ansible]": {
    "editor.autoIndent": "advanced",
    "editor.tabSize": 2
  }
}

File Associations

The files.associations setting automatically detects Ansible files based on path patterns. This prevents you from manually setting the language every time you open a playbook.

Common patterns to add:

PatternPurpose
**/playbooks/*.ymlPlaybook files
**/roles/**/tasks/*.ymlRole tasks
**/roles/**/handlers/*.ymlRole handlers
**/roles/**/defaults/*.ymlRole defaults
**/group_vars/**/*.ymlGroup variables
**/host_vars/**/*.ymlHost variables

Key Features

Syntax Highlighting

The Ansible extension provides context-aware highlighting:

  • Module names in a distinct color (e.g., ansible.builtin.copy)
  • Parameters vs values differentiated visually
  • Jinja2 expressions ({{ variable }}) highlighted within YAML strings
  • Comments and tags clearly visible

Auto-Completion

Start typing and press Ctrl+Space to trigger suggestions:

  • Module names: Type ansible.builtin. to see all built-in modules
  • Module parameters: After a module name, get parameter suggestions with documentation
  • Parameter values: For enum parameters like state:, see valid values (present, absent, etc.)
  • Jinja2 filters: Inside {{ }}, get filter suggestions like default, join, regex_search

Real-Time Linting

With ansible-lint installed, the extension checks your playbooks as you type:

  • Syntax errors: Missing colons, incorrect indentation
  • Best practice violations: Missing task names (name:), use of command instead of specific modules
  • Deprecated features: Old module names, removed parameters
  • FQCN warnings: Suggests using fully qualified collection names

Lint errors appear as squiggly underlines with hover-over explanations.

Ansible Lightspeed AI

Enable Lightspeed for AI-powered code generation:

  1. Open Settings → search "Ansible Lightspeed"
  2. Check "Enable Ansible Lightspeed"
  3. Sign in with your GitHub or Red Hat account

Ansible Lightspeed setting on Visual Studio Code

Once enabled, type a descriptive task name and Lightspeed generates the corresponding Ansible code as ghost text. Press Tab to accept.

For a deep dive, see our Ansible Lightspeed Complete Guide.

Keyboard Shortcuts for Ansible Development

ShortcutAction
Ctrl+SpaceTrigger auto-completion
Ctrl+Shift+P → "Ansible"Access Ansible commands
`Ctrl+``Open integrated terminal
Ctrl+Shift+MView Problems panel (lint errors)
F2Rename symbol
Ctrl+/Toggle comment
Alt+Up/DownMove line up/down
Ctrl+DSelect next occurrence
Ctrl+Shift+KDelete line
TabAccept Lightspeed suggestion

Complementary Extensions

Enhance your Ansible workflow with these additional VS Code extensions:

ExtensionPurpose
YAML (Red Hat)Advanced YAML support and schema validation
Remote - SSHEdit playbooks on remote servers
GitLensGit blame and history for playbook changes
indent-rainbowVisualize YAML indentation levels
Better JinjaEnhanced Jinja2 template highlighting
DockerManage Execution Environments

Workspace Setup for Ansible Projects

A well-organized VS Code workspace improves productivity:

ansible-project/
├── .vscode/
│   ├── settings.json     # Project-specific VS Code settings
│   └── extensions.json   # Recommended extensions
├── ansible.cfg           # Ansible configuration
├── inventory/
│   ├── production/
│   └── staging/
├── playbooks/
├── roles/
├── group_vars/
├── host_vars/
└── collections/
    └── requirements.yml

Create .vscode/extensions.json to recommend extensions for your team:

{
  "recommendations": [
    "redhat.ansible",
    "redhat.vscode-yaml",
    "ms-python.python"
  ]
}

Troubleshooting

Extension Not Detecting Ansible Files

  • Check the language indicator in the status bar — switch from "YAML" to "Ansible"
  • Add file associations in settings.json for your project structure
  • Restart VS Code after installing the extension

Linting Not Working

  • Verify ansible-lint is installed: pip show ansible-lint
  • Check the Python interpreter path in extension settings
  • Open the Output panel (Ctrl+Shift+U) → select "Ansible" for error details

Auto-Completion Missing Modules

  • Ensure Ansible is installed in the Python environment VS Code is using
  • Install collections locally: ansible-galaxy collection install community.general
  • Restart the Ansible language server: Ctrl+Shift+P → "Ansible: Restart Ansible Server"

Slow Performance on Large Projects

  • Exclude directories in settings: "files.watcherExclude": { "**/collections/**": true }
  • Disable telemetry for faster startup
  • Use workspace trust to limit scanning

Conclusion

VS Code with the Red Hat Ansible extension is the definitive development environment for Ansible automation. The combination of intelligent syntax highlighting, real-time linting, auto-completion, and Ansible Lightspeed AI creates a workflow where you spend less time looking up documentation and more time writing working automation.

Start with the basic extension installation, configure file associations for your project structure, and gradually enable advanced features like Lightspeed AI as you grow comfortable. The productivity gains compound — especially for teams standardizing on a common development environment.