# Pass parameter/argument to (generic) template

**URL:** <https://forum.ansible.com/t/pass-parameter-argument-to-generic-template/14013>\
**Category:** Ansible Project\
**Created:** [November 27, 2013, 11:58am UTC](https://forum.ansible.com/t/pass-parameter-argument-to-generic-template/14013 "2013-11-27T11:58:36Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Yoann\_DAVID](https://avatars.discourse-cdn.com/v4/letter/y/85e7bf/32.png) [@Yoann\_DAVID](https://forum.ansible.com/u/Yoann_DAVID)\
**Post date:** [November 27, 2013, 11:58am UTC](https://forum.ansible.com/t/pass-parameter-argument-to-generic-template/14013/1 "2013-11-27T11:58:36Z")

</div>

Hello,

I want to create a generic template which can be reused for several task, but this also need to have a parametrer/variable but how to pass this variable (task specific) to my template, an example (real one) :

My template :

> \<VirtualHost \*:80\>  
> ServerName {{ site.server\_name }}  
> \<Directory “/var/www/{{ site.site\_name }}/”\>  
> Options FollowSymLinks  
>   
> DocumentRoot /var/www/{{ site.site\_name }}  
> ErrorLog ${APACHE\_LOG\_DIR}/error\_{{ site.site\_name }}.log  
> LogLevel warn  
> CustomLog ${APACHE\_LOG\_DIR}/access\_{{ site.site\_name }}.log combined env=!Local

My variable is here site, and can be defined like this :

> adminer:  
> site\_name: adminer  
> server\_name: [dbadmin.domain.fr](http://dbadmin.domain.fr)

Or like this :

> phpinfo:  
> site\_name: phpinfo  
> server\_name: [phpinfo.domain.fr](http://phpinfo.domain.fr)

I want to pass those 2 variables to the template but in 2 differents tasks, I imagine something like this :

> - name: Paramétrage du virtualhost  
> template: src=vhost\_adminer.j2  
> dest=/etc/apache2/sites-available/{{ adminer.site\_name }}  
> owner=root  
> group=root  
> mode=0644  
> site=adminer

For the 2 tasks.

But it doesn’t work.

> failed: [127.0.0.1] =\> {“failed”: true, “item”: “”}  
> msg: this module requires key=value arguments

I know I can use the with\_item and pass a list to my template but I need to use the template in 2 differents tasks with 2 differents variable, so no with\_items.

Any idea ?

Thanks a lot

---

<div class="post-metadata">

**Author:** ![Michael\_DeHaan2](https://avatars.discourse-cdn.com/v4/letter/m/da6949/32.png) [@Michael\_DeHaan2](https://forum.ansible.com/u/Michael_DeHaan2)\
**Post date:** [November 27, 2013, 1:04pm UTC](https://forum.ansible.com/t/pass-parameter-argument-to-generic-template/14013/2 "2013-11-27T13:04:44Z")

</div>

You should use parameterized roles in this case.

[http://ansibleworks.com/docs/playbooks\_roles.html#id5](http://ansibleworks.com/docs/playbooks_roles.html#id5)

---

<div class="post-metadata">

**Author:** ![Yoann\_DAVID](https://avatars.discourse-cdn.com/v4/letter/y/85e7bf/32.png) [@Yoann\_DAVID](https://forum.ansible.com/u/Yoann_DAVID)\
**Post date:** [November 27, 2013, 6:02pm UTC](https://forum.ansible.com/t/pass-parameter-argument-to-generic-template/14013/3 "2013-11-27T18:02:12Z")

</div>

Thanks for the answer but is there a other way, because I prefer to let the 2 tasks in the same role.

---

<div class="post-metadata">

**Author:** ![danrough](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/danrough/32/3541_2.png) [@danrough](https://forum.ansible.com/u/danrough)\
**Post date:** [November 27, 2013, 10:56pm UTC](https://forum.ansible.com/t/pass-parameter-argument-to-generic-template/14013/4 "2013-11-27T22:56:25Z")

</div>

Hi there,

We were faced with what seems like a similar problem recently. I couldn’t find any information online about how to solve it, but was lucky enough to be given a pointer by somebody on the IRC channel. I thought that I would blog our solution so that it’s available for others faced with a similar task.

You can find that post here: [http://danrough.net/2013/11/27/iterating-an-ansible-task-using-a-dictionary-object-of-variables/](http://danrough.net/2013/11/27/iterating-an-ansible-task-using-a-dictionary-object-of-variables/)

If there are any elements of the blog that you think could be improved do let me know, I’d be keen to make the blog as clear as possible for all those that are looking for a solution in the future.

Dan.
