Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/templates/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ansible win_服务停止失败-如何断言?_Ansible - Fatal编程技术网

Ansible win_服务停止失败-如何断言?

Ansible win_服务停止失败-如何断言?,ansible,Ansible,我在ansible中为windows服务执行一个简单的服务停止: - name: stop service win_service: name: "{{ tomcat_srv_name }}" state: stopped 由于远程服务器出现问题,停止失败。如果我在远程服务器上尝试,我会得到一个超时。但上述可解释的说法永远悬而不决。 有办法抓住这个吗?像wait_for…?这样的功能应该可以工作(没有在windows机器上使用过)。您可能需要根据停止tomcat通常需要的

我在ansible中为windows服务执行一个简单的服务停止:

- name: stop service
  win_service:
    name: "{{ tomcat_srv_name }}"
    state: stopped 
由于远程服务器出现问题,停止失败。如果我在远程服务器上尝试,我会得到一个超时。但上述可解释的说法永远悬而不决。 有办法抓住这个吗?像wait_for…?

这样的功能应该可以工作(没有在windows机器上使用过)。您可能需要根据停止tomcat通常需要的时间稍微调整这些值

- name: stop service
  win_service:
    name: "{{ tomcat_srv_name }}"
    state: stopped
  async: 45
  poll: 5

根据@kfreezy的注释,我构建了此块以捕获潜在错误并作出相应反应:

  block:
    # try to stop the service
    - win_service:
        name: "{{ srv_name }}"
        state: stopped
      async: 45
      poll: 5
      register: service_stop_info
    - debug:
        msg: "STOP seevice {{ srv_name }} results in: {{ service_stop_info.state }}"
  rescue:
    # in case the service can not be stopped, kill its process 
    - name: Kill process of service
      win_command: taskkill /f /fi "Services eq {{ srv_name }}"
      register: cmd_result_service_kill
    - debug: 
        msg: "KILL process of service {{ srv_name }} results in: {{ cmd_result_service_kill.stdout }}"  
  always:
    # restart the service 
    - win_service:
        name: "{{ srv_name }}"
        state: started 
      register: service_start_info
    - debug:
        msg: "START service {{ srv_name }} results in: {{ service_start_info.state }}"