generating multiple files from one template

2,079 views
Skip to first unread message

Andy Altepeter

unread,
Feb 26, 2014, 10:08:13 AM2/26/14
to ansible...@googlegroups.com
Greetings,

I have a need to generate multiple configuration files on a single host from one template.  Is this possible in ansible? 

Let's say I have a variable like:

vars:
  configs:
    - port: 8080
      directory: /var/www/host1
    - port: 8081
      directory: /var/www/host2

I need to generate a configuration file for each tuple in this list.  The template needs to have access to each tuple in the list using a single name.  In a python program it might look like:

for config in configs:
   generate_template(config=config)

And the template then uses the variable name "config" to refer to the current tuple.

Is this possible with just ansible?  I know I can use with_items to iterate over the list, but does the template have access to the "current" tuple?

Thanks,
Andy





Michael DeHaan

unread,
Feb 26, 2014, 10:25:31 AM2/26/14
to ansible...@googlegroups.com
You can easily put variables in your template that the template can key off of.

Usage of things like "set_fact" or parameterized role arguments may be relevant here.




--
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/2c4ac5a8-fca2-4b79-9890-5257ff18125d%40googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.

Brian Coca

unread,
Feb 26, 2014, 11:16:31 AM2/26/14
to ansible...@googlegroups.com
very easy:

- template: src=sametemplate dest=/var/www/{{item}}
  with_items: 
    - host1
    - host2
and you can use 'item' as a key inside the template to write specific options to each.
Reply all
Reply to author
Forward
0 new messages