# network-engine - parser problem

**URL:** <https://forum.ansible.com/t/network-engine-parser-problem/29740>\
**Category:** Ansible Project\
**Created:** [February 22, 2019, 5:27am UTC](https://forum.ansible.com/t/network-engine-parser-problem/29740 "2019-02-22T05:27:37Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![andybourges](https://avatars.discourse-cdn.com/v4/letter/a/a3d4f5/32.png) [@andybourges](https://forum.ansible.com/u/andybourges)\
**Post date:** [February 22, 2019, 5:27am UTC](https://forum.ansible.com/t/network-engine-parser-problem/29740/1 "2019-02-22T05:27:37Z")

</div>

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 🙂 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:

---

<div class="post-metadata">

**Author:** ![trishna](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/trishna/32/963_2.png) [@trishna](https://forum.ansible.com/u/trishna)\
**Post date:** [February 22, 2019, 12:57pm UTC](https://forum.ansible.com/t/network-engine-parser-problem/29740/2 "2019-02-22T12:57:28Z")

</div>

Hi,

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

- name: trunk allowed vlan  
&nbsp;&nbsp;pattern\_match:  
&nbsp;&nbsp;&nbsp;&nbsp;regex: " switchport trunk allowed vlan (.\*)"  
&nbsp;&nbsp;&nbsp;&nbsp;content: "{{ item }}"  
&nbsp;&nbsp;&nbsp;&nbsp;match\_all: yes  
&nbsp;&nbsp;register: allowed\_vlans

Return would be somewhat like

```auto
"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,

---

<div class="post-metadata">

**Author:** ![andybourges](https://avatars.discourse-cdn.com/v4/letter/a/a3d4f5/32.png) [@andybourges](https://forum.ansible.com/u/andybourges)\
**Post date:** [February 22, 2019, 3:55pm UTC](https://forum.ansible.com/t/network-engine-parser-problem/29740/3 "2019-02-22T15:55:18Z")

</div>

Hi Trishna,

> You might want to use `match_all: yes` which doesn't stop on your first  
> match.
> 
> - name: trunk allowed vlan  
> &nbsp;&nbsp;pattern\_match:  
> &nbsp;&nbsp;&nbsp;&nbsp;regex: " switchport trunk allowed vlan (.\*)"  
> &nbsp;&nbsp;&nbsp;&nbsp;content: "{{ item }}"  
> &nbsp;&nbsp;&nbsp;&nbsp;match\_all: yes  
> &nbsp;&nbsp;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 ☹ ...

Thanks and best regards,

Andreas

---

<div class="post-metadata">

**Author:** ![trishna](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/trishna/32/963_2.png) [@trishna](https://forum.ansible.com/u/trishna)\
**Post date:** [February 25, 2019, 3:35pm UTC](https://forum.ansible.com/t/network-engine-parser-problem/29740/4 "2019-02-25T15:35:41Z")

</div>

Hi Andy,

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

```auto
    - 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

```

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

Regards,
