---
- name: parser meta data
  parser_metadata:
    version: 1.0
    command: show lldp neighbors detail
    network_os: ios

- name: match sections
  pattern_match:
    regex: "^-----.*"
    match_all: true
    match_greedy: true
  register: context

- name: parse lldp neighbors
  pattern_group:
    - name: parse local port
      pattern_match:
        regex: "Local Intf: (.+)"
        content: "{{ item }}"
      register: local_port

    - name: parse remote prort
      pattern_match:
        regex: "Port id: (.+)"
        content: "{{ item }}"
      register: remote_port

    - name: parse remote host
      pattern_match:
        regex: "System Name: (.+)"
        content: "{{ item }}"
      register: remote_host

  loop: "{{ context }}"
  register: matches

- name: build lldp neighbor facts
  register: lldp
  export: true
  extend: "{{ toplevel | default('cisco_ios') }}"
  json_template:
    template:
      - key: neighbors
        elements:
          - key: port
            value: "{{ item.local_port.matches.0  | expand_interface_name }}"
          - key: neighbor
            value: "{{ item.remote_host.matches.0 }}"
          - key: neighbor_port
            value: "{{ item.remote_port.matches.0 }}"
        repeat_for: "{{ matches }}"
