coc-diagnostic icon indicating copy to clipboard operation
coc-diagnostic copied to clipboard

ansible-lint integration

Open utrumo opened this issue 5 years ago • 3 comments

I have a problem with ansible-lint integration with coc-diagnostic.

This linter can check files only recursive. There is a link on problem: https://github.com/ansible/ansible-lint/issues/667

There is a my config for it:

"ansible-lint": {
  "command": "ansible-lint",
  "isStderr": false,
  "isStdout": true,
  "args": [
    "--parseable-severity",
    "%filepath"
  ],
  "rootPatterns": ["ansible.cfg"],
  "debounce": 100,
  "offsetLine": 0,
  "offsetColumn": 0,
  "sourceName": "ansible-lint",
  "formatLines": 1,
  "formatPattern": [
    "^[^:]+:(\\d+):\\s(\\S+)\\s\\[(\\S+)\\]\\s(.*)$",
    {
      "line": 1,
      "security": 3,
      "message": [2, " [",  3, "] ", 4]
    }
  ],
  "securities": {
    "VERY_HIGHT": "error",
    "HIGHT": "error",
    "MEDIUM": "warning",
    "LOW": "warning",
    "VERY_LOW": "warning",
    "INFO": "info"
  }
}

Can i somehow to ignore not needed output about another files? I was write this stupid small script and add it to coc-settings config, but ,it did not work with coc-diagnostic, i don't know why:

#!/bin/bash
path=$1
pathCutted=$(echo $path | cut -c 2-)
cd / && ansible-lint --parseable-severity $path | grep $pathCutted

utrumo avatar Jan 25 '20 11:01 utrumo

Can i somehow to ignore not needed output about another files?

Not support yet.

iamcco avatar Jan 27 '20 06:01 iamcco

I was also using a wrapper script, but after a recent commit https://github.com/iamcco/diagnostic-languageserver/commit/03a713e1ac762b9d1bf786a348adefcd666d1ce8 I was able to use ansible-lint directly.

Example config:

{
  "ansible_lint":{
    "command":"ansible-lint",
    "args":[
      "--parseable-severity",
      "-w",
      "%file"
    ],
    "rootPatterns":[
      "tasks",
      "ansible.cfg",
      ".git"
    ],
    "sourceName":"ansible-lint",
    "formatPattern":[
      "^([^:]+):(\\d+):\\s*(\\S+\\s*)\\[(\\S+?)\\]\\s*(.*)$",
      {
        "sourceName":1,
        "sourceNameFilter":true,
        "line":2,
        "security":4,
        "message":[
          3,
          5
        ]
      }
    ],
    "securities":{
      "VERY_HIGH":"error",
      "HIGH":"warning",
      "MEDIUM":"warning",
      "LOW":"info",
      "VERY_LOW":"hint",
      "INFO":"hint"
    }
  }
}

There is two important points:

  • ansible-lint accepts a list of Ansible playbook files or a list of role directories. It means that we can't provide path to roles/role_name/tasks/main.yml file. We should point it to roles/role_name directory. If we don't provide any path, than auto-detection mode is triggered, which searches for all roles/playbooks in current directory. So I added tasks to rootPatterns. Language server should run it inside role directory (e.g. roles/role_name), if it's some file inside role, or in your project directory for other files. Latter is pretty slow (because it analyses anything), but works.
  • I couldn't find a way to tell language server to run it without stdin and %file at the same time, so I added -w %file to args. -w is a warn_list, not filename, so effectively it runs it without filename (auto-detection mode), but language server isn't using stdin.

It uses filename as sourceName. I would prefer it to show "ansible-lint" instead, but it's required or "sourceNameFilter": true wouldn't work.

stasjok avatar Sep 01 '20 12:09 stasjok

Another solution is to simply use https://github.com/ansible/ansible-language-server instead which has ansible-lint integration.

lithammer avatar Dec 06 '21 13:12 lithammer