# use subelement-variables in templates

**URL:** <https://forum.ansible.com/t/use-subelement-variables-in-templates/15920>\
**Category:** Ansible Project\
**Created:** [August 12, 2014, 12:10pm UTC](https://forum.ansible.com/t/use-subelement-variables-in-templates/15920 "2014-08-12T12:10:06Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Sebastian\_Gumprich](https://avatars.discourse-cdn.com/v4/letter/s/a6a055/32.png) [@Sebastian\_Gumprich](https://forum.ansible.com/u/Sebastian_Gumprich)\
**Post date:** [August 12, 2014, 12:10pm UTC](https://forum.ansible.com/t/use-subelement-variables-in-templates/15920/1 "2014-08-12T12:10:06Z")

</div>

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`

---

<div class="post-metadata">

**Author:** ![Michael\_DeHaan1](https://avatars.discourse-cdn.com/v4/letter/m/5e9695/32.png) [@Michael\_DeHaan1](https://forum.ansible.com/u/Michael_DeHaan1)\
**Post date:** [August 12, 2014, 2:14pm UTC](https://forum.ansible.com/t/use-subelement-variables-in-templates/15920/2 "2014-08-12T14:14:13Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![Sebastian\_Gumprich](https://avatars.discourse-cdn.com/v4/letter/s/a6a055/32.png) [@Sebastian\_Gumprich](https://forum.ansible.com/u/Sebastian_Gumprich)\
**Post date:** [August 14, 2014, 6:58am UTC](https://forum.ansible.com/t/use-subelement-variables-in-templates/15920/3 "2014-08-14T06:58:58Z")

</div>

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!
