The Horrors of Ansible Complex Variables

By "complex variable" I mean anything that isn't a "name=value" simple variable. So lists, arrays, dictionaries, hashes, lists in dictionaries, lists in lists, dictionaries in lists ...

I discovered recently that my Ansible scripts include two different complex variable structures. As it turns out, one works with Jinja2 and not with Ansible, and the other works with Ansible and not with Jinja2. I now need to be able run Ansible commands using a complex variable and create a (Jinja2) template from the same variable. Each of the previous incompatible variables has been working well in context for a couple years. This has led to a long study of complex variables: I hope I can clarify this not-well-described area for some other poor sucker using Ansible. You'll see in "Dictionaries According to Ansible" (linked below) that Ansible's own documentation describes a form of dictionary that doesn't work with Jinja2 (works great with Ansible though ...).

It also begs the question "how the hell did the creators of Ansible allow a complex variable structuring so flexible it can achieve such spectacular dysfunction?" I can't answer that, but let's hope I can clarify what works for both ...

About Versions

I'll try to include the Ansible version in each page created. You should definitely check your Ansible version: Ansible is a volatile language. I'm sure the developers like to call it "under development," but "volatile" is a better word. Don't get me wrong: it's always improving, but it's a pain to constantly have to go back and change keywords or even structures in your older scripts because they've been deprecated ...

Along those lines, I apologize for using with_items: and loop: almost interchangeably: the latter is the newer form, and it appears with_items: will eventually disappear.

Types of Variables

Bibliography

  • http://jinja.pocoo.org/docs/dev/templates/ - this is Jinja's very dense documentation. Almost everything you need is there, although it's seriously packed in and lacking examples. It's well and clearly written ... did I mention it's lacking examples? It has examples. It needs more.