Ansible-lint for playbooks

During the #vBrownBag DevOps series after-show from my Using Ansible to provision VM’s in vCenter, Mike Marseglia asked about options for linting Ansible playbooks. Since I didn’t know, I thought it would be worthwhile to look into it. There is an Ansible-Lint repo on GitHub, reading through the information, it seemed straight forward. Here I am going to have a look at installing and using it against some example playbooks.

Installation should be easy, assuming you’ve got the correct packages installed, see my previous Ansible posts - if you got through that install, you should be able to install this with a single line:

pip install ansible-lint

Once installed you should now be able to do something like this:

ansible-lint clone-vm.yml

The clone-vm.yml is from my #vBrownBag series. As you can see in this screenshot, it suggests I have some trailing whitespace

Once I tiddy up the extra whitespace in the playbook, no suggestions are returned.

That is a pretty basic example, let’s say I’ve missed something such as a { when using vars_prompt, here you can see I have a missing backet for vm

Once again, now that it is fixed, no suggestions are returned. One thing that at least this specific tool does not help with is spacing errors, so your playbook will need to be valid, running ansible-lint here for example where my spacing is incorrect results in an general Ansible error, though it does point out where the error likely is:

Going forward I’ll certainly be looking into using this when writing a playbook to ensure general recommended practices are adhereed to. I’m still on the lookout for a tool that can help with spacing though!