generating multiple files from one template

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

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.

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.