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

如何用Ansible过滤ESXi vmnic指定字段并校验状态?

如何用Ansible Playbook筛选ESXi vmnic指定字段并做合规检查

原始Playbook代码

---
- hosts: host
  vars:
    vcenter_server: "192.168.35.219"
    vcenter_user: "root"
    vcenter_pass: "P@ssw0rd"
    esxi_hostname: "esxihost1"
  gather_facts: true


  - name: Gather info about vmnics of an ESXi Host
    community.vmware.vmware_host_vmnic_info:
      hostname: '{{ vcenter_server }}'
      username: '{{ vcenter_user }}'
      password: '{{ vcenter_pass }}'
      esxi_hostname: '{{ esxi_hostname }}'
      validate_certs: no
    delegate_to: localhost
    register: host_vmnics

  - name: print esxi info
    ansible.builtin.debug: 
      var: host_vmnics.hosts_vmnics_info.esxihost1.vmnic_details

当前执行结果

TASK [Gather info about vmnics of an ESXi Host] ***********************************************************************************************************
ok: [192.168.35.219 -> localhost]

TASK [print esxi info] ************************************************************************************************************************************
ok: [192.168.35.219] => {
    "host_vmnics.hosts_vmnics_info.esxihost1.vmnic_details": [
        {
            "actual_duplex": "Full Duplex",
            "actual_speed": 10000,
            "adapter": "VMware Inc. vmxnet3 Virtual Ethernet Controller",
            "configured_duplex": "Full Duplex",
            "configured_speed": 10000,
            "device": "vmnic0",
            "driver": "nvmxnet3",
            "lldp_info": "N/A",
            "location": "0000:0b:00.0",
            "mac": "00:0c:29:bc:67:65",
            "status": "Connected"
        }
    ]
}

需求说明

  • 仅展示actual_duplex、actual_speed、device、status四个字段
  • 当这四个字段的值分别匹配Full Duplex、10000、vmnic0、Connected时,输出OK;否则输出NOTOK

实现方案

修改后的Playbook如下,通过json_query筛选字段,再对比预期值输出状态:

---
- hosts: host
  vars:
    vcenter_server: "192.168.35.219"
    vcenter_user: "root"
    vcenter_pass: "P@ssw0rd"
    esxi_hostname: "esxihost1"
    # 定义预期合规值
    expected_vmnic:
      actual_duplex: "Full Duplex"
      actual_speed: 10000
      device: "vmnic0"
      status: "Connected"
  gather_facts: true

  tasks:
    - name: 收集ESXi主机vmnic信息
      community.vmware.vmware_host_vmnic_info:
        hostname: '{{ vcenter_server }}'
        username: '{{ vcenter_user }}'
        password: '{{ vcenter_pass }}'
        esxi_hostname: '{{ esxi_hostname }}'
        validate_certs: no
      delegate_to: localhost
      register: host_vmnics

    - name: 提取指定字段的vmnic信息
      set_fact:
        filtered_vmnic: "{{ host_vmnics.hosts_vmnics_info[esxi_hostname].vmnic_details | json_query('[].{actual_duplex: actual_duplex, actual_speed: actual_speed, device: device, status: status}') }}"

    - name: 输出筛选后的vmnic信息
      ansible.builtin.debug:
        var: filtered_vmnic

    - name: 状态合规输出OK
      ansible.builtin.debug:
        msg: "OK"
      when: filtered_vmnic[0] == expected_vmnic

    - name: 状态不合规输出NOTOK
      ansible.builtin.debug:
        msg: "NOTOK"
      when: filtered_vmnic[0] != expected_vmnic

关键说明

  1. 字段筛选:使用json_query从原始结果中提取需要的四个字段,生成精简的信息结构
  2. 合规检查:提前定义预期值变量expected_vmnic,通过when条件判断筛选后的信息是否匹配预期,输出对应状态
  3. 多vmnic场景适配:如果主机有多个vmnic,可通过循环(如loop: "{{ filtered_vmnic }}")逐个检查每个vmnic的状态

内容的提问来源于stack exchange,提问作者hyun.ji

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.07 17:20:26