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.
`
`
This works until I add a loop with_items.
`
with_items:
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.
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
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
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?
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!
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.
Maybe use a vars file with hash keyed to whatever your situation requires? EG vars file:
`
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.
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?
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:
domain: www.fomain.tld
domain: foo.bar
in template:
<VirtualHost *:80>
DocumentRoot /var/www/{{ item.name }}
ServerName {{ item.name }}/{{ item.domain }}
?
Hello,
This implies that all Jinja2 templates variables should have `item.` prefix,
which is ugly, because limits their usage to loop-only tasks.