# Using complex data structures in templates

**URL:** <https://forum.ansible.com/t/using-complex-data-structures-in-templates/23259>\
**Category:** Ansible Project\
**Created:** [December 14, 2016, 4:23pm UTC](https://forum.ansible.com/t/using-complex-data-structures-in-templates/23259 "2016-12-14T16:23:53Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Dick\_Visser](https://avatars.discourse-cdn.com/v4/letter/d/d9b06d/32.png) [@Dick\_Visser](https://forum.ansible.com/u/Dick_Visser)\
**Post date:** [December 14, 2016, 4:23pm UTC](https://forum.ansible.com/t/using-complex-data-structures-in-templates/23259/1 "2016-12-14T16:23:53Z")

</div>

Hi

Some of my playbooks involve configuring files in languages such as PHP.  
The configuration is basically a large PHP array, for example this SimpleSAMLphp config file:

\<?php $config = array ( 'language.i18n.backend' =\> 'gettext/gettext', 'template.debug' =\> true, 'baseurlpath' =\> 'wayf/', 'certdir' =\> '/etc/simplesamlphp/wayf/cert/', 'logging.handler' =\> 'file', 'loggingdir' =\> '/var/log/simplesamlphp/wayf/', 'datadir' =\> '/var/lib/simplesamlphp/wayf/data/', 'tempdir' =\> '/tmp/simplesaml-wayf', 'attributenamemapdir' =\> '/etc/simplesamlphp/wayf/attributemap/', 'secretsalt' =\> 'secret', 'auth.adminpassword' =\> 'secret', 'logging.processname' =\> 'simplesamlphp.wayf', 'language.available' =\> array('en', 'no', 'nn', 'se', 'da', 'de', 'es', 'sv', 'fi', 'es', 'fr', 'it', 'nl', 'lb', 'cs', 'sl', 'hr', 'hu', 'pl', 'pt', 'pt-BR', 'tr', 'ja', 'zh-tw', 'el', 'lt', 'sam', 'ru'), 'session.cookie.secure' =\> TRUE, etc etc The way I now do this is a template with variables such as: simplesamlphp\_template\_debug simplesamlphp\_datadir simplesamlphp\_secretsalt There are many possible configuration options, so I'm only using a subset as ansible variables. This doesn't really scale well. Also, it's not just a simple array, but it can be nested as well (see example above). I'm looking for a way to configure things in Ansible using the native data structure (yaml), and then templating that out to another language. So if we consider the above example, you would have this var in a playbook: config: language.i18n.backend: gettext/gettext template.debug: true baseurlpath: wayf/ certdir: /etc/simplesamlphp/wayf/cert/ logging.handler: file loggingdir: /var/log/simplesamlphp/wayf/ datadir: /var/lib/simplesamlphp/wayf/data/ tempdir: /tmp/simplesaml-wayf attributenamemapdir: /etc/simplesamlphp/wayf/attributemap/ secretsalt: secret auth.adminpassword: secret language.available: - en - no - nn - se - da - es - sv - fi - fr - it logging.processname: simplesamlphp.wayf session.cookie.secure: TRUE I was thinking about some way to serialize the yaml data structure and unserialize it using PHP? But that requires PHP, which is not ideal. Any ideas on how I can do this? Many thanks

---

<div class="post-metadata">

**Author:** ![j.r.hawkesworth](https://avatars.discourse-cdn.com/v4/letter/j/a3d4f5/32.png) [@j.r.hawkesworth](https://forum.ansible.com/u/j.r.hawkesworth)\
**Post date:** [December 15, 2016, 6:41am UTC](https://forum.ansible.com/t/using-complex-data-structures-in-templates/23259/2 "2016-12-15T06:41:42Z")

</div>

Make use of the template module - templates can do much more than just substitute variable values into text files.

I suggest reading through [http://jinja.pocoo.org/docs/dev/templates/](http://jinja.pocoo.org/docs/dev/templates/) to get an idea of what is possible. Although the examples are most generating html, it can generate any text file format.

You can loop through your yaml array to set the languages using a for loop - something like this (not tested)

`'language.available' => array({% for item in language.available %}'{{ item }}'{% if not loop.last %}, {% endif %}{% endfor %}),`

Hope this helps,

Jon
