# Ansible playbook - pass variables to role with template

**URL:** <https://forum.ansible.com/t/ansible-playbook-pass-variables-to-role-with-template/26021>\
**Category:** Ansible Project\
**Created:** [November 14, 2017, 4:26pm UTC](https://forum.ansible.com/t/ansible-playbook-pass-variables-to-role-with-template/26021 "2017-11-14T16:26:28Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![knowledgefromgroups](https://avatars.discourse-cdn.com/v4/letter/k/f475e1/32.png) [@knowledgefromgroups](https://forum.ansible.com/u/knowledgefromgroups)\
**Post date:** [November 14, 2017, 4:26pm UTC](https://forum.ansible.com/t/ansible-playbook-pass-variables-to-role-with-template/26021/1 "2017-11-14T16:26:28Z")

</div>

Hi all,

I’m wonder if it’s possible to pass variables from playbook to role which has template.  
Example:

playbook.yml

- roles:
- { role: monit, monit\_proc: “sshd”,monit\_ssh\_port: “25”}

monit role:  
monit/templates/sshd.conf.j2 :  
check process sshd with pidfile {{ monit\_ssh\_pid }}  
group system  
start program “/etc/init.d/ssh start”  
stop program “/etc/init.d/ssh stop”  
if failed port {{ monit\_ssh\_port }} protocol ssh then restart  
if {{ monit\_restarts }} restarts within {{ monit\_cycles}} cycles then timeout

monit/tasks/main.yml :

- name: Add {{ monit\_proc }}  
template: src=/etc/ansible/configs/monit/monit.d/{{monit\_proc}}.j2.conf  
dest=/etc/monit/conf.d/{{monit\_proc}}.conf  
tags:
- monit\_cfg

Or maybe it’s wrong approach ?

Best regards,

Morbious

---

<div class="post-metadata">

**Author:** ![Soniya\_panwar](https://avatars.discourse-cdn.com/v4/letter/s/22d042/32.png) [@Soniya\_panwar](https://forum.ansible.com/u/Soniya_panwar)\
**Post date:** [November 21, 2017, 8:58am UTC](https://forum.ansible.com/t/ansible-playbook-pass-variables-to-role-with-template/26021/2 "2017-11-21T08:58:17Z")

</div>

Hello,  
Your requirement is not clear enough. can you please elaborate more on what are you trying to achieve,

> > if it’s possible to pass variables from playbook to role which has template.  
> > if you wants to decide the template to use at run time, then you need not provide the complete path. Instead you may place all the templates in a folder inside roles. The template file can then be called in main.yml using a variable as shown below:

playbook.yml

- roles:
- { role: monit, monit\_proc: “sshd”,monit\_ssh\_port: “25”}

monit/tasks/main.yml :

- name: Add {{ monit\_proc }}  
template:  
src: “{{monit\_proc}}.cfg.j2”  
dest: /etc/monit/conf.d/{{monit\_proc}}.conf

Thanks  
Soniya
