AnsiblePilot — Master Ansible Automation

AnsiblePilot is the leading resource for learning Ansible automation, DevOps, and infrastructure as code. Browse over 1,400 tutorials covering Ansible modules, playbooks, roles, collections, and real-world examples. Whether you are a beginner or an experienced engineer, our step-by-step guides help you automate Linux, Windows, cloud, containers, and network infrastructure.

Popular Topics

About Luca Berton

Luca Berton is an Ansible automation expert, author of 8 Ansible books published by Apress and Leanpub including "Ansible for VMware by Examples" and "Ansible for Kubernetes by Example", and creator of the Ansible Pilot YouTube channel. He shares practical automation knowledge through tutorials, books, and video courses to help IT professionals and DevOps engineers master infrastructure automation.

Install and Configure Ansible Extension for VSCode

By Luca Berton · Published 2024-01-01 · Category: installation

Learn how to install and configure the Ansible extension for VSCode. Follow this guide to streamline your Ansible playbook and role development process.

Install and Configure Ansible Extension for VSCode

As an Ansible automation expert, I often use Visual Studio Code (VSCode) as my integrated development environment (IDE) for writing Ansible playbooks and roles. VSCode provides a powerful and user-friendly interface that streamlines the development process. In this article, I will guide you through the steps to install Ansible VSCode on your local machine.

Step 1: Install VSCode

The first step is to install Visual Studio Code on your local machine. You can download and install VSCode from the official website. Once installed, open VSCode and navigate to the extensions tab on the left-hand side of the screen.

Step 2: Search for the Ansible Extension

In the extensions tab, search for the Ansible extension by typing "ansible" in the search bar. Select the "Ansible" extension by Microsoft from the list of results. Click on the green "Install" button to install the extension.

Step 3: Install Required Dependencies

In addition to the Ansible extension, you will also need to install a few dependencies to use Ansible with VSCode. These dependencies include:
  • Python: VSCode uses Python to execute Ansible playbooks and roles. Install Python 3.6 or later if you don't have it installed already.
  • Ansible: Install Ansible on your local machine using pip. You can install Ansible by running the command pip install ansible.
  • Ansible-Lint: Ansible-Lint is a tool that checks your Ansible code for potential issues and errors. Install Ansible-Lint by running the command pip install ansible-lint.

Step 4: Configure VSCode

Once you have installed the Ansible extension and its dependencies, you need to configure VSCode to use them. Open your VSCode settings by clicking on the gear icon in the bottom left-hand corner and selecting "Settings".

In the search bar at the top of the settings tab, type "ansible". This will show you the Ansible-related settings in VSCode. You can configure the following settings:

  • Ansible: Path: Set the path to the Ansible executable on your local machine.
  • Python: Set the path to the Python executable on your local machine.
  • Ansible: Configuration Files: Set the path to your Ansible configuration files.
  • Ansible: Galaxy Server Url: Set the URL of the Galaxy server to use for Ansible Galaxy roles.

Result

Visual Studio Code editor with the Red Hat Ansible extension installed

Configure it in settings.json

The settings above can also be edited directly. Open the Command Palette (Ctrl+Shift+P / Cmd+Shift+P), run Preferences: Open User Settings (JSON), and point the extension at the Python environment where you installed Ansible:

{
  "ansible.python.interpreterPath": "/path/to/venv/bin/python",
  "ansible.validation.lint.enabled": true,
  "ansible.validation.lint.path": "/path/to/venv/bin/ansible-lint"
}

Fix "Ansible not found in the environment"

This is the most common first-run problem: the extension installs fine but pops up "Ansible not found in the environment" (or shows no linting/autocompletion). It means the extension's Python interpreter can't see your ansible install — almost always because Ansible lives in a virtual environment the extension isn't using. To fix it:

  1. Confirm where Ansible is installed: which ansible (or pip show ansible).
  2. Set ansible.python.interpreterPath to the Python from that same environment
(the bin/python next to your ansible binary).
  1. Reload the window — Developer: Reload Window from the Command Palette.

Enable autocompletion on your playbooks

The extension only provides IntelliSense, hover docs, and validation when the file is recognized as the Ansible language, which it auto-detects for common paths. To force it on a specific file, add a modeline as the first line:

---
# code: language=ansible

Or map your playbook/task paths to the Ansible language globally in settings.json:

{
  "files.associations": {
    "**/playbooks/*.yml": "ansible",
    "**/tasks/*.yml": "ansible"
  }
}

Conclusion

Visual Studio Code is a powerful IDE for writing Ansible playbooks and roles. By following the steps outlined in this article, you can easily install and configure the Ansible extension for VSCode on your local machine. With VSCode, you can streamline your Ansible development process and catch potential issues and errors with Ansible-Lint.

See also: Ansible expect Module: Automate Interactive Commands (Examples)

Category: installation

Browse all Ansible tutorials · AnsiblePilot Home