-1

I am looking to get list of Windows services that is running along with their state by using win_service_info module – Gather information about Windows services.

---
- name: Service Status
  hosts: test
  gather_facts: yes

  tasks:
  
  - name: Create variable with services
    set_fact: 
      service_names: []

  - name: Get service info
    ansible.windows.win_service_info:
      name: USRJOB*
    register: service_info

  - name: Load variable
    set_fact:
      service_names: "{{ service_names }}" + {{ item }}
    loop: "{{ service_info | community.general.json_query('services[*].name') }}"
    
  - name: Print the Countries after adding element
    debug: var=service_names

I couldn't load the variable service_names with all the services, no idea on how to also show the state of the service on the same debug message.

U880D
  • 8,601
  • 6
  • 24
  • 40
Raj
  • 1
  • 1
    Can you share the content of `service_info`? Or provide a [minimal reproducible example](https://stackoverflow.com/help/minimal-reproducible-example)? – U880D May 23 '23 at 23:12

1 Answers1

1

just change the third task to:

  - name: Load variable
    set_fact:
      service_names: "{{ service_names  + [ item ] }}"
    loop: "{{ service_info | community.general.json_query('services[*].name') }}"
    

Edit:

and if you want the state too, so you can change this task to:

  - name: Load variable
    set_fact:
      service_names: "{{ service_names  + [ item.name ~ ':' ~ item.state ] }}"
    with_items:
      - "{{ service_info.services }}"
Oliver Gaida
  • 1,722
  • 7
  • 14