Maybe use a vars file with hash keyed to whatever your situation requires?
EG vars file:
---
template_var:
site1:
name: site1
domain: example.com
site2:
name: site2
domain: anotherexample.com
and in template:
<VirtualHost *:80>
DocumentRoot /var/www/{{ template_vars[item][name] }}
ServerName {{ template_vars[item][name] }}{{
template_vars[item][domain]}}
</VirtualHost>
On Monday, September 1, 2014 10:27:14 PM UTC-7, anatoly techtonik wrote:
>
> Hi,
>
> I need to set values for variable per task. Is it possible at all in
> Ansible?
> I have this template:
>
> <VirtualHost *:80>
> DocumentRoot /var/www/{{name}}
> ServerName {{name}}{{domain}}
> </VirtualHost>
>
> And I want to call it with specific name and domain parameter without
> modifying the template.
> I tried this:
>
> - template: dest=/etc/apache2/sites-available/{{ item }}
> src=templates/virtualhost.j2
> with_items:
> - sitename
>
> But this syntax is not good, because it requires to rewrite template to
> use {{ item }}.
> Is it possible to avoid that?
>
> I tried moving the task into separate playbook and invoke it as
> parametrized include.
>
> - include: roles/web/tasks/virtualhost-add.yml name=sitename domain=
> example.com
>
> This works until I add a loop with_items.
>
> - include: roles/web/tasks/virtualhost-add.yml name={{item}} domain=
> example.com
> with_items:
> - sitename
>
> Now Ansible fails with:
>
> ERROR: [DEPRECATED]: include + with_items is a removed deprecated feature.
> Please update your playbooks.
>
> So, is that at all possible and what is the current best practice for
> templating files in a loop? I thought that the most simple way is to add
> some variables_override parameter to template module.
>
--
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 [email protected].
To post to this group, send email to [email protected].
To view this discussion on the web visit
https://groups.google.com/d/msgid/ansible-project/6093212f-9cd1-4124-968a-88f7dcaa3146%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.