Ansible - 我需要检查给定的服务是否在目标 Windows 主机中运行,如果没有运行,则需要启动该服务

Ansible - 我需要检查给定的服务是否在目标 Windows 主机中运行,如果没有运行,则需要启动该服务

首先,我检查正在运行的 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

相关内容