From 8b04d75421c67e540a0e2115ca98d4688611552d Mon Sep 17 00:00:00 2001 From: wangruidong <940853815@qq.com> Date: Fri, 26 Jun 2026 16:36:48 +0800 Subject: [PATCH] perf: gather mac address --- .../automations/gather_facts/host/posix/main.yml | 15 +++++++++++++++ .../gather_facts/host/windows/main.yml | 11 +++++++++++ apps/assets/serializers/asset/info/gathered.py | 5 +++++ 3 files changed, 31 insertions(+) diff --git a/apps/assets/automations/gather_facts/host/posix/main.yml b/apps/assets/automations/gather_facts/host/posix/main.yml index 9acfc7e7e..96abb5929 100644 --- a/apps/assets/automations/gather_facts/host/posix/main.yml +++ b/apps/assets/automations/gather_facts/host/posix/main.yml @@ -24,6 +24,21 @@ kernel: "{{ ansible_kernel }}" + - name: Get MAC addresses + shell: | + for iface in $(ls /sys/class/net/ | grep -v '^lo$'); do + mac=$(cat /sys/class/net/$iface/address 2>/dev/null) + if [ -n "$mac" ] && [ "$mac" != "00:00:00:00:00:00" ]; then + echo "${iface}: ${mac}" + fi + done + register: mac_info + ignore_errors: yes + + - name: Merge MAC info into final info + set_fact: + info: "{{ info | combine({'mac_address': mac_info.stdout_lines | default([])}) }}" + - name: Get GPU info with nvidia-smi shell: | nvidia-smi --query-gpu=name,memory.total,driver_version --format=csv,noheader,nounits diff --git a/apps/assets/automations/gather_facts/host/windows/main.yml b/apps/assets/automations/gather_facts/host/windows/main.yml index 57e18d25f..61e3081fa 100644 --- a/apps/assets/automations/gather_facts/host/windows/main.yml +++ b/apps/assets/automations/gather_facts/host/windows/main.yml @@ -16,5 +16,16 @@ cpu_vcpus: "{{ ansible_processor_vcpus }}" memory: "{{ (ansible_memtotal_mb / 1024) | round(2) }}" + - name: Get MAC addresses on Windows + win_shell: | + Get-NetAdapter | Where-Object { $_.MacAddress -and $_.MacAddress -ne '00-00-00-00-00-00' } | + ForEach-Object { "$($_.Name): $($_.MacAddress -replace '-', ':')" } + register: mac_info + ignore_errors: yes + + - name: Merge MAC info into final info + set_fact: + info: "{{ info | combine({'mac_address': mac_info.stdout_lines | default([])}) }}" + - debug: var: info diff --git a/apps/assets/serializers/asset/info/gathered.py b/apps/assets/serializers/asset/info/gathered.py index 6474d08fc..a8ca55fd3 100644 --- a/apps/assets/serializers/asset/info/gathered.py +++ b/apps/assets/serializers/asset/info/gathered.py @@ -18,6 +18,11 @@ class HostGatheredInfoSerializer(serializers.Serializer): arch = serializers.CharField(max_length=16, allow_blank=True, required=False, label=_('OS arch')) gpu_model = serializers.CharField(allow_blank=True, required=False, label=_('GPU model')) + mac_address = serializers.ListField( + child=serializers.CharField(allow_blank=True), + required=False, + label=_('MAC address'), + ) category_gathered_serializer_map = {