ansible-network / network-engine

This role provides the foundation for building network roles by providing modules and plugins that are common to all Ansible Network roles.
GNU General Public License v3.0
112 stars 53 forks source link

Minor syntax bug in your template #235

Closed hisaza closed 5 years ago

hisaza commented 5 years ago

ISSUE TYPE

ANSIBLE VERSION

ansible --version
ansible 2.7.9
  config file = /Users/hectorisaza/ansible.cfg
  configured module search path = [u'/etc/ansible/library']
  ansible python module location = /usr/local/lib/python2.7/site-packages/ansible
  executable location = /usr/local/bin/ansible
  python version = 2.7.16 (default, Mar  4 2019, 09:01:38) [GCC 4.2.1 Compatible Apple LLVM 10.0.0 (clang-1000.11.45.5)]

ansible-galaxy list | grep ansible.network
- ansible-network.network-engine, v2.7.5

Network OS

SUMMARY

STEPS TO REPRODUCE

In this path: https://github.com/ansible-network/network-engine/blob/devel/tests/command_parser/command_parser/parser_templates/ios/show_interfaces.yaml

There is a minor bug that has a comma next to the regex key, that will not display the captured data.

    - name: match hardware
      pattern_match:
        regex: "Hardware is (\\S+),"  <---- bug ","
        content: "{{ item }}"
      register: type

EXPECTED RESULTS

ok: [csr1] => { "interface": { "ansible_facts": { "interface_facts": [ { "GigabitEthernet1": { "config": { "description": null, "mtu": "1500", "name": "GigabitEthernet1", "type": "CSR" <----- Expect to see the type value to be populated } } },

ACTUAL RESULTS

ok: [csr1] => { "interface": { "ansible_facts": { "interface_facts": [ { "GigabitEthernet1": { "config": { "description": null, "mtu": "1500", "name": "GigabitEthernet1", "type": null <--------- Comes back as null } } },

kvernNC commented 5 years ago

Could also use : regex: "Hardware is ([^,]+),"

This could show another way working with python re.

joubbi commented 5 years ago

For the same result it's possible to use regex: "Hardware is (.*),"

Unfortunately I haven't figured out a way to show the loopback device type. For a loopback device the hardware line doesn't end with a comma. There is a newline right after the device type: Loopback2 is up, line protocol is up \n Hardware is Loopback\n Description:

kvernNC commented 5 years ago

Hi @joubbi,

for capturing both case, you can use non-greedy expression and use | to add multiple ending: regex: "Hardware is (.*?)(?:,|\\\n)"

see here

joubbi commented 5 years ago

Thank you @kvernNC ! I still have a lot to learn about regex ;-) I updated my version https://github.com/joubbi/command_parser_show_interfaces