Ansible support in Visual Studio Code

Posted by Christian Jung on Thu, Feb 4, 2021

Update: Since I published this article, I learned that work has been started on a new native Ansible Extension (updated link) for Visual Studio Code.

Microsoft retired the Ansible extension for Visual Studio Code

If you use the VSCode extension for Ansible from Microsoft, you might have noticed that it recently was retired. When looking for alternatives, I found a combination of the Red Hat YAML extension and the JSON Schemastore do the trick. Unfortunately there are still some glitches which I haven’t been able to solve yet.

JSONSchema support

Visual Studio Code combined with the Red Hat YAML extension can use the Ansible schemas provided by the JSON Schema Store. There are currently four schemas available for Ansible:

What I found missing is some configuration in Visual Studio Code to automatically use those. Visual Studio Code can either query them online or from a local file. For better performance and offline support, downloading the files and storing them locally might be the preferred option.

Configuration changes

Install the Red Hat YAML extension by using the Visual Studio Code UI or from the command line:

1code --install-extension redhat.vscode-yaml

Download the schema files you want to use and store them locally:

1mkdir ~/.jsonschema
2cd ~/.jsonschema

Edit your Visual Studio Code settings.json file and add the necessary lines. On Linux you typically find this file in ~/.config/Code/User/settings.json.

1    "yaml.schemas": {
2        // match your locally downloaded JSON Schema to the file glob you want to use
3        "~/.jsonschema/ansible-playbook": ["^/*.yml"],
4        "~/.jsonschema/ansible-role-2.9": ["^/roles/*/tasks/*.yml", "^/tasks/*.yml"]
5        // add the settings for collections and inventories as needed...
6    },

Note: You might have to restart Visual Studio Code to apply those changes.

How to use it

To verify your setup works, create a file in the root of your project, give it a name matching the pattern above (e.g. vscode-test.yml) and start typing a Playbook like this:

VSCode yum example

I find it most useful when writing roles. Check out the following example of an file called roles/example/tasks/main.yml:

VSCode yum module options

You can also get help for module options by simply hovering the mouse pointer over the option in question:

VSCode yum option help

If Visual Studio Code does not automatically detect what you want, try to press Ctrl+Space to get a list of options. In this example, I added a new task and pressed Ctrl+Space to get a list of all Ansible Modules:

VSCode module list


There are a couple of things I haven’t been able to solve or workaround yet:

  • performance is sometimes quite bad - I guess this is due to the fact that the JSONschema is actually rather large

  • there seem to be glitches in the JSON Schema, e.g. sometimes I see errors like this:

    VSCode set_fact module error

    Although this is the correct syntax for setting a fact with the set_fact module, Visual Studio Code is showing the error message.


I only tested this on Fedora 33 and RHEL 8.