use subelement-variables in templates

718 views
Skip to first unread message

Sebastian Gumprich

unread,
Aug 12, 2014, 8:10:06 AM8/12/14
to ansible...@googlegroups.com
Hi,

I have the following variables:

splunkforwarder_log_items:
 
- source: "/var/log"
    app_name
: "var_log"
    recursive
: true
    index
: "testindex"
    splunkforwarder_log_files
:
     
- logfile: "messages"
        name
: "var_log"
        sourcetype
: linux_messages_syslog



and the following task:

- name: drop props configuration
 
template:
    src
=props_conf.j2
    dest
={{splunkforwarder_dir}}/etc/apps/{{item.0.app_name}}/props.conf
    owner
=splunk
   
group=splunk
    mode
=640
  with_subelements
:
   
- splunkforwarder_log_items
   
- splunkforwarder_log_files



Is it possible to use the variables from the subelement "splunkforwarder_log_files" in the "props_conf.j2"-template? For example like this:

{% for sources in splunkforwarder_log_items.splunkforwarder_log_files %}
[source::.../{{sources.logfile}}]
sourcetype
= {{sources.sourcetype}}
{% endfor %}



Or is there a simpler alternative?

Regards
Sebastian

Michael DeHaan

unread,
Aug 12, 2014, 10:14:18 AM8/12/14
to ansible...@googlegroups.com
With what you have above, item.0 is the overall list and item.1 is each item in the splunkforwarder_log_files, however it looks like you shouldn't be using "with_subelements" at all, because the only variable in your task definition that changes is item.0

Unless I'm missing something, I would switch to a basic "with_items" and avoid calling the template task multiple times, and then just rely on the loop inside the template.



--
You received this message because you are subscribed to the Google Groups "Ansible Project" group.
To unsubscribe from this group and stop receiving emails from it, send an email to ansible-proje...@googlegroups.com.
To post to this group, send email to ansible...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/ansible-project/6fa6b0f5-5bac-455c-bb8b-9cbd05ae5c76%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Sebastian Gumprich

unread,
Aug 14, 2014, 2:58:58 AM8/14/14
to ansible...@googlegroups.com
That did the trick!

Here's my configuration now, for completeness:

vars:


splunkforwarder_log_items
:
 
- app1_name: var_log
    source
: "/var/log"

    recursive
: true
    index
: "testindex"

splunkforwarder_log_files
:
 
- logfile: "messages"

    app_name
: "var_log"
    sourcetype
: linux_messages_syslog


task:


- name: drop props configuration
 
template:
    src
=props_conf.
j2
    dest
={{splunkforwarder_dir}}/etc/apps/{{ item.app_name }}/props.conf
    owner
=splunk
   
group=splunk
    mode
=640
  with_items
: splunkforwarder_log_files


template:

{% for source in splunkforwarder_log_files %}
[source::.../{{source.logfile}}]
sourcetype
= {{source.sourcetype}}
{% endfor %}



Thanks!
Reply all
Reply to author
Forward
0 new messages