在ansible中从until循环打印自定义消息

mon*_*onk 5 ansible

我正在尝试多次运行命令并检查输出中是否包含某些字符串(“hi”)。我有目的地模拟失败并期望until循环失败。到目前为止一切都很好。

现在,我需要一些自定义消息来说明until循环或task失败的原因。例如:"Your command failed to print hi"

所以问题是,如果循环在重试中失败,如何从直到循环打印自定义消息。

剧本:

-->cat until.yml
---

- hosts: localhost
  gather_facts: no
  tasks:

    - name: "check command"
      shell: echo hello
      register: var
      until: var.stdout.find('hi') != -1
      retries: 5
      delay: 1
Run Code Online (Sandbox Code Playgroud)

剧本输出:

 -->ansible-playbook until.yml
PLAY [localhost] *************************************************************************************************************************************************************************************************************************

TASK [check command] ********************************************************************************************************************************************************************************************************
FAILED - RETRYING: who triggered the playbook (5 retries left).
FAILED - RETRYING: who triggered the playbook (4 retries left).
FAILED - RETRYING: who triggered the playbook (3 retries left).
FAILED - RETRYING: who triggered the playbook (2 retries left).
FAILED - RETRYING: who triggered the playbook (1 retries left).
fatal: [localhost]: FAILED! => {
    "attempts": 5,
    "changed": true,
    "cmd": "echo hello",
    "delta": "0:00:00.003004",
    "end": "2019-12-03 10:04:14.731488",
    "rc": 0,
    "start": "2019-12-03 10:04:14.728484"
}

STDOUT:

hello


PLAY RECAP *******************************************************************************************************************************************************************************************************************************
localhost                  : ok=0    changed=0    unreachable=0    failed=1
Run Code Online (Sandbox Code Playgroud)

Zei*_*tor 0

查看可用于此目的的块错误处理。

基本概况:

- block:
    - name: A task that may fail.
      debug:
        msg: "I may fail"
      failed_when: true
      register: might_fail_exec

  rescue:
    - name: fail nicely with a msg
      fail:
        msg: "The task that might fail has failed. Here is some info from the task: {{ might_fail_exec }}"
Run Code Online (Sandbox Code Playgroud)