First, I check the list of windows process are running and collect the output into a register variable. next I need to start the service which are not running based on the above output(register).
- name: Check weather service is running or not
ansible.windows.win_service_info:
name: "{{ item }}"
register: win_service_info
with_items:
- BrokerInfrastructure #Background Tasks Infrastructure (BrokerInfrastructure) Service
- DcomLaunch #DCOM Server Process Launcher Service
- gpsvc #Group Policy Client Service
- name: run services
ansible.windows.win_service:
name: << don't know how to write >>
start_mode: auto
state: started
register: service_startup
with_items: "{{ win_service_info.results }}"
when: << i don't know how to write >>
You don't need to check whether the service is running in the use case you describe. Just tell Ansible you want the services to be enabled and running as the end-state. In other words, remove the first block and use only the second: