BuildInventoryWithFormat renders an inventory in the requested format.
(nodes []*api.Node, guests []*api.VM, defaults InventoryDefaults, format string)
| 76 | |
| 77 | // BuildInventoryWithFormat renders an inventory in the requested format. |
| 78 | func BuildInventoryWithFormat(nodes []*api.Node, guests []*api.VM, defaults InventoryDefaults, format string) InventoryResult { |
| 79 | format = NormalizeInventoryFormat(format) |
| 80 | style := NormalizeInventoryStyle(defaults.Style) |
| 81 | |
| 82 | hosts := make([]InventoryHost, 0, len(nodes)+len(guests)) |
| 83 | groups := make(map[string][]InventoryHost) |
| 84 | |
| 85 | pushHost := func(host InventoryHost) { |
| 86 | hosts = append(hosts, host) |
| 87 | for _, group := range host.GroupNames { |
| 88 | groups[group] = append(groups[group], host) |
| 89 | } |
| 90 | } |
| 91 | |
| 92 | for _, node := range nodes { |
| 93 | if node == nil { |
| 94 | continue |
| 95 | } |
| 96 | |
| 97 | target := chooseHostTarget(node.IP, node.Name) |
| 98 | if target == "" { |
| 99 | continue |
| 100 | } |
| 101 | |
| 102 | alias := uniqueAlias(hosts, "node_"+sanitizeIdentifier(node.Name)) |
| 103 | vars := map[string]string{ |
| 104 | "ansible_host": target, |
| 105 | "ansible_user": defaults.NodeSSHUser, |
| 106 | "pvetui_kind": "node", |
| 107 | "pvetui_node": node.Name, |
| 108 | "pvetui_online": fmt.Sprintf("%t", node.Online), |
| 109 | } |
| 110 | if node.SourceProfile != "" { |
| 111 | vars["pvetui_source_profile"] = node.SourceProfile |
| 112 | } |
| 113 | if strings.TrimSpace(defaults.SSHPrivateKeyFile) != "" { |
| 114 | vars["ansible_ssh_private_key_file"] = strings.TrimSpace(defaults.SSHPrivateKeyFile) |
| 115 | } |
| 116 | if strings.TrimSpace(defaults.DefaultPassword) != "" { |
| 117 | vars["ansible_password"] = strings.TrimSpace(defaults.DefaultPassword) |
| 118 | } |
| 119 | mergeInventoryVars(vars, defaults.InventoryVars) |
| 120 | |
| 121 | groupNames := []string{"proxmox_nodes"} |
| 122 | if node.Online { |
| 123 | groupNames = append(groupNames, "running") |
| 124 | } else { |
| 125 | groupNames = append(groupNames, "stopped") |
| 126 | } |
| 127 | |
| 128 | pushHost(InventoryHost{ |
| 129 | Alias: alias, |
| 130 | Target: target, |
| 131 | GroupNames: groupNames, |
| 132 | Vars: vars, |
| 133 | }) |
| 134 | } |
| 135 |