ansible.md

  1# Ansible
  2
  3Support for Ansible in Zed is provided via a community-maintained [Ansible extension](https://github.com/kartikvashistha/zed-ansible).
  4
  5- Tree-sitter: [zed-industries/tree-sitter-yaml](https://github.com/zed-industries/tree-sitter-yaml)
  6- Language Server: [ansible/vscode-ansible](https://github.com/ansible/vscode-ansible/tree/main/packages/ansible-language-server)
  7
  8## Setup
  9
 10### File detection
 11
 12To avoid mishandling non-Ansible YAML files, the Ansible Language is not associated with any file extensions by default. To 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:
 13
 14```json
 15"file_types": {
 16    "Ansible": [
 17      "**.ansible.yml",
 18      "**.ansible.yaml",
 19      "**/defaults/*.yml",
 20      "**/defaults/*.yaml",
 21      "**/meta/*.yml",
 22      "**/meta/*.yaml",
 23      "**/tasks/*.yml",
 24      "**/tasks/*.yaml",
 25      "**/handlers/*.yml",
 26      "**/handlers/*.yaml",
 27      "**/group_vars/*.yml",
 28      "**/group_vars/*.yaml",
 29      "**/host_vars/*.yml",
 30      "**/host_vars/*.yaml",
 31      "**/playbooks/*.yml",
 32      "**/playbooks/*.yaml",
 33      "**playbook*.yml",
 34      "**playbook*.yaml"
 35    ]
 36  }
 37```
 38
 39Feel free to modify this list as per your needs.
 40
 41#### Inventory
 42
 43If your inventory file is in the YAML format, you can either:
 44
 45- Append the `ansible-lint` inventory json schema to it via the following comment at the top of your inventory file:
 46
 47```yml
 48# yaml-language-server: $schema=https://raw.githubusercontent.com/ansible/ansible-lint/main/src/ansiblelint/schemas/inventory.json
 49```
 50
 51- 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)):
 52
 53```json
 54"lsp": {
 55    "yaml-language-server": {
 56      "settings": {
 57        "yaml": {
 58          "schemas": {
 59            "https://raw.githubusercontent.com/ansible/ansible-lint/main/src/ansiblelint/schemas/inventory.json": [
 60              "./inventory/*.yaml",
 61              "hosts.yml",
 62            ]
 63          }
 64        }
 65      }
 66    }
 67},
 68```
 69
 70### LSP Configuration
 71
 72By default, the following default config 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:
 73
 74```json
 75{
 76  "ansible": {
 77    "ansible": {
 78      "path": "ansible"
 79    },
 80    "executionEnvironment": {
 81      "enabled": false
 82    },
 83    "python": {
 84      "interpreterPath": "python3"
 85    },
 86    "validation": {
 87      "enabled": true,
 88      "lint": {
 89        "enabled": true,
 90        "path": "ansible-lint"
 91      }
 92    }
 93  }
 94}
 95```
 96
 97> [!NOTE]
 98> In order for linting to work, ensure that `ansible-lint` is installed and discoverable on your PATH
 99
100When desired, any of the above default settings can be overridden under the `"lsp"` section of your Zed settings file. For example:
101
102```json
103"lsp": {
104  // Note, the Zed Ansible extension prefixes all settings with `ansible`
105  // so instead of using `ansible.ansible.path` use `ansible.path`.
106  "ansible-language-server": {
107    "settings": {
108      "ansible": {
109        "path": "ansible"
110      },
111      "executionEnvironment": {
112        "enabled": false
113      },
114      "python": {
115        "interpreterPath": "python3"
116      },
117      "validation": {
118        "enabled": false, // disable validation
119        "lint": {
120          "enabled": false, // disable ansible-lint
121          "path": "ansible-lint"
122        }
123      }
124    }
125  }
126}
127```
128
129A 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/5a89836d66d470fb9d20e7ea8aa2af96f12f61fb/docs/als/settings.md).
130Feel free to modify option values as needed.