user45097
user45097

Reputation: 621

Pass service/init.d name to Ansible handler as variable

New to Ansible and was considering the following creation of services on the fly and how best to manage this. I have described below how this doesn't work, but hopefully its enough to describe the problem.

Any pointers appreciated. Thanks.

I'm using a template file to deploy a bunch of near identical application servers. During the deployment of the application servers, a corresponding init script is placed using the variable:`

/etc/init.d/{{ application_instance }}`

Next I'd like to enable and ensure its started:

 name: be sure app_xyz is running and enabled
  service: name={{ application_instance }} state=started enabled=yes

Further on I'd like to call a restart of the the application when configuration files are updated:

- name: be sure app_xyz is configured
  template: src=xyz.conf dest=/opt/application/{{ application_server }}.conf
  notify:
    - restart {{ application_server }}

With the handler looking like this:

- name: restart {{ application_server }}
  service: name={{ application_server }} state=restarted

Upvotes: 1

Views: 758

Answers (1)

ProfHase85
ProfHase85

Reputation: 12183

You don't need a dynamical handler name for it. What about a static handler name:

# handler
- name: restart application server
  service: name={{ application_server }} state=restarted

# task
- name: be sure app_xyz is configured
  template: src=xyz.conf dest=/opt/application/{{ application_server }}.conf
  notify:
    - restart application server
  service: name={{ application_server }} state=restarted

Upvotes: 2

Related Questions