You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在Ansible arubaoss_command的wait_for中使用正则匹配条件?

问题:ArubaOSS Command模块的wait_for参数是否支持正则匹配?

我在编写Ansible任务时,尝试使用arubanetworks.aos_switch.arubaoss_command模块的wait_for参数判断条件是否满足,当前任务配置如下:

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

当前配置通过固定字符串匹配满足条件后才执行后续任务,但目标字符串虽有固定模式(比如 1 xx:xx:xx-xx:xx:xx这类格式),实际内容是可变的。我的环境为AWX版本<21.13.0,查阅文档仅发现wait_for支持纯字符串匹配,请问是否支持类似wait_for: result[0] matches(\s+......-......)的正则匹配方式?


回答

arubanetworks.aos_switch.arubaoss_command模块的wait_for参数支持Ansible原生的测试运算符,其中就包括matches用于正则匹配,完全可以实现你需要的模式匹配需求。

你可以直接将wait_for的条件修改为正则匹配的形式,比如针对你提到的空格+1+空格+6位十六进制-6位十六进制的固定模式,示例配置如下:

- 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] matches '^\s+1\s+[0-9a-f]{6}-[0-9a-f]{6}$'
  register: port_security_intrusion_output
  when: 
    - int_brief_filtred.msg | length > 0
  loop: "{{ int_brief_filtred.msg }}"

补充说明:

  • matches后面的正则表达式可根据实际输出的格式调整,比如MAC地址分隔符、长度有变化时,对应修改正则规则即可
  • AWX 21.13.0依赖的Ansible核心版本已原生支持matches测试运算符,你的环境可以正常使用该功能

内容的提问来源于stack exchange,提问作者ExploitZ

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.12 06:25:12