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

RequirementMinimum VersionInstall Command
VS Code1.85+Download
Python3.9+sudo dnf install python3 or brew install python
Ansible2.14+pip install ansible
ansible-lint6.0+pip install ansible-lint

Step 1: Install the Ansible Extension

  1. Open VS Code
  2. Click the Extensions icon (⇧⌘X / Ctrl+Shift+X)
  3. Search for "Ansible"
  4. Install "Ansible" by Red Hat (not the community one)

Or install from the command line:

code --install-extension redhat.ansible

What the Extension Provides

FeatureDescription
Syntax highlightingYAML + Jinja2 color coding
Auto-completionModule names, parameters, and values
Hover documentationModule docs on mouse hover
ansible-lint integrationReal-time linting in the editor
YAML validationSchema-based YAML validation
Go to definitionNavigate to role/task definitions
Code snippetsQuick 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
ExtensionPurpose
YAML (Red Hat)Enhanced YAML language support
JinjaJinja2 template syntax highlighting
Remote - SSHEdit files on remote servers
GitLensGit blame and history inline
indent-rainbowVisualize indentation levels
Even Better TOMLFor 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:

PrefixExpands To
taskNew task skeleton
playNew play skeleton
blockBlock/rescue/always
whenConditional task
loopTask with loop
handlerHandler skeleton

Keyboard Shortcuts

ShortcutAction
Ctrl+SpaceTrigger auto-completion
F12Go to definition
Ctrl+Shift+MShow 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

  1. Check the file is recognized as Ansible (bottom-right language indicator)
  2. Click the language indicator → select "Ansible"
  3. 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 .yml files) — add "prettier.disableLanguages": ["ansible", "yaml"]

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.