Ansible playbook - pass variables to role with template

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

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