Writing ansible playbooks with vim (and some plugins)

Does anyone use vim to write ansible playbooks/roles or so?
What Plugins do you use? Do you have autocompletion or an sidebar with documentation?

When I use VIM, I turn off all auto completion. I don’t care much anymore for solving issues I have with editors (even if user error haha). So, I go for a more complex setup that gives me the editor I prefer.

I’m not knocking VIM, I’ve developed entire DJango websites and modules in VIM. I just prefer GUI editors with tabs, search, file explorers, plugins, etc.

My preference is to use WSLv2 on Windows, with VSCode as my editor.

This allows me to have my Windows base image that the company requires, a dev environment on the Ubuntu install for WSLv2, and a decent GUI for Ansible (VSCode has YAML Linter).

A big bonus to this setup is that VSCode has a plugin for connecting to WSL environments. So, I get the benefits I list above with the speed of a local environment.

I store my work environments in GIT, I my work locally in WSL. When I’m ready to move my tested code to production, I push my changes to main GIT repo and pull the changes on my production server.

I use vim, I think the relevant lines for Ansible in ~/.vimrc are:

" Highlight trailing whitespace
" https://stackoverflow.com/a/48940543
autocmd BufWinEnter <buffer> match Error /\s\+$/
autocmd InsertEnter <buffer> match Error /\s\+\%#\@<!$/
autocmd InsertLeave <buffer> match Error /\s\+$/
autocmd BufWinLeave <buffer> call clearmatches()

" https://github.com/pearofducks/ansible-vim
au BufRead,BufNewFile *.yml set filetype=yaml.ansible
au BufRead,BufNewFile *.yaml set filetype=yaml.ansible

" indentLine
" https://www.arthurkoziel.com/setting-up-vim-for-yaml/
" https://github.com/Yggdroot/indentLine
let g:indentLine_char = '¦'

nmap <F11> :IndentLinesToggle<CR>
nmap <F12> :IndentGuidesToggle<CR>

" https://vi.stackexchange.com/a/24396
let g:vim_json_syntax_conceal = 0
let g:vim_markdown_conceal = 0
let g:vim_markdown_conceal_code_blocks = 0

" https://github.com/preservim/vim-indent-guides
let g:indent_guides_enable_on_vim_startup = 1
let g:indent_guides_enable_on_vim_startup = 0
let g:indent_guides_auto_colors = 1

Plugin URLs in the comments above.

For complicated templates it is often handy to switch the syntax highlighting to jinga2 rathert than the files format, eg:

:set syntax=jinja2

And then back again, for example:

:set syntax=apache
1 Like

I use vim to edit yml files and tmux to ansible-doc, playbooks execution, etc on another windows.

Here are some configs I have in .vimrc :slight_smile:

" ai : autoindent - copy indent from current line when starting a new line
" et : expandtab - turns TABs into spaces. Make TAB key a shortcut for spaces
" nu : number - show line numbers
" cuc: cursorcolumn - Highlight the screen column of the cursor
" cul: cursorline - draw an horizontal underline to highlight current line
" ts : tabstop - number of visual spaces a tab counts for when vim is displaying a TAB character.
" sts: softtabstop - number of spaces that are inserted when you use hit TAB key or delete TAB character with backspace
" sw : shiftwidth - number of spaces to use for each step of (auto)indent.
" fdm: foldmethod -  The kind of folding used. "indent" lines with equal indent form a fold.
autocmd FileType yaml setlocal ai et nu cuc cul ts=2 sts=2 sw=2 fdm=indent
set foldlevelstart=3

" Display TAB charecters as Erros
match Error /\t/

" https://github.com/Yggdroot/indentLine
let g:indentLine_char_list = ['┊','┆','¦']
3 Likes