Set vars for template from task

203 views
Skip to first unread message

anatoly techtonik

unread,
Sep 2, 2014, 1:27:14 AM9/2/14
to ansible...@googlegroups.com
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.

Akos Vandra

unread,
Sep 2, 2014, 8:41:43 AM9/2/14
to ansible-project
This is related to my last email (subject: support passing new
variables directly to the template module, but not only), and issues
#8733 and #4546
> --
> 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/dcb5622e-dfd2-4d8d-888c-7dc4b673ed6e%40googlegroups.com.
> For more options, visit https://groups.google.com/d/optout.

Michael DeHaan

unread,
Sep 2, 2014, 6:47:25 PM9/2/14
to ansible...@googlegroups.com
These are separate questions really.

In your case, you don't want to template the file with_items because that will result in the file being written more than once.

You probably want to reference a variable *inside* that template instead.

You can't do include+with_items, as that's not a thing :)





anatoly techtonik

unread,
Sep 3, 2014, 1:43:06 AM9/3/14
to ansible...@googlegroups.com
On Wed, Sep 3, 2014 at 1:47 AM, Michael DeHaan <mic...@ansible.com> wrote:
> These are separate questions really.
>
> In your case, you don't want to template the file with_items because that
> will result in the file being written more than once.

I don't get it. There is a variable in template dest attribute, which changes
on every iteration. Why file will be written more than once?

> You probably want to reference a variable *inside* that template instead.

Embedding all needed variables inside the template looks wrong, or
I don't get something again.

> You can't do include+with_items, as that's not a thing :)

Although it answers the question, it still doesn't resolve the problem. )
Why it can not be implemented?
> You received this message because you are subscribed to a topic in the
> Google Groups "Ansible Project" group.
> To unsubscribe from this topic, visit
> https://groups.google.com/d/topic/ansible-project/SulFWHwt_OI/unsubscribe.
> To unsubscribe from this group and all its topics, 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/CA%2BnsWgyyQCmxyXBL-ifscAshtR-%2Bh%3DQ5OVDK%3DE%2BGHCHQuRBc4Q%40mail.gmail.com.
>
> For more options, visit https://groups.google.com/d/optout.



--
anatoly t.

Michael DeHaan

unread,
Sep 3, 2014, 7:38:37 AM9/3/14
to ansible...@googlegroups.com
Sorry, I missed the variable in the "dest" on first read, as you had used "dest" before "src", and I'm used to reading "src" first all the time (since I think of it as a remote copy).

I'm not going to answer the include+with_items question here because it's been answered a couple hundred times already, but there are lots of threads on this subject about why it is technically not possible.

Thanks!




anatoly techtonik

unread,
Sep 3, 2014, 8:10:25 AM9/3/14
to ansible...@googlegroups.com
On Wed, Sep 3, 2014 at 2:38 PM, Michael DeHaan <mic...@ansible.com> wrote:
> Sorry, I missed the variable in the "dest" on first read, as you had used
> "dest" before "src", and I'm used to reading "src" first all the time (since
> I think of it as a remote copy).
>
> I'm not going to answer the include+with_items question here because it's
> been answered a couple hundred times already, but there are lots of threads
> on this subject about why it is technically not possible.

The original question is not about include. Include could be a possible hack.
It could help if template allowed to set key=value pairs for variables
in a loop.

On a side note.
If there new threads about the include appear constantly, maybe its worth to
write a blog post to reference people to some specific place with clear details?
Without knowing the reasons it is unlikely that I can propose anything.
> https://groups.google.com/d/msgid/ansible-project/CA%2BnsWgyJc0fXHvP05XpUpLVdN0GdzCWO_vKD7rMfcM8zgtdtSQ%40mail.gmail.com.

Dylan Martin

unread,
Sep 3, 2014, 2:34:16 PM9/3/14
to ansible...@googlegroups.com
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>

anatoly techtonik

unread,
Sep 3, 2014, 2:43:09 PM9/3/14
to ansible...@googlegroups.com
On Wed, Sep 3, 2014 at 9:34 PM, Dylan Martin <dy...@sweetlabs.com> wrote:
> 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>

Using {{ item[..] }} arrays in template arrays is ugly. Template usage
should not depend on if it is called in a loop or as a single run.

Michael DeHaan

unread,
Sep 4, 2014, 9:20:53 PM9/4/14
to ansible...@googlegroups.com
Calling a template in a loop will result in the template being called multiple times, and replacing the file on top of itself each time.

You don't want to do that unless the filename changes in each iteration of the loop.





--
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.

anatoly techtonik

unread,
Sep 5, 2014, 2:21:16 AM9/5/14
to ansible...@googlegroups.com
On Fri, Sep 5, 2014 at 4:20 AM, Michael DeHaan <mic...@ansible.com> wrote:
> Calling a template in a loop will result in the template being called
> multiple times, and replacing the file on top of itself each time.
>
> You don't want to do that unless the filename changes in each iteration of
> the loop.

I want exactly that - change filename and variables for that filename in each
iteration of the loop. How to do this?

David Karban

unread,
Sep 5, 2014, 4:39:01 AM9/5/14
to ansible...@googlegroups.com
Hi,

not sure I understand your problem correctly, but isn`t this enough?

- template: dest=/etc/apache2/sites-available/{{ item.domain }}
            src
=templates/virtualhost.j2
  with_items
:
   
- sitename


I presume, sitename is list of names and attributes like:
sitename:
  - name: first_domain
    domain: www.fomain.tld
  - name: second_domain
    domain: foo.bar

in template:

<VirtualHost *:80>
    DocumentRoot /var/www/{{ item.name }}
    ServerName {{ item.name }}/{{ item.domain }}
</VirtualHost>

?


--
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.

For more options, visit https://groups.google.com/d/optout.



--
David Karban
Specialista na správu linuxových serverů
www.karban.eu

anatoly techtonik

unread,
Sep 6, 2014, 2:17:41 AM9/6/14
to ansible...@googlegroups.com
Hello,

This implies that all Jinja2 templates variables should have `item.` prefix,
which is ugly, because limits their usage to loop-only tasks.
> You received this message because you are subscribed to a topic in the
> Google Groups "Ansible Project" group.
> To unsubscribe from this topic, visit
> https://groups.google.com/d/topic/ansible-project/SulFWHwt_OI/unsubscribe.
> To unsubscribe from this group and all its topics, 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/CAOBFM95PSpWtMrphCEbcJFhuUwaf4yeKkbQr8eNFFwY%2B-ipDVg%40mail.gmail.com.
>
> For more options, visit https://groups.google.com/d/optout.



--
anatoly t.
Reply all
Reply to author
Forward
0 new messages