Some Ansible Hints2014-07-12
Three useful little tidbits for using Ansible that I’ve come across.
1. How to use
with_items on lists of multiple types
With a little jinja-fu you can iterate a list of strings and dicts to template the action in more elaborate ways than the typical use.
I was configuring the installation of some Python packages and wanted to fix
the version of Django, but not the version of
MySQL-python, since any updates
to it are likely to be for security/compatability. Typically,
would be used to iterate over a list with dicts, like
version: something}, but it’s tiresome to write when you often don’t care
about one of the keys. Also, providing
version="" won’t work with the
module, since it would get converted to
pip install package==, which pip
doesn’t interpet as ‘latest’, so there would already have been a special case.
However with this little extra templating, I turned it into a nice readable
I found this a good reminder that Ansible uses Jinja2 everywhere you could want it to, and that Jinja2 can do a lot more than simple variable substitution.
EDIT This has been deemed bad practice and now fails on the latest Ansible with this error:
Guess it’s just too clever!
2. Increase forks in .ansible.cfg
Although the default configuration is sensible, the ‘forks’ parameter may be
worth tweaking upwards - as the docs state, 5 is “very very conservative:”. I
found that increasing it did sped things up drastically when I had to manage
more than a few hosts. My current
(I manage my dotfiles using Ansible, but that’s another post)
3. Use a different cow
Ansible will automatically use cowsay to brighten up the log output whilst
running if it is installed (on Mac,
brew install cowsay). But you can
also change the cowfile it uses to further entertain yourself.
You can get the list of cowfiles installed with
And you can choose which one will be used by setting the environment variable in your shell startup file (.bash_profile, .zshrc, etc.):
I’ve opted for the subtly mutated three-eyed cow, which looks like this:
Some are incredibly impractical, as they take up a lot of screen estate, for
I was also amused to see
cowsay comes with Ren and Stimpy cowfiles, which
gives you an idea of its age.
Those are all the hints I have at the moment—tune in next time!
© 2019 All rights reserved.