How can I evaluate a "wait_for" condition using regex?

48 Views Asked by At

I am trying to check if a condition is met using the wait_for argument on a task:

- name: GET SHOW PORT SECURITY INTRUSION OUTPUT
  arubanetworks.aos_switch.arubaoss_command:
    commands:
      - "show port-security intrusion-log | i ^  {{item.Port}}    ."
    wait_for: result[0] contains '  1     bc9fe4-c653a4'
  register: port_security_intrusion_output
  when: 
    - int_brief_filtred.msg | length > 0
  loop: "{{ int_brief_filtred.msg }}"

This way, the playbook will only move to the next task if the condition is met. The problem is: This string will vary, but always following the same pattern.

AWX version: < AWX 21.13.0 > ansible module: arubaoss_command ansible collection: arubanetworks.aos_switch

On the documentations I only found the possibility of evaluating pure strings, not regex.

Is there a wait_for condition option like this? wait_for: result[0] matches(\s+......-......)

1

There are 1 best solutions below

0
ExploitZ On

As you can see in all of the documentation, all the examples only uses the keyword "contains". They dont show other possibilities such as "find" or "regex". I tested them and didnt work. The only thing that worked and its not in the documentation is the "not contains". And with a combination of "contains" AND "not contains", i was capable to achieve what i needed:

  - name: GET SHOW PORT SECURITY INTRUSION OUTPUT
      arubanetworks.aos_switch.arubaoss_command:
        commands:
          - "show port-security intrusion-log | i ^  {{item.Port}}  ."
        wait_for: 
          - result[0] contains '  {{item.Port}}  '
          - result[0] not contains 'Mode'
        match: all
      register: port_security_intrusion_output
      when: 
        - int_brief_filtred.msg | length > 0
      loop: "{{ int_brief_filtred.msg }}"