Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
324 views
in Technique[技术] by (71.8m points)

loops - I have a Jinja template that only prints the first match of a regex match but I would like it to print all matches

I run a playbook that executes a Cisco command show interfaces and I store that into a variable. I then jump into a template for further processing to only find Ethernet interfaces and errors.

I find the first match of both interface and error which is fine but I do not find the second match of interface and error in the control environment. The lab device has two interfaces Ethernet0/0 and Ethernet0/1.

Can someone make a suggestion so that I can match on both interfaces and respective errors?

Here is the task:

Blockquote

tasks:
- name: gather interface stats and store in register
  ios_command:
    commands: 
      - show interface | section include is up
  register: interface_errors

- name: store interface_errors_file
  copy:
    content: {{interface_errors}}
    dest: ./{{inventory_hostname}}_interface_full.txt
  
- name: deploy template
  copy:
    content: "{{lookup('template', 'errors.j2') }}"
    dest: ./{{inventory_hostname}}_errors.txt

Template:

Blockquote

  {% for int in interface_errors.stdout %}
  {% set int_name = int | regex_search('Ethernetd{1,3}/d{1,3} is up') %}
  {% if 'Ethernet' in int_name | string %}
   - Interface: {{int_name}}
  {% endif %}
  {% endfor %}

{% for int_err in interface_errors.stdout %}
{% set int_errors = int_err | regex_search('d{1,10000}.input.errors|output.errors') %}
{% if 'errors' in int_errors %}
- Interface Errors: {{int_errors}}
{% endif %}
{% endfor %}
question from:https://stackoverflow.com/questions/65911132/i-have-a-jinja-template-that-only-prints-the-first-match-of-a-regex-match-but-i

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...