重复执行重复任务,直到输出中包含指定的字符串

时间:2019-12-19 12:05:25

标签: ansible big-ip

我有一项任务需要执行,直到stdout中有某个字符串为止。 bigip_command的标准输出是列表(https://docs.ansible.com/ansible/latest/modules/bigip_command_module.html#return-values

    - name: Disable pool member
      bigip_command:
        commands: "tmsh show /sys connection ss-server-addr xx.xx.xx.xx ss-server-port 8080"
        provider:
          user: "xx"
          password: "xxx"
          server_port: xx
          server: xxx
      delegate_to: localhost
      register: result
      until: "'Total records returned: 0' in result.stdout"<br><br>

输出为:

失败-重试:停用成员池(还剩3次重试)

但是返回的总记录数:0在result.stdout中。

使用debug的输出是:

    - name: debug
      debug:
        msg: "{{ result.stdout }}"

输出:

    ok: [xxx] => {
        "msg": [
            "Sys::Connections\nTotal records returned: 0"
       ]
    }

1 个答案:

答案 0 :(得分:0)

请参见Retrying a task until a condition is met。试试

- name: Disable pool member
  vars:
    my_regex: 'Total records returned: 0'
  bigip_command:
    commands: "yyy"
    user: "xx"
    password: "xxx"
    server_port: xx
    server: xxx
  delegate_to: localhost
  register: result
  until: result.stdout is search(my_regex)
  retries: 5
  delay: 10

(未经测试)