Hi,
I would like to build up a variable from defaults, group_vars and host_vars - but hash_behaviour doesn’t seem to be working as I’d expect it to.
The plan is to have a default config (in this case haproxy) and then allow additional frontends to be defined at group and host_var level.
I’ve created a very simple example which hopefully explains the issue:
https://github.com/tdac/Ansible-HashMerge-Issue
I’m wondering if the “-” notation in the inventory stops Ansible seeing this as mergable?
Thanks in advance for any pointers you can give me!
Tim
What you have is a list not a hash aka dictionary.
The documentation says it only works on hash/dictionary so I guess thats why.
Thanks Kai,
That would explain it - is there a ‘standard’ way of merging a list in this context?
Tim
Standard in what situation?
List is usually used in a loop, and the with_items can have several entries so you'll have a merge there.
with_items:
- haproxy_frontend_host | default()
- haproxy_frontend_group | default()
Hi Kai,
Thanks - so if I’ve understood correctly - I could set:
haproxy_frontend_group:
in group_vars
and
haproxy_frontend_host:
and then use your ‘with items’ suggestion to combine them…I suppose my dream was to have list variables that work like hashes do. For example If one defines a default configuration for a group of servers in group_vars - and then want to add ‘special cases’ at host_vars level - that’s currently not possible with lists (which is a shame - as lists offer a richer way to define services I think?).
Is there a theoretical reason why lists can’t be merged the same way as hashes?
Tim
Yes, but I forgot the curly brackets, it should be.
with_items:
- '{{ haproxy_frontend_host | default() }}'
- '{{ haproxy_frontend_group | default() }}'