Try   HackMD

Ansible integration with vim

Ansible module snippet from ansible-doc

This requires a recent version of jq (>=1.7):

ansible-doc -t module -s -j service | jq -r ' to_entries[] as $module | "\($module.value.doc.plugin_name):\n" + ($module.value.doc.options | to_entries | map( if .value.required then if .value.choices then " \(.key): \(.value.choices | join("|"))" else if .value.default == null then " \(.key): [\(.value.type)]" else " \(.key): \(.value.default)" end end else if .value.choices then " #\(.key): \(.value.choices | join("|"))" else if .value.default == null then " #\(.key): [\(.value.type)]" else " #\(.key): \(.value.default)" end end end ) | join("\n") )'
ansible.builtin.service:
  #arguments: 
  #enabled: [bool]
  name: [str]
  #pattern: [str]
  #runlevel: default
  #sleep: [int]
  #state: reloaded|restarted|started|stopped
  #use: auto
  • optional parameters are commented out
  • default values are specified if existing
  • choices are specified if existing
  • otherwise the expected data type is specified

Vim integration

I use the ansible-vim plugin, mainly for filetype detection.

Below is my ~/.vim/ftplugin/ansible.vim file:

" Use ansible documentation in a new split command! -nargs=+ AnsibleDoc new | \ setlocal buftype=nofile bufhidden=hide noswapfile nobuflisted ft=man | \ execute '0read !ansible-doc' <q-args> | \ silent! %substitute#\e\[[0-9;]*[mK]##g | \ normal gg autocmd BufNewFile,BufRead,BufEnter *.yml setlocal keywordprg=:AnsibleDoc " Modules snippets from ansible-doc " {{{ " Snippet generation function, using `ansible-doc` and `jq` function! AnsibleModuleSnippet(...) if a:0 > 0 let l:module_name = a:1 else let l:module_name = substitute(expand("<cWORD>"), ':$', '', '') endif let l:doc_command = "ansible-doc -t module -s -j ".l:module_name let l:jq_query = 'to_entries[] as $module | \ "\($module.value.doc.plugin_name):\n" + \ ($module.value.doc.options | \ to_entries | \ map( \ if .value.required then \ if .value.choices then \ " \(.key): \(.value.choices | join("|"))" \ else \ if .value.default == null then \ " \(.key): [\(.value.type)]" \ else \ " \(.key): \(.value.default)" \ end \ end \ else \ if .value.choices then \ " #\(.key): \(.value.choices | join("|"))" \ else \ if .value.default == null then \ " #\(.key): [\(.value.type)]" \ else \ " #\(.key): \(.value.default)" \ end \ end \ end \ ) | \ join("\n") \ )' let l:jq_command = "jq -r '".l:jq_query."'" let l:output = system(l:doc_command." | ".l:jq_command) let l:lines = split(l:output, "\n") let l:current_line = line('.') let l:current_col = col('.') call append(l:current_line, l:lines) normal dd0 endfunction " Snippet generation command auto-completion function function! AnsibleDocList(ArgLead, CmdLine, CursorPos) return system("ansible-doc -l | awk '{print $1}' | grep " . a:ArgLead) endfunction " Snippet generation command command! -complete=custom,AnsibleDocList -nargs=1 AnsibleModuleSnippet \ call AnsibleModuleSnippet(<q-args>) " }}} " Create a module snippet from the current word nnoremap ,m :call AnsibleModuleSnippet()<CR> " Create a module snippet from fuzzy search with autocompletion nnoremap ,M :AnsibleModuleSnippet<Space> " Use ansible-lint as compiler, for linting setlocal makeprg=eval\ 'ansible-lint\ -f\ pep8\ --nocolor\ --show-relpath\ --offline\ %\ 2>/dev/null' augroup AnsibleLint autocmd! autocmd BufReadPost,BufWinEnter,BufWritePost * QuickFix augroup END " quick Jinja2 templating inoremap <buffer> {{ {{ }}<left><left><left> inoremap <buffer> "{ "{{ }}"<left><left><left><left> inoremap <buffer> {% {% %}<left><left><left>

The QuickFix function is defined in my main vimrc to call the makeprg and create a cwindow at the bottom of the Vim window, with the list of required fixes:

highlight QuickFixLine ctermbg=237 augroup QuickFix autocmd! autocmd FileType qf setlocal norelativenumber colorcolumn= nowrap augroup END command! QuickFix silent! make % | redraw! | botright cwindow