You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何移除Ansible提取AP MAC地址时输出中的null值?

如何从Ansible提取的AP MAC地址列表中移除null值

问题背景

执行Aruba WLC的show ap database long命令后,用Ansible提取AP的MAC地址,由于输出包含标题、分隔线等无关行,导致提取结果中出现多个null值,需要移除这些无效值。

设备原始输出

(wc1) *#show ap database long

AP Database
-----------
Name                Group         AP Type  IP Address    Status              Flags  Switch IP   Standby IP  Wired MAC Address  Serial #    Port  FQLN  Outer IP  User
----                -----         -------  ----------    ------              -----  ---------   ----------  -----------------  --------    ----  ----  --------  ----
AP1                 INDOOR        325      192.13.7.36   Up 67d:10h:43m:3s   2Sr    192.13.7.9  192.13.7.8  80:8c:c7:ed:fe:42  CNFZHN415M  N/A   N/A   N/A       
AP2                 INDOOR        325      192.13.7.36   Up 67d:10h:43m:3s   2Sr    192.13.7.9  192.13.7.8  80:7c:c0:ed:fe:33  CNFZHN452M  N/A   N/A   N/A    
AP3                 INDOOR        325      192.13.7.36   Up 67d:10h:43m:3s   2Sr    192.13.7.9  192.13.7.8  80:6c:c9:ed:fe:45  CNFZHN453M  N/A   N/A   N/A    
AP4                 INDOOR        325      192.13.7.36   Up 67d:10h:43m:3s   2Sr    192.13.7.9  192.13.7.8  80:5c:c8:ed:fe:67  CNFZHN454M  N/A   N/A   N/A    

脚本运行输出

ok: [wc2] => (item=AP1_renamed  AIR_MONITOR   225      192.10.108.14  Up 96d:16h:0m:20s    2      192.10.108.4  0.0.0.0     60:f3:e6:d6:9e:06  CZ0825446   N/A   N/A   N/A       ) => {
    "ansible_facts": {
        "ap_mac_addresses": [
            null,
            null,
            null,
            null,
            "20:a6:3d:cc:93:1c",
            "20:a6:cd:dc:93:2e",
            "20:a6:3d:cc:03:3a",
            "20:a6:3d:cc:94:c6",
            "b0:b8:37:cd:3b:ea",
            "94:b4:3f:c7:2e:40",
            "94:b4:3f:c7:1e:4e",
            "94:b4:3f:c7:1d:20",
            "94:b4:3f:c7:2d:9c",
            "94:b4:2f:c7:3d:92",
            "94:b4:2f:c7:4d:9a",
            "04:bd:28:ca:52:3c",
            "94:b4:2f:c7:6d:b4",
            "94:b4:2f:c7:7d:06",
            "94:b4:0f:c7:8d:96",
            "94:b4:2f:c7:9c:e6",
            "40:e3:e6:c6:16:c0",
            "40:e3:e6:c6:22:4a",
            "40:e3:e6:c6:3e:c6"
        ]
    },
    "ansible_loop_var": "item",
    "changed": false,
    "item": "AP1_renamed  MONITOR   225      192.10.108.14  Up 96d:16h:0m:20s    2      192.10.108.4  0.0.0.0     60:f3:e6:d6:9e:06  CZ0825446   N/A   N/A   N/A       "
}

原Ansible脚本

---

- name: collect AP and wlc database and export to a csv file
  hosts: wlc
  gather_facts: no
  vars:
   ansible_connection: network_cli
   ansible_network_os: aruba
   
  tasks:

  - name: show AP inventory
    cli_command:
      command: "show ap database long"
    register: ap_database


  - name: Extract AP mac addresses
    set_fact:
      ap_mac_addresses: '{{ ap_mac_addresses | default ([]) + ([item |string | regex_search("([0-9A-Fa-f]{2}[:-]){5}([0-9A-Fa-f]{2})")]) }}'
       # ap_mac_addresses: '{{ ap_mac_addresses | default([]) + ([item | string | split()    
    with_items: "{{ap_database.stdout_lines}}"


  - name: print AP mac address output
    debug:
      msg: "{{ item }}"
    with_items: "{{ [] }}"

  - name: print not nulls
    debug: 
      msg: "{{ ap_mac_addresses | reject(none) | list }}"

解决方案

方法1:提取阶段过滤无关行(推荐)

直接在遍历输出行时,过滤掉不包含AP数据的行,从根源避免生成null值。可以通过匹配AP名称前缀(如AP\d+),或者匹配MAC地址格式来筛选有效行:

修改Extract AP mac addresses任务的with_items参数:

with_items: "{{ ap_database.stdout_lines | select('search', '^AP\\d+') | list }}"

或者更通用的MAC匹配:

with_items: "{{ ap_database.stdout_lines | select('search', '([0-9A-Fa-f]{2}[:-]){5}([0-9A-Fa-f]{2})') | list }}"

方法2:提取后过滤null值

如果已经生成包含null的列表,可以用Ansible过滤器移除无效值。原脚本中的reject(none)用法错误,正确写法如下:

  • 方法A:使用select()过滤器(自动过滤假值,包括null、空字符串、false)
msg: "{{ ap_mac_addresses | select() | list }}"
  • 方法B:明确匹配null值并排除
msg: "{{ ap_mac_addresses | reject('equalto', none) | list }}"

优化后的完整脚本

---

- name: collect AP and wlc database and export to a csv file
  hosts: wlc
  gather_facts: no
  vars:
    ansible_connection: network_cli
    ansible_network_os: aruba
   
  tasks:

  - name: show AP inventory
    cli_command:
      command: "show ap database long"
    register: ap_database

  - name: Extract AP mac addresses
    set_fact:
      ap_mac_addresses: '{{ ap_mac_addresses | default([]) + ([item | string | regex_search("([0-9A-Fa-f]{2}[:-]){5}([0-9A-Fa-f]{2})")]) }}'
    # 只遍历包含AP数据的行,避免生成null
    with_items: "{{ ap_database.stdout_lines | select('search', '^AP\\d+') | list }}"

  - name: Print cleaned AP MAC addresses
    debug:
      msg: "{{ ap_mac_addresses | select() | list }}"

内容的提问来源于stack exchange,提问作者Lrn

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.07 20:55:25