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 ...
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
loop: almost interchangeably: the latter is the newer form, and it appears
with_items: will eventually disappear.
Types of Variables
- Lists (this is straightforward and works in both "languages")
- Dictionaries According to Ansible - Ansible's own documentation provides a non-functioning (at least in Jinja2) form of dictionary
- Dictionaries According to Giles - I show a fairly simple dictionary style that works in both languages
- User Dictionaries With a Group List Added - a more complex example showing how to extend this variable structure
- 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.