network-engine - parser problem

Hi,

…I’m currently having a look at network-engine and so far it looks really promising! Seems like the best solution for network automation there is today :slight_smile: However, I’m having trouble parsing a certain configuration on a cisco device:

interface port-channel99
switchport
switchport mode trunk
switchport trunk allowed vlan 15-19,25-29,35-39,45-49,55-59,65-69
switchport trunk allowed vlan add 85-89,95-99,636,698,752,901,997
switchport trunk allowed vlan add 1104,1119,1999,3800-3810
mtu 9216

What I like to achieve is to parse the valuse of “switchport trunk allowed vlan” and all values of “switchport trunk allowed vlan add” into a single fact variable. My currenty code was inspired by the examples for network-engine I could find on the web, but obviously this does not solve my problem:

Hi,

You might want to use `match_all: yes` which doesn't stop on your first match.

- name: trunk allowed vlan
  pattern_match:
    regex: " switchport trunk allowed vlan (.*)"
    content: "{{ item }}"
    match_all: yes
  register: allowed_vlans

Return would be somewhat like

"allowed_vlans": [
  {
    "matches": "15-19,25-29,35-39,45-49,55-59,65-69"
  },
  {
    "matches": "add 85-89,95-99,636,698,752,901,997"
  },
  {
    "matches": "add 1104,1119,1999,3800-3810"
  }
],

Regards,

Hi Trishna,

You might want to use `match_all: yes` which doesn't stop on your first
match.

- name: trunk allowed vlan
  pattern_match:
    regex: " switchport trunk allowed vlan (.*)"
    content: "{{ item }}"
    match_all: yes
  register: allowed_vlans

Thanks for your reply. I finally got it by using your recommendation above. But had to concat/string-replace the 'add ' keyword within that string, which brings me back to my initial question: is it possible to have multiple groups within the regex_match?

e.g.

regex: " switchport trunk allowed (vlan )? (.*)"

...and only use the content of the second group? I tried, but failed :frowning: ...

Thanks and best regards,

Andreas

Hi Andy,

You will need to tweak the regex and the template parsing a bit.

    - name: trunk allowed vlan
      pattern_match:
        regex: "  switchport trunk allowed vlan (?:\w+\s+|)(.+)$"
        content: "{{ item }}"
        match_all: yes
      register: allowed_vlans

- name: generate json data structure
  json_template:
    template:
      - key: "{{ item.name.matches.0 }}"
        object:
          - key: description
            value: "{{ item.description.matches.0 }}"
          - key: allowed_vlans
            repeat_for: "{{ item.allowed_vlans }}"
            repeat_var: nested_item
            elements: "{{ nested_item.matches }}"

"elements" will store the vlans as list

    "ansible_facts": {
        "interface_facts": {
            "port-channel99": {
                "allowed_vlans": [
                    "15-19,25-29,35-39,45-49,55-59,65-69",
                    "85-89,95-99,636,698,752,901,997",
                    "1104,1119,1999,3800-3810"
                ],
                "description": null
            }
        }
```
I believe this is what you are looking for.

Regards,