在Ansible中运行Cisco IOSXR命令的pattern_match时遇错误
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:
- Added
register: interface_outputto theiosxr_commandtask so we can access its output in subsequent steps. - Replaced the non-existent
pattern_matchtask withset_fact, using Jinja2'sregex_searchfilter to apply your regex directly to the command output. - Updated the debug task to reference the new
last_clearedfact 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_findallmodule returns a list of matches, so we access the first match withmatches[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

