Browse Source

fix: all组显示所有主机 + playbook warn模块改为debug

Hermes Agent 6 days ago
parent
commit
ecb8afd732
4 changed files with 35 additions and 29 deletions
  1. BIN
      ansible-deploy
  2. 7 1
      internal/services/ansible.go
  3. 26 26
      inventory/hosts.json
  4. 2 2
      playbooks/check-cpu-mem-disk.yml

BIN
ansible-deploy


+ 7 - 1
internal/services/ansible.go

@@ -275,6 +275,12 @@ func (s *AnsibleService) ListGroups() []models.HostGroup {
 		// 动态展开组内主机(通过 host.Groups 字段关联,而非 group.Hosts)
 		var hostList []models.Host
 		for _, h := range s.hosts {
+			if gcopy.Name == "all" {
+				// all 组包含所有主机
+				hcopy := *h
+				hostList = append(hostList, hcopy)
+				continue
+			}
 			for _, hGroup := range h.Groups {
 				if hGroup == gcopy.Name {
 					hcopy := *h
@@ -282,7 +288,7 @@ func (s *AnsibleService) ListGroups() []models.HostGroup {
 					break
 				}
 			}
-			// 也检查主机的默认组(all/ungrouped)
+			// 也检查主机的默认组(ungrouped)
 			if len(h.Groups) == 0 && gcopy.Name == "ungrouped" {
 				hcopy := *h
 				hostList = append(hostList, hcopy)

+ 26 - 26
inventory/hosts.json

@@ -1,23 +1,23 @@
 [
   {
-    "id": "57884720",
-    "name": "scmp46",
-    "ip": "172.16.11.42",
+    "id": "706f8ce7",
+    "name": "nas",
+    "ip": "10.168.1.209",
     "port": 22,
     "username": "root",
-    "password": "STC#scmp%0818",
+    "password": "WXJwxj91612!!",
     "groups": [
-      "zebu_user01"
+      "all"
     ],
     "status": "online",
-    "last_check": "2026-05-13T18:30:38.103119534+08:00",
-    "created_at": "0001-01-01T00:00:00Z",
-    "updated_at": "2026-05-13T18:28:16.71131472+08:00"
+    "last_check": "2026-05-13T17:34:10.808052527+08:00",
+    "created_at": "2026-05-13T16:03:45.265250935+08:00",
+    "updated_at": "2026-05-13T16:03:45.265251013+08:00"
   },
   {
-    "id": "5150c740",
-    "name": "scmp47",
-    "ip": "172.16.11.44",
+    "id": "4d7a1f03",
+    "name": "scmp48",
+    "ip": "172.16.11.46",
     "port": 22,
     "username": "root",
     "password": "STC#scmp%0818",
@@ -25,29 +25,29 @@
       "zebu_user01"
     ],
     "status": "online",
-    "last_check": "2026-05-13T18:30:33.343216547+08:00",
+    "last_check": "2026-05-13T18:27:19.272543838+08:00",
     "created_at": "0001-01-01T00:00:00Z",
-    "updated_at": "2026-05-13T18:28:08.656450224+08:00"
+    "updated_at": "2026-05-13T18:23:27.433461112+08:00"
   },
   {
-    "id": "706f8ce7",
-    "name": "nas",
-    "ip": "10.168.1.209",
+    "id": "57884720",
+    "name": "scmp46",
+    "ip": "172.16.11.42",
     "port": 22,
     "username": "root",
-    "password": "WXJwxj91612!!",
+    "password": "STC#scmp%0818",
     "groups": [
-      "all"
+      "zebu_user01"
     ],
     "status": "online",
-    "last_check": "2026-05-13T17:34:10.808052527+08:00",
-    "created_at": "2026-05-13T16:03:45.265250935+08:00",
-    "updated_at": "2026-05-13T16:03:45.265251013+08:00"
+    "last_check": "2026-05-13T18:30:38.103119534+08:00",
+    "created_at": "0001-01-01T00:00:00Z",
+    "updated_at": "2026-05-13T18:28:16.71131472+08:00"
   },
   {
-    "id": "4d7a1f03",
-    "name": "scmp48",
-    "ip": "172.16.11.46",
+    "id": "5150c740",
+    "name": "scmp47",
+    "ip": "172.16.11.44",
     "port": 22,
     "username": "root",
     "password": "STC#scmp%0818",
@@ -55,8 +55,8 @@
       "zebu_user01"
     ],
     "status": "online",
-    "last_check": "2026-05-13T18:27:19.272543838+08:00",
+    "last_check": "2026-05-13T18:30:33.343216547+08:00",
     "created_at": "0001-01-01T00:00:00Z",
-    "updated_at": "2026-05-13T18:23:27.433461112+08:00"
+    "updated_at": "2026-05-13T18:28:08.656450224+08:00"
   }
 ]

+ 2 - 2
playbooks/check-cpu-mem-disk.yml

@@ -48,6 +48,6 @@
       when: cpu_usage | int >= crit_threshold
 
     - name: 警告判断
-      warn:
-        msg: "{{ inventory_hostname }} CPU 使用率超过 {{ warn_threshold }}%: {{ cpu_usage }}%"
+      debug:
+        msg: "⚠️ WARNING: {{ inventory_hostname }} CPU 使用率超过 {{ warn_threshold }}%: {{ cpu_usage }}%"
       when: cpu_usage | int >= warn_threshold and cpu_usage | int < crit_threshold