Search code examples
ansiblebig-ip

Ansible repeat task until a specified string is in the output


I have an task that needs to be executed till there is a certain string in the stdout. The stdout of bigip_command is a list (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>

Output is:

FAILED - RETRYING: Disable pool member (3 retries left)

But Total records returned: 0 is in the result.stdout.

with debug the output is:

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

output:

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

Solution

  • See Retrying a task until a condition is met. Try

    - 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
    

    (not tested)