Ansible playbook - pass variables to role with template

49 views
Skip to first unread message

knowledge...@gmail.com

unread,
Nov 14, 2017, 11:26:28 AM11/14/17
to Ansible Project
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
Message has been deleted

Soniya panwar

unread,
Nov 21, 2017, 3:58:18 AM11/21/17
to Ansible Project
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
Reply all
Reply to author
Forward
0 new messages