Using complex data structures in templates

64 views
Skip to first unread message

Dick Visser

unread,
Dec 14, 2016, 11:24:56 AM12/14/16
to ansible...@googlegroups.com
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







--
Dick Visser
Sr. System & Network Engineer
GÉANT

Want to join us? We're hiring: http://www.geant.org/jobs

J Hawkesworth

unread,
Dec 15, 2016, 1:41:42 AM12/15/16
to Ansible Project
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/ 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
Reply all
Reply to author
Forward
0 new messages