Introduction
Visual Studio Code with the Red Hat Ansible extension is the best IDE setup for Ansible development. It provides syntax highlighting, auto-completion, inline documentation, ansible-lint integration, and Jinja2 template support — all in one editor. This guide covers installation, configuration, and productivity tips.
Prerequisites
| Requirement | Minimum Version | Install Command |
|---|---|---|
| VS Code | 1.85+ | Download |
| Python | 3.9+ | sudo dnf install python3 or brew install python |
| Ansible | 2.14+ | pip install ansible |
| ansible-lint | 6.0+ | pip install ansible-lint |
Step 1: Install the Ansible Extension
- Open VS Code
- Click the Extensions icon (⇧⌘X / Ctrl+Shift+X)
- Search for "Ansible"
- Install "Ansible" by Red Hat (not the community one)
Or install from the command line:
code --install-extension redhat.ansible
What the Extension Provides
| Feature | Description |
|---|---|
| Syntax highlighting | YAML + Jinja2 color coding |
| Auto-completion | Module names, parameters, and values |
| Hover documentation | Module docs on mouse hover |
| ansible-lint integration | Real-time linting in the editor |
| YAML validation | Schema-based YAML validation |
| Go to definition | Navigate to role/task definitions |
| Code snippets | Quick templates for tasks, plays, roles |
Step 2: Install Python Dependencies
# Create a virtual environment (recommended)
python3 -m venv ~/.venvs/ansible
source ~/.venvs/ansible/bin/activate
# Install Ansible and tools
pip install ansible ansible-lint yamllint molecule
# Verify
ansible --version
ansible-lint --version
Windows Setup
# Install Python from python.org or Microsoft Store
pip install ansible-core ansible-lint
# Note: Full Ansible on Windows requires WSL for most modules
# Install WSL: wsl --install
Step 3: Configure VS Code Settings
Open settings (⌘, / Ctrl+,) and search for "ansible", or edit settings.json directly:
{
// Associate YAML files in ansible directories with Ansible language
"files.associations": {
"**/playbooks/*.yml": "ansible",
"**/roles/**/*.yml": "ansible",
"**/tasks/*.yml": "ansible",
"**/handlers/*.yml": "ansible",
"**/group_vars/*.yml": "ansible",
"**/host_vars/*.yml": "ansible",
"inventory/*.yml": "ansible"
},
// Ansible extension settings
"ansible.python.interpreterPath": "~/.venvs/ansible/bin/python",
"ansible.ansible.path": "~/.venvs/ansible/bin/ansible",
"ansible.validation.lint.enabled": true,
"ansible.validation.lint.path": "~/.venvs/ansible/bin/ansible-lint",
"ansible.completion.provideRedirectModules": true,
"ansible.completion.provideModuleOptionAliases": true,
// YAML settings for Ansible files
"[ansible]": {
"editor.tabSize": 2,
"editor.insertSpaces": true,
"editor.autoIndent": "advanced",
"editor.quickSuggestions": {
"comments": true,
"other": true,
"strings": true
}
},
// General YAML settings
"[yaml]": {
"editor.tabSize": 2,
"editor.insertSpaces": true
}
}
Step 4: Configure ansible-lint
Create .ansible-lint in your project root:
---
profile: production
# Exclude paths
exclude_paths:
- .github/
- .venv/
- collections/
# Custom rules
skip_list:
- yaml[line-length]
# Enable auto-fix
enable_list:
- fqcn
- no-changed-when
Recommended Additional Extensions
| Extension | Purpose |
|---|---|
| YAML (Red Hat) | Enhanced YAML language support |
| Jinja | Jinja2 template syntax highlighting |
| Remote - SSH | Edit files on remote servers |
| GitLens | Git blame and history inline |
| indent-rainbow | Visualize indentation levels |
| Even Better TOML | For ansible.cfg TOML format |
Install all at once:
code --install-extension redhat.vscode-yaml
code --install-extension samuelcolvin.jinjahtml
code --install-extension ms-vscode-remote.remote-ssh
code --install-extension eamodio.gitlens
code --install-extension oderwat.indent-rainbow
Productivity Tips
Snippets
Type these prefixes and press Tab:
| Prefix | Expands To |
|---|---|
task | New task skeleton |
play | New play skeleton |
block | Block/rescue/always |
when | Conditional task |
loop | Task with loop |
handler | Handler skeleton |
Keyboard Shortcuts
| Shortcut | Action |
|---|---|
| Ctrl+Space | Trigger auto-completion |
| F12 | Go to definition |
| Ctrl+Shift+M | Show Problems panel (lint errors) |
| Ctrl+` | Toggle terminal |
| Ctrl+Shift+P → "Ansible: Run" | Run current playbook |
Multi-Root Workspace
For projects with separate roles, collections, and playbooks:
// myproject.code-workspace
{
"folders": [
{ "path": "playbooks", "name": "Playbooks" },
{ "path": "roles", "name": "Roles" },
{ "path": "collections", "name": "Collections" }
],
"settings": {
"ansible.python.interpreterPath": "~/.venvs/ansible/bin/python"
}
}
Troubleshooting
"ansible-lint not found"
// Point to the exact path
"ansible.validation.lint.path": "/home/user/.venvs/ansible/bin/ansible-lint"
Auto-completion Not Working
- Check the file is recognized as Ansible (bottom-right language indicator)
- Click the language indicator → select "Ansible"
- Verify Python path is correct in settings
YAML Files Not Detected as Ansible
Add explicit file associations in settings.json:
"files.associations": {
"*.yml": "ansible"
}
Or for more targeted detection, use directory-based patterns (shown in Step 3).
Extension Conflicts
Disable these if they conflict with the Ansible extension:
- Other YAML formatters that override indentation
- Prettier (for
.ymlfiles) — add"prettier.disableLanguages": ["ansible", "yaml"]
Links
Related Articles
- VS Code for Ansible Development
- Ansible-Lint Guide
- Ansible Best Practices Guide
- Install Ansible on Ubuntu
- Ansible Configuration Settings
Conclusion
Install the Red Hat Ansible extension, configure your Python interpreter path, enable ansible-lint validation, and set up file associations for automatic Ansible language detection. Use 2-space indentation (never tabs), add .ansible-lint to your project root, and install complementary extensions (YAML, Jinja, Remote-SSH) for the complete development experience.