use subelement-variables in templates

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

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.

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!