forked from jtopjian/ansible-terraform-inventory
-
Notifications
You must be signed in to change notification settings - Fork 0
/
state.go
182 lines (150 loc) · 4.06 KB
/
state.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
package main
import (
"encoding/json"
"sort"
)
// Interface State represents the methods a state struct has to implement to
// parse a Terraform state.
type State interface {
GetGroups() ([]string, error)
GetGroup(group string) (interface{}, error)
GetGroupsForHost(host string) ([]string, error)
GetChildrenForGroup(group string) ([]string, error)
GetVarsForGroup(group string) (map[string]interface{}, error)
GetVarsForHost(host string) (map[string]interface{}, error)
GetHosts() ([]string, error)
GetHost(host string) (interface{}, error)
GetHostsForGroup(group string) ([]string, error)
}
func BuildInventory(state State) (map[string]interface{}, error) {
inv := make(map[string]interface{})
meta := make(map[string]interface{})
hostvars := make(map[string]interface{})
allHosts := []string{}
// Get all ansible_group resources.
groups, err := state.GetGroups()
if err != nil {
return nil, err
}
// For each ansible_group defined...
for _, group := range groups {
g := make(map[string]interface{})
hosts, err := state.GetHostsForGroup(group)
if err != nil {
return nil, err
}
// Get the children of the group.
children, err := state.GetChildrenForGroup(group)
if err != nil {
return nil, err
}
// Get any variables for the group.
vars, err := state.GetVarsForGroup(group)
if err != nil {
return nil, err
}
// Set the hosts.
if len(hosts) > 0 {
g["hosts"] = hosts
}
// Set the children.
if len(children) > 0 {
g["children"] = children
}
// Set the variables.
g["vars"] = vars
inv[group] = g
}
// Now that we've accounted for all explicitly defined
// groups, let's find any groups which were implicitly
// defined. These are ansible_host resources with group
// memberships of groups that have no explicit
// ansible_group resource.
//
// In addition, create an "ungrouped" group which will
// contain hosts that have no group membership.
var ungrouped []string
// Get all ansible_host resources defined.
hosts, err := state.GetHosts()
if err != nil {
return nil, err
}
// For each host...
for _, host := range hosts {
// Add the host to the set of all hosts.
allHosts = append(allHosts, host)
// Get any variable defined and set it in the inventory.
vars, err := state.GetVarsForHost(host)
if err != nil {
return nil, err
}
hostvars[host] = vars
// Find all groups that the host is a part of.
groups, err := state.GetGroupsForHost(host)
if err != nil {
return nil, err
}
// If no groups were defined, add the host to the "ungrouped" group.
if len(groups) == 0 {
ungrouped = append(ungrouped, host)
}
// For each group defined in the host...
for _, group := range groups {
// Check and see if this group has already been accounted for.
// If it has, check for the host membership.
if v, ok := inv[group]; ok {
groupInventory := v.(map[string]interface{})
if hostInventory, ok := groupInventory["hosts"].([]string); ok {
var found bool
for _, h := range hostInventory {
if h == host {
found = true
}
}
if !found {
groupInventory["hosts"] = append(hostInventory, host)
}
}
} else {
// if the group wasn't already accounted for, do it now.
inv[group] = map[string]interface{}{
"hosts": []string{host},
"vars": map[string]interface{}{},
}
}
}
}
// If there are any "ungrouped" hosts, create an inventory entry
// for "ungrouped".
if len(ungrouped) > 0 {
inv["ungrouped"] = map[string]interface{}{
"hosts": ungrouped,
"vars": map[string]interface{}{},
}
}
// Create an "all" group if one was not defined.
if _, ok := inv["all"]; !ok {
sort.Strings(allHosts)
all := map[string]interface{}{
"hosts": allHosts,
"vars": map[string]interface{}{},
}
inv["all"] = all
}
meta["hostvars"] = hostvars
inv["_meta"] = meta
return inv, nil
}
func ToJSON(state State) (string, error) {
var s string
inv, err := BuildInventory(state)
if err != nil {
return s, err
}
b, err := json.Marshal(inv)
if err != nil {
return s, err
}
s = string(b)
return s, nil
}