ansible.md

  1---
  2title: Ansible
  3description: "Configure Ansible language support in Zed, including language servers, formatting, and debugging."
  4---
  5
  6# Ansible
  7
  8Support for Ansible in Zed is provided via a community-maintained [Ansible extension](https://github.com/kartikvashistha/zed-ansible).
  9
 10- Tree-sitter: [zed-industries/tree-sitter-yaml](https://github.com/zed-industries/tree-sitter-yaml)
 11- Language Server: [ansible/vscode-ansible](https://github.com/ansible/vscode-ansible/tree/main/packages/ansible-language-server)
 12
 13## Setup
 14
 15### File detection
 16
 17To avoid mishandling non-Ansible YAML files, the Ansible Language is not associated with any file extensions by default.
 18
 19To change this behavior, you can add a `"file_types"` section to Zed settings inside your project (`.zed/settings.json`) or your Zed user settings (`~/.config/zed/settings.json`) to match your folder/naming conventions. For example:
 20
 21```json [settings]
 22{
 23  "file_types": {
 24    "Ansible": [
 25      "**.ansible.yml",
 26      "**.ansible.yaml",
 27      "**/defaults/*.yml",
 28      "**/defaults/*.yaml",
 29      "**/meta/*.yml",
 30      "**/meta/*.yaml",
 31      "**/tasks/*.yml",
 32      "**/tasks/*.yaml",
 33      "**/handlers/*.yml",
 34      "**/handlers/*.yaml",
 35      "**/group_vars/*.yml",
 36      "**/group_vars/*.yaml",
 37      "**/host_vars/*.yml",
 38      "**/host_vars/*.yaml",
 39      "**/playbooks/*.yml",
 40      "**/playbooks/*.yaml",
 41      "**playbook*.yml",
 42      "**playbook*.yaml"
 43    ]
 44  }
 45}
 46```
 47
 48Feel free to modify this list as per your needs.
 49
 50#### Inventory
 51
 52If your inventory file is in the YAML format, you can either:
 53
 54- Append the `ansible-lint` inventory JSON schema to it via the following comment at the top of your inventory file:
 55
 56```yml
 57# yaml-language-server: $schema=https://raw.githubusercontent.com/ansible/ansible-lint/main/src/ansiblelint/schemas/inventory.json
 58```
 59
 60- or, configure the YAML language server settings to set this schema for all your inventory files, that match your inventory pattern, under your Zed settings ([ref](https://zed.dev/docs/languages/yaml)):
 61
 62```json [settings]
 63{
 64  "lsp": {
 65    "yaml-language-server": {
 66      "settings": {
 67        "yaml": {
 68          "schemas": {
 69            "https://raw.githubusercontent.com/ansible/ansible-lint/main/src/ansiblelint/schemas/inventory.json": [
 70              "./inventory/*.yaml",
 71              "hosts.yml"
 72            ]
 73          }
 74        }
 75      }
 76    }
 77  }
 78}
 79```
 80
 81### LSP Configuration
 82
 83By default, the following configuration is passed to the Ansible language server. It conveniently mirrors the defaults set by [nvim-lspconfig](https://github.com/neovim/nvim-lspconfig/blob/03bc581e05e81d33808b42b2d7e76d70adb3b595/lua/lspconfig/configs/ansiblels.lua) for the Ansible language server:
 84
 85```json
 86{
 87  "ansible": {
 88    "ansible": {
 89      "path": "ansible"
 90    },
 91    "executionEnvironment": {
 92      "enabled": false
 93    },
 94    "python": {
 95      "interpreterPath": "python3"
 96    },
 97    "validation": {
 98      "enabled": true,
 99      "lint": {
100        "enabled": true,
101        "path": "ansible-lint"
102      }
103    }
104  }
105}
106```
107
108> **Note:** In order for linting to work, ensure that `ansible-lint` is installed and discoverable on your `$PATH`.
109
110When desired, any of the above default settings can be overridden under the `"lsp"` section of your Zed settings file. For example:
111
112```json [settings]
113{
114  "lsp": {
115    // The Zed Ansible extension prefixes all settings with `ansible`
116    // so use `ansible.path` instead of `ansible.ansible.path`.
117    "ansible-language-server": {
118      "settings": {
119        "ansible": {
120          "path": "ansible"
121        },
122        "executionEnvironment": {
123          "enabled": false
124        },
125        "python": {
126          "interpreterPath": "python3"
127        },
128        "validation": {
129          "enabled": false,
130          "lint": {
131            "enabled": false,
132            "path": "ansible-lint"
133          }
134        }
135      }
136    }
137  }
138}
139```
140
141A full list of options/settings that can be passed to the server can be found at the project's page [here](https://github.com/ansible/vscode-ansible/blob/main/docs/als/settings.md).