0

winapp.example.comHello, I have an inventory file that holds both windows application servers, web servers and only respective services which need to stopped or restarted. Requirement is only specific services should be stopped for group of hosts. For e.g. appservers hosts should use services under appservers:vars

Appreciate your help !!!

Inventory\hosts.ini file contains

[appservers]
winapp1.example.com
winapp2.example.com

[appservers:vars]
  services:
    - WorkflowService
    - ConfigurationService
    - SyncService
    - ParentConfigurationService

[slaveappservers]
winslvapp1.example.com
winslvapp2.example.com

[slaveappservers:vars]
  services:
    - SyncService
    - ParentConfigurationService

[webservers] 
webser1.ent.wfb.bank.corp

[webservers:vars]
  services:
    - WorkflowService
    - SyncService
    - ParentConfigurationService

[allservers:children]
appservers
slaveappservers
webservers

service_stop.yml

---
- name:  SHRP service stop Demo
  hosts: all
  gather_facts: false

  tasks:
  - name: Pause a service
    win_service:
      name: "{{ item }}"
      state: stopped
    loop: 
      - "{{ services }}"
John Mahowald
  • 32,050
  • 2
  • 19
  • 34

1 Answers1

1

Your already have one solution. Define a variable with the same name, but group specific values. Use that one variable name in plays that can run on both groups.

Several syntaxes exist to get group specific values.

  • Defining a variable in inventory for a group
  • In group_vars files adjacent to playbook or inventory.
  • A lookup expression or dict indexed by group name.

Yours is an example of the first.

John Mahowald
  • 32,050
  • 2
  • 19
  • 34
  • I thought I provided a variable name as "services" to make it common across groups. With above settings I get an error when play is executed as "services" not defined. Appreciate your help !!! – Anand Subra Feb 28 '22 at 15:38
  • Please edit your question to add the error. – John Mahowald Feb 28 '22 at 17:21
  • I have the same code mentioned above and When I ran the playbook, I got the following error. { "msg": "Invalid data passed to 'loop', it requires a list, got this instead: services. Hint: If you passed a list/dict of just one element, try adding wantlist=True to your lookup invocation or use q/query instead of lookup.", "_ansible_no_log": null } – Anand Subra Feb 28 '22 at 20:14