如果stdout结果等于失败,如何使用Ansible重新启动服务器?

问题描述

我为jboss编写任务,但是如果stdout结果等于失败,我不知道如何重新启动服务器。

我的任务

- name: Server
  become: true
  check_deployments: 
    server: 111.111.111.111:8888
    command: /deployment=*:read-attribute(name=status)
    cli_path: /testusers/wildfly/bin
    user: admin
    password: admin
  register: command

- name: wf_debug  
  debug:
    msg: "{{ command.stdout_lines}}"

输出

TASK [check_wf_status : wf_debug] ******************************************************************************************************************************************
ok: [111.111.111.111] => {
    "msg": [
        "{","    \"outcome\" => \"success\",","    \"result\" => [","        {","            \"address\" => [(\"deployment\" => \"test-module1\")],"            \"outcome\" => \"success\","            \"result\" => \"OK\"","        },"            \"address\" => [(\"deployment\" => \"test-module2\")],"            \"result\" => \"Failed\"","    ]","}"
    ]
}

如果result = Failed,如何使用Ansible重启服务器?

解决方法

如果result = FAILED,如何使用Ansible重启服务器?

这应该可以工作,就像to the docs

- name: Unconditionally reboot the machine with all defaults,when the check_deployments task has failed
  reboot:
  when: "'FAILED' in command.stdout_lines"

请注意,您正在注册“命令”。这也是一个不良的模块,可能引起混乱。最好用其他名称注册它,例如register: check_deploy