Hello,
I have a list-item in the group-vars for “all” hosts:
`
“/etc/ansible/group_vars/all”
splunkforwarder_log_items:
- app_name: var_messages
source: “/var/log/messages”
`
I also have the same list-item in another (narrowed down) group-var:
`
“/etc/ansible/group_vars/group_1”
splunkforwarder_log_items:
- app_name: var_secure
source: “/var/log/secure”
index: “testindex”
sourcetype: linux_secure
`
Now I want to use both lists (or the conflated list) in the following task, so {{ item.app_name }}
would be substituted with var_messages
and ``var_secure from both lists.
`
- name: create input configuration
template:
src=inputs_conf.j2
dest=/etc/apps/{{ item.app_name }}/local/inputs.conf
with_items: splunkforwarder_log_items
`
Sadly, only the list from “group_1” is used.
Is there a way to conflate these lists / use both? Or am I doing something wrong?
Regards
Sebastian
ansible by default will override previous definitions with newer ones,
in this case group 'all' gets loaded first and then overwritten by
group1.
You can change this behavior globally using the hash_behaviour entry
in ansible.cfg.
Or you can name the variable differently and then use the |union
filter in the task to aggregate them just for that task.
Thanks for your input.
I tried setting hash_behaviour to “merge” but this doesn’t work. Even when I put the two lists into one vars-file, only the last list will be used.
But you mentioning naming the variable different gave me an idea. I’m using different variables in group_vars and host_vars now and use multiple tasks and templates.
Thanks!
Sebastian Gumprich <zufallsheld@gmail.com> napisał:
Thanks for your input.
I tried setting hash_behaviour to "merge" but this doesn't work.
hash_behaviour only affects dictionaries
Even when
I put the two lists into one vars-file, only the last list will be
used.
Repeated keys in a dictionary is implementation-defined in YAML, and PyYaml (the library Ansible uses) just ignores all but the last repeated key.