首先,我检查正在运行的 Windows 进程列表,并将输出收集到寄存器变量中。接下来,我需要根据上述输出(寄存器)启动未运行的服务。
- 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 >>
答案1
您无需在您描述的用例中检查服务是否正在运行。只需告诉 Ansible 您希望服务作为最终状态启用并运行即可。换句话说,删除第一个块并仅使用第二个块:
- name: run services
ansible.windows.win_service:
name: "{{ item }}"
start_mode: auto
state: started
with_items:
- BrokerInfrastructure #Background Tasks Infrastructure (BrokerInfrastructure) Service
- DcomLaunch #DCOM Server Process Launcher Service
- gpsvc #Group Policy Client Service