How to call templates as functions / how to use roles as functions

I am generating some supervisor files from templates - but my services (roles) have dependencies on other services. So because the template uses the variable name instead of it picking “theServiceThatServiceADependsOn” and “ServiceA” as the names it picks “ServiceA” and “ServiceA” both times. I am guessing this is to do with variable scoping - that the var name in the dependent service is getting overridden?

What’s the easiest way around this? Is there some way to pass in variables when I call my template, just like a function call in a conventional language? I wondered whether I can just have a vars block in my action, but I checked out a load of Ansible scripts from github, so I can search them to find solutions to my problems, but I can’t see anyone else doing that?

Also I need to trigger supervisorctl in several different roles - we decided restarting supervisor was a bad idea because it will restart existing running services so I am doing this:

  • name: myservice | Get supervisor to re-read configs
    action: command supervisorctl reread

  • name: myservice | Trigger a supervisor update
    action: command supervisorctl update

  • name: myservice | Get supervisor to restart service
    action: command supervisorctl restart {{ name }}

Is there any way I can specify this in one place? A role ought to be able to do it but I need to pass an argument in (name) - I am thinking of this as a function?

thanks!

M

I think what you are looking for are parameterized roles.

http://docs.ansible.com/playbooks_roles.html#roles, look for “parameterized roles”.

  • James