diff --git a/ansible-deploy b/ansible-deploy index c1a286c..5015d36 100755 Binary files a/ansible-deploy and b/ansible-deploy differ diff --git a/internal/services/ansible.go b/internal/services/ansible.go index b422d3a..2fa937b 100644 --- a/internal/services/ansible.go +++ b/internal/services/ansible.go @@ -47,10 +47,30 @@ func NewAnsibleService(cfg *Config) *AnsibleService { // 加载现有数据 svc.loadHosts() + svc.loadGroups() return svc } +// loadGroups 加载主机组列表 +func (s *AnsibleService) loadGroups() { + groupsFile := filepath.Join(s.config.InventoryDir, "groups.json") + data, err := os.ReadFile(groupsFile) + if err != nil { + return + } + + var groups map[string]models.HostGroup + if err := json.Unmarshal(data, &groups); err == nil { + for name, g := range groups { + if name != "all" && name != "ungrouped" { + gcopy := g + s.groups[name] = &gcopy + } + } + } +} + // generateID 生成唯一ID func (s *AnsibleService) generateID() string { hash := md5.New() @@ -121,22 +141,22 @@ func (s *AnsibleService) loadHosts() { var hosts []models.Host if err := json.Unmarshal(data, &hosts); err == nil { - for _, h := range hosts { - hcopy := h - if hcopy.ID == "" { - hcopy.ID = s.generateID() - } - if hcopy.Port == 0 { - hcopy.Port = 22 - } - if hcopy.Username == "" { - hcopy.Username = "root" - } - if hcopy.Status == "" { - hcopy.Status = "pending" - } - s.hosts[hcopy.ID] = &hcopy + for _, h := range hosts { + host := h // 避免循环变量指针问题 + if host.ID == "" { + host.ID = s.generateID() } + if host.Port == 0 { + host.Port = 22 + } + if host.Username == "" { + host.Username = "root" + } + if host.Status == "" { + host.Status = "pending" + } + s.hosts[host.ID] = &host + } // 保存以持久化补全的字段 s.saveHosts() } @@ -147,7 +167,13 @@ func (s *AnsibleService) saveHosts() error { hostsFile := filepath.Join(s.config.InventoryDir, "hosts.json") var hosts []models.Host for _, h := range s.hosts { - hosts = append(hosts, *h) + hcopy := *h + // 确保每个主机都有ID,并更新map中的指针 + if hcopy.ID == "" { + hcopy.ID = s.generateID() + h.ID = hcopy.ID // 更新map中的指针 + } + hosts = append(hosts, hcopy) } data, _ := json.MarshalIndent(hosts, "", " ") @@ -246,15 +272,21 @@ func (s *AnsibleService) ListGroups() []models.HostGroup { var groups []models.HostGroup for _, g := range s.groups { gcopy := *g - // 展开组内主机的详细信息 + // 动态展开组内主机(通过 host.Groups 字段关联,而非 group.Hosts) var hostList []models.Host - for _, hName := range g.Hosts { - for _, h := range s.hosts { - if h.Name == hName { - hostList = append(hostList, *h) + for _, h := range s.hosts { + for _, hGroup := range h.Groups { + if hGroup == gcopy.Name { + hcopy := *h + hostList = append(hostList, hcopy) break } } + // 也检查主机的默认组(all/ungrouped) + if len(h.Groups) == 0 && gcopy.Name == "ungrouped" { + hcopy := *h + hostList = append(hostList, hcopy) + } } gcopy.HostList = hostList groups = append(groups, gcopy) diff --git a/inventory/hosts b/inventory/hosts index 7c7854b..0a0b8cf 100644 --- a/inventory/hosts +++ b/inventory/hosts @@ -1,11 +1,10 @@ # Ansible Inventory File # Generated by ansible-deploy -[ungrouped] - scmp47 ansible_host=172.16.11.44 ansible_user=root - [all] nas ansible_host=10.168.1.209 ansible_user=root [zebu_user01] + scmp47 ansible_host=172.16.11.44 ansible_user=root + scmp48 ansible_host=172.16.11.46 ansible_user=root scmp46 ansible_host=172.16.11.42 ansible_user=root diff --git a/inventory/hosts.json b/inventory/hosts.json index f08f294..79c6ec2 100644 --- a/inventory/hosts.json +++ b/inventory/hosts.json @@ -1,31 +1,4 @@ [ - { - "id": "c3e976cb", - "name": "scmp46", - "ip": "172.16.11.42", - "port": 22, - "username": "root", - "password": "STC#scmp%0818", - "groups": [ - "zebu_user01" - ], - "status": "online", - "last_check": "2026-05-13T17:43:47.863884597+08:00", - "created_at": "2026-05-13T16:19:53.979745105+08:00", - "updated_at": "2026-05-13T16:19:53.979745185+08:00" - }, - { - "id": "5ef41aa3", - "name": "scmp47", - "ip": "172.16.11.44", - "port": 22, - "username": "root", - "groups": [], - "status": "pending", - "last_check": "0001-01-01T00:00:00Z", - "created_at": "0001-01-01T00:00:00Z", - "updated_at": "2026-05-13T17:40:47.833904109+08:00" - }, { "id": "706f8ce7", "name": "nas", @@ -40,5 +13,50 @@ "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": "4d7a1f03", + "name": "scmp48", + "ip": "172.16.11.46", + "port": 22, + "username": "root", + "password": "STC#scmp%0818", + "groups": [ + "zebu_user01" + ], + "status": "online", + "last_check": "2026-05-13T18:27:19.272543838+08:00", + "created_at": "0001-01-01T00:00:00Z", + "updated_at": "2026-05-13T18:23:27.433461112+08:00" + }, + { + "id": "57884720", + "name": "scmp46", + "ip": "172.16.11.42", + "port": 22, + "username": "root", + "password": "STC#scmp%0818", + "groups": [ + "zebu_user01" + ], + "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" + }, + { + "id": "5150c740", + "name": "scmp47", + "ip": "172.16.11.44", + "port": 22, + "username": "root", + "password": "STC#scmp%0818", + "groups": [ + "zebu_user01" + ], + "status": "online", + "last_check": "2026-05-13T18:30:33.343216547+08:00", + "created_at": "0001-01-01T00:00:00Z", + "updated_at": "2026-05-13T18:28:08.656450224+08:00" } ] \ No newline at end of file diff --git a/web/dist/index.html b/web/dist/index.html index 65e206a..d0dc92e 100644 --- a/web/dist/index.html +++ b/web/dist/index.html @@ -389,6 +389,7 @@