Ansible-lint --fix

Hi!

I’m trying to set up my developing environment with LazyVim with ansible enabled. Afaik it installs ansible-lint and sets up ansible-language-server.

When I save a file, it seems to run some reformatter. I would say it is the ansible-lint fixer. Can anyone explain to me how is this working?

I have ansible code like this:

property:
  "{{ some long jinja expression that
          I would like to keep indented
      the way I write it }}"

However, the reformatter is modifying it into something like:

property: "{{ some long jinja expression that
      I would like to keep indented
      the way I write it }}"

Can you help me understand what’s going on? Is it the ansible-lint --fix? What property can I setup in the config to avoid this reformatting?

Hey there,

I’m running a similar setup with neovim and ansible LSP via Mason!

The rules that ansible lint autofix actually can fix on it’s own are well documented here.

Ansible lint is getting used to lint the file and provide you with linting feedback, for instance error messages and highlighting those, but should not actually change your code. Afaik it does not use ansible-lint --fix, but you could cross check this with copying examples of provided documentation and if your setup manages to autofix these.

I’d guess that this is due to your LSP setup trying to help you writing “valid” YAML. This also doesn’t necessarily has to be the ansible LSP maybe rather some kind of YAML LSP. So its probably rather an code syntax validation than a linting thingy.

Hope this helps!

2 Likes

Thanks a lot for your answer.

I did browse the documentation but I didn’t find this specific case. It seems that ansible-lint acts as a reformatter that also reformats yaml, so that’s why I thought it was the one to blame.

This is a feature of LazyVim, and here’s a discussion with a few ways to disable or customize the feature:
Best way to disable auto-format on save completely · LazyVim/LazyVim · Discussion #141 · GitHub

2 Likes

Thanks!

I was digging and found this.

  • LazyVim uses both LSP and a formatter.
  • LSP is managed via nvim-lspconfig, but the installation of the required tools is managed via mason and mason-lspconfig.nvim (this one also manages file type association).
  • Formatting is managed via conform.nvim and the autoformat option. It seems that by lazyvim sets prettier for yaml formatting (also installed via mason).

So that, on save it is conform.nvim the one calling prettier which is running using its default configuration.

And finally, prettier-ignore did the trick.

# prettier-ignore
property:
  "{{ some long jinja expression that
          I would like to keep indented
      the way I write it }}"
1 Like