ansible / vscode-ansible

vscode/vscodium extension for providing Ansible auto-completion and integrating quality assurance tools like ansible-lint, ansible syntax check, yamllint, molecule and ansible-test.
https://ansible.readthedocs.io/projects/vscode-ansible/
MIT License
337 stars 77 forks source link
ansible-dev-tools ansible-language-server ansible-lint hack hacktoberfest lsp molecule taskfile vscode-extension

Ansible VS Code Extension by Red Hat

This extension adds language support for Ansible to Visual Studio Code and OpenVSX compatible editors by leveraging ansible-language-server.

Language association to yaml files

The extension works only when a document is assigned ansible language. The following method is used to assign ansible language to the document opened by the extension:

Without file inspection

Additionally, in VS Code, you can add persistent file association for language to settings.json file like this:

{
  ...

  "files.associations": {
    "*plays.yml": "ansible",
    "*init.yml": "yaml",
  }
}

With file inspection

File inspection for ansible keywords

Modelines (optional)

# code: language=ansible
or
# code: language=yaml

Rest all the .yml, or .yaml files will remain yaml by default unless the user explicitly changes the language to ansible for which the process is mentioned below.

Activating Red Hat Ansible extension manually

It is recommended to open a folder containing Ansible files with a VS Code workspace.

Linter support

Note:

Features

Syntax highlighting

Syntax highlighting

Ansible keywords, module names and module options, as well as standard YAML elements are recognized and highlighted distinctly. Jinja expressions are supported too, also those in Ansible conditionals (when, failed_when, changed_when, check_mode), which are not placed in double curly braces.

The screenshots and animations presented in this README have been taken using the One Dark Pro theme. The default VS Code theme will not show the syntax elements as distinctly, unless customized. Virtually any theme other than default will do better.

Validation

YAML validation

While you type, the syntax of your Ansible scripts is verified and any feedback is provided instantaneously.

Integration with ansible-lint

Linter support

On opening and saving a document, ansible-lint is executed in the background and any findings are presented as errors. You might find it useful that rules/tags added to warn_list (see Ansible Lint Documentation) are shown as warnings instead.

Smart autocompletion

Autocompletion

The extension tries to detect whether the cursor is on a play, block or task etc. and provides suggestions accordingly. There are also a few other rules that improve user experience:

Auto-closing Jinja expressions

Easier Jinja expression typing

When writing a Jinja expression, you only need to type "{{, and it will be mirrored behind the cursor (including the space). You can also select the whole expression and press space to put spaces on both sides of the expression.

Documentation reference

Documentation on hover

Documentation is available on hover for Ansible keywords, modules and module options. The extension works on the same principle as ansible-doc, providing the documentation straight from the Python implementation of the modules.

Jump to module code

Go to code on Ctrl+click

You may also open the implementation of any module using the standard Go to Definition operation, for instance, by clicking on the module name while holding ctrl/cmd.

Ansible Lightspeed with watsonx Code Assistant

AI based Ansible code recommendations

Requirements

For Windows users, this extension works perfectly well with extensions such as Remote - WSL and Remote - Containers.

If you have any other extension providing language support for Ansible, you might need to uninstall it first.

Configuration

This extension supports multi-root workspaces, and as such, can be configured on any level (User, Remote, Workspace and/or Folder).

Data and Telemetry

The vscode-ansible extension collects anonymous usage data and sends it to Red Hat servers to help improve our products and services. Read our privacy statement to learn more. This extension respects the redhat.telemetry.enabled setting, which you can learn more about at https://github.com/redhat-developer/vscode-redhat-telemetry#how-to-disable-telemetry-reporting

Known limitations

Contact

Credit

Based on the good work done by Tomasz Maciążek