Same list from multiple vars-files consolidated

81 views
Skip to first unread message

Sebastian Gumprich

unread,
Jan 7, 2015, 8:59:29 AM1/7/15
to ansible...@googlegroups.com
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

Brian Coca

unread,
Jan 7, 2015, 9:11:30 AM1/7/15
to ansible...@googlegroups.com
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.



--
Brian Coca

Sebastian Gumprich

unread,
Jan 8, 2015, 3:18:41 AM1/8/15
to ansible...@googlegroups.com
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!

Tomasz Kontusz

unread,
Jan 8, 2015, 3:53:43 AM1/8/15
to ansible...@googlegroups.com


Sebastian Gumprich <zufal...@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.


>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!
>
>
>Am Mittwoch, 7. Januar 2015 15:11:30 UTC+1 schrieb Brian Coca:
>>
>> 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.
>>
>>
>>
>> --
>> Brian Coca
>>

--
Wysłane za pomocą K-9 Mail.
Reply all
Reply to author
Forward
0 new messages