在ansible中打印来自直到循环的自定义消息

在ansible中打印来自直到循环的自定义消息,ansible,Ansible,我尝试多次运行一个命令,并检查输出中是否包含一些字符串(“hi”)。我故意模拟失败,并期望直到循环失败。到目前为止一切都很好 现在,我需要一些自定义消息,说明为什么till循环或任务失败。例如:“您的命令无法打印hi” 所以问题是,如果循环在重试时无法通过,我如何从直到循环打印自定义消息 剧本: -->cat until.yml --- - hosts: localhost gather_facts: no tasks: - name: "check command"

我尝试多次运行一个命令,并检查输出中是否包含一些字符串(“hi”)。我故意模拟失败,并期望
直到
循环失败。到目前为止一切都很好

现在,我需要一些自定义消息,说明为什么
till
循环或
任务
失败。例如:
“您的命令无法打印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
剧本输出:

 -->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
看看哪些可以用于此目的

基本概况:

- 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 }}"

您可以将任务分为两个任务:

  • 第一个任务将使用
    直到
    循环轮询所需的输出。但是我们使用了
    ignore\u errors:True
    ,这样
    直到
    循环不会使剧本失败。我们将捕获结果

  • 在第二个任务中,使用
    assert
    打印成功案例的
    success\u msg
    ,打印失败案例的
    fail\u msg

  • 以下是经过调整的最小工作示例:

    ---
    
    - hosts: localhost
      gather_facts: no
      tasks:
    
        - name: "check command"
          shell: echo hello
          register: var
          until: var.stdout.find('hi') != -1
          retries: 5
          delay: 1
          ignore_errors: true
    
        - name: "Print result"
          assert:
            that: var.stdout|regex_search('hi')
            fail_msg: "COuld not find HI in command output"
            success_msg: "Hi is present in Command output"