I need to model some data on a per-inventory basis, i.e. each inventory would define the same variables with different values. This seems simple enough when using simple variables i.e. no lists or hashes e.g.
However, I want to use this in conjunction with Ansible’s environment feature and ideally want to be able to define a hash in each inventory file which I can then reference later on. As far as I can tell there is no longer any way to define a hash in an inventory file now that yaml inventory files have been deprecated. Is my understanding correct here?
I’m probably missing something but I can’t see an idiomatic Ansible way of doing this. Hence grateful for any tips.
Sorry folks, possibly bad form to self-reply purely in order to bump. Am a bit stuck on this though so some assistance would be invaluable.
Thinking about this problem has made me wonder if it would be possible/ useful for Ansible to natively support some sort of per-inventory yaml variables file? Perhaps something called ‘inventory_vars’ which worked in a similar way as ‘group_vars’ but for inventories?
If I do this then won’t I have to duplicate my group_vars files across each inventory folder? Is that considered Idiomatic usage? I’m asking as I’ve tried to follow the best practices page for layout and I haven’t noticed a layout that resembles your suggestion.
Gah, it’s clearly staring me in the face but I’m just not getting it (sorry!).
So using your example Michael, let’s say I want to define a hash called ‘proxy_details’ which, in each inventory, will hold different values for the same hash keys:
For my stage and production inventories it will look like this:
I wasn’t saying you had to, just showing that you can flexibly organize them in differnet ways, specially if you wanted different versions of the same group file (all) for each inventory. For your last question:
/some/dir/inventory
production
stage
development
group_vars/
all => proxy_details: [http_proxy_host: abc.def.com, http_proxy_port: 80]
webservers
development => proxy_details: [http_proxy_host: zwx.dev.com, http_proxy_port: 80]
or optionally, instead of in all
staging => proxy_details: [http_proxy_host: abc.def.com, http_proxy_port: 80]
production => proxy_details: [http_proxy_host: abc.def.com, http_proxy_port: 80]
Oh yeah, good point! I spend most of my time programming in Groovy where a Map (which is about as close to a hash as you can get) is defined with square brackets, as a result I sometimes find the Groovy → YAML context switch ends up in me writing nonsense YAML!