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

在Ansible中运行Cisco IOSXR命令的pattern_match时遇错误

Fixing the "no action detected" Error with pattern_match in Ansible

First, let's break down why you're seeing that error: Ansible doesn't have a built-in pattern_match module—that's the core reason for the "no action detected" message. You're trying to use a module that doesn't exist in default Ansible modules or collections.

Here are two reliable ways to achieve your goal: extracting the text after "counters" from your IOS-XR command output.

Option 1: Use Jinja2's regex_search Filter with set_fact

This is the simplest approach since it uses core Ansible functionality, no extra collections required.

Modify your playbook like this:

---
- hosts: SCRING
  gather_facts: true
  connection: network_cli
  tasks:
    - name: show int | inc clearing
      iosxr_command:
        commands: show int bundle-ether 21 | inc clearing
      register: interface_output

    - name: Extract last cleared time using regex
      set_fact:
        last_cleared: "{{ interface_output.stdout[0] | regex_search('(?<=counters ).*$') }}"

    - debug:
        msg: "{{ inventory_hostname }} counters last cleared - {{ last_cleared }}"

Key Changes:

  1. Added register: interface_output to the iosxr_command task so we can access its output in subsequent steps.
  2. Replaced the non-existent pattern_match task with set_fact, using Jinja2's regex_search filter to apply your regex directly to the command output.
  3. Updated the debug task to reference the new last_cleared fact we created.

Option 2: Use community.general.regex_findall Module

If you prefer a dedicated regex module, you can use community.general.regex_findall from the Community General collection. First, install the collection on your Ansible control node:

ansible-galaxy collection install community.general

Then adjust your playbook:

---
- hosts: SCRING
  gather_facts: true
  connection: network_cli
  tasks:
    - name: show int | inc clearing
      iosxr_command:
        commands: show int bundle-ether 21 | inc clearing
      register: interface_output

    - name: Get last cleared time with regex module
      community.general.regex_findall:
        string: "{{ interface_output.stdout[0] }}"
        regex: "(?<=counters ).*$"
      register: last_cleared_result

    - debug:
        msg: "{{ inventory_hostname }} counters last cleared - {{ last_cleared_result.matches[0] }}"

Notes:

  • The community.general.regex_findall module returns a list of matches, so we access the first match with matches[0].
  • Ensure the Community General collection is installed on the machine running Ansible before using this option.

Either of these approaches will fix the "no action detected" error and let you extract the desired text using your regex.

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.14 07:53:05