Setup Your vim editor for Ansible Playbook
You may ignore this article if you are already an expert in vim as this is a quick note for those who are new to vim editor. As we know, Ansible is used by engineers from all platforms including Linux/Unix, Windows, Networking, DBA side, Developers, Middleware etc. And we know, Ansible controlnode is available only for unix based platforms so far. So handling playbooks is a headache as even a single white space in your playbook can spoil your entire day.
You have so many ways to edit playbook – you can use any visual editor like atom, VSCode, Sublime etc or can sync with git or directly use GUI in linux environment. And most of them have plugins for ansible as you can easily highlight syntax and understand the indentation.
But as a linux user, my favorite editor is vim as I can make small changes and test playbook instantly from CLI itself. See below, how did I tune my vim variables to get an easy to understand look when I edit ansible playbooks.
Learn about vim parameters and variables for easy editing of the yaml or any file format where indentation is very important. See some of the examples shown below.
set et ts=2 ai sw=2 nu
set nu - show line number
et - expand tab
See some of the defenitions below.
ts : tabstop – eg: tabstop=2
et : expandtab
sw : shiftwidth – eg: shiftwidth=4
sts : softtabstop – eg: softtabstop=4
nu – show line number
This is on by default in most of the cases but make sure.
Choose your colorscheme
You can see the installed colorschemes by typing :colorscheme and then a space and press tab in vim editor.
Show Line Numbers
You must enable the line number visibility
Make your cursor more visible
It would be great if you enable this as your current cursor position will be more visible.
Enable Indent Visibility
To set all items permanent
Edit your ~/.vimrc file and add below lines
autocmd FileType yaml setlocal et ts=2 ai sw=2 nu sts=0
Finally see the vim editor with autoindent, cursorline, indentlines, etc