PratapReddy Dodda
PratapReddy Dodda

Reputation: 35

Stop Tomcat Service based on environment using Ansible

I am working on ansible scripts to stop tomcat service on remote hosts

we have different environments, on one of the environment the service name is different and remaining environments it is same

Below is the task i am using

- name: stop WEB service
    win_service:
      name: "{{ item }}"
      state: stopped
    loop: "{{ web_service }}"

group vars

web_service:
  - 'Tomcat9.0.45'

On one environment the service name is Tomcat9.0.45 and remaining environment it is Tomcat9

Can anyone help me how to update task to stop service based on environment?

Upvotes: 0

Views: 175

Answers (1)

Chen A.
Chen A.

Reputation: 11328

There are couple ways to solve this. One robust option would be to add a variable to every group/host with the relevant version. For example, configure this on the relevant group / host:

tomcat_version: 9.0.45

And on the second group / hosts:

tomcat_version: 9

and in your task, you won't need the loop at all

- name: stop WEB service
  win_service:
    name: "Tomcat{{ tomcat_version }}"
    state: stopped

This way, every group/host has it's own version. It's easier to change in the future as well (think if tomorrow, you would have 9.0.60?)

Upvotes: 1

Related Questions