Ansible 正则表达式与命令输出不匹配 - 空列表 -

Ansible 正则表达式与命令输出不匹配 - 空列表 -

我正在尝试使用带有正则表达式的 ansible 获取设备 ID 列表和端口 ID 列表,但我得到一个空列表,在输出下方,我试图解析它:

Device ID        Local Intrfce     Holdtme    Capability  Platform  Port ID
hello.fr.com #(this is in line separatly)
                 Fa 3/1/1         400             R S I    XXXX       Gi 3/3 #(and this in the next line)
cdp.fr.com
                 Fa 0/0/1         600            R S I     XXXX         Gi 3/3

Total cdp entries displayed : 2

这是我的代码:



tasks:
   - name: get neighbors
     ios_command:
       commands: 
         - show cdp neighbors
     register: output

   - set_fact: 
       reg_address: '(\S+[.]\S+[.]\S+[.]\S+)\s+'
       reg_ports: '\s+\S+\s\S+\s+\d+\s+\w\s\w\s\w\s+\S+\s+(\S+\s\d+[/]\d+)'

   - set_fact: 
       List_interfaces: []
       List_ports: []

   - set_fact: 
       List_interfaces: "{{List_interfaces + item | string | regex_search(reg_address, '\\1') }}"
     loop: "{{output.stdout_lines[0]}}"
     when: "{{ List_interfaces | length }} > 0"
   - set_fact: 
       List_ports: "{{List_ports + item | string | regex_search(reg_ports, '\\1') }}"
     loop: "{{ output.stdout_lines[0] }}"
     when: "{{ List_ports | length }} > 0"

答案1

例如

    - set_fact:
        No_Of_Neighbors: "{{ output.stdout_lines[-1]['Total cdp entries displayed'] }}"
    - set_fact:
        cdp_neighbors: "{{ cdp_neighbors|default({})|
                           combine({Device_ID: {'Port_ID': Port_ID}}) }}"
      loop: "{{ range(0, No_Of_Neighbors|int)|list }}"
      vars:
        Device_ID: "{{ output.stdout_lines[item * 2 + 1] }}"
        params: "{{ output.stdout_lines[item * 2 + 2].split() }}"
        Port_ID: "{{ params[-2] }} {{ params[-1] }}"

给出

  cdp_neighbors:
    cdp.fr.com:
      Port_ID: Gi 3/3
    hello.fr.com:
      Port_ID: Gi 3/3

这种解析并不通用。请参阅显示 cdp 邻居


问:获取设备ID列表和端口ID列表

答:例如

    - set_fact:
        dev_ids: "{{ dev_ids|default([]) + [Device_ID] }}"
        port_ids: "{{ port_ids|default([]) + [Port_ID] }}"
      loop: "{{ range(0, No_Of_Neighbors|int)|list }}"

给出(使用相同的变量)

  dev_ids:
  - hello.fr.com
  - cdp.fr.com

  port_ids:
  - Gi 3/3
  - Gi 3/3

问:我不明白这部分:

      vars:
        Device_ID: "{{ output.stdout_lines[item * 2 + 1] }}"
        params: "{{ output.stdout_lines[item * 2 + 2].split() }}"
        Port_ID: "{{ params[-2] }} {{ params[-1] }}"

答:下面的例子应该是不言自明的

- hosts: localhost
  vars:
    output: [line0, line1, line2, line3, line4, line5, line6]
  tasks:
    - debug:
        msg: "{{ item }}
              {{ output[item * 2 + 1] }}
              {{ output[item * 2 + 2] }}"
      loop: "{{ range(0, 2)|list }}"

给出

ok: [localhost] => (item=0) => {
    "msg": "0 line1 line2"
}
ok: [localhost] => (item=1) => {
    "msg": "1 line3 line4"
}

下面的任务给出了相同的结果

    - debug:
        msg: "{{ x }} {{ y }}  {{ z }}"
      loop: "{{ range(0, 2)|list }}"
      vars:
        x: "{{ item }}"
        y: "{{ output[item * 2 + 1] }}"
        z: "{{ output[item * 2 + 2] }}"

注意:截至 2021 年 6 月,我仍然找不到 ios_commands 的生产解析器。例如,仅限 Develansible-ttp。欢迎发表评论。

相关内容