Skip to content

Commit

Permalink
Merge pull request #5 from relex/var-dirs-support
Browse files Browse the repository at this point in the history
Support for group_vars and host_vars
  • Loading branch information
bemyak authored Jul 7, 2021
2 parents 4bd1278 + 203039c commit f74a08d
Show file tree
Hide file tree
Showing 22 changed files with 782 additions and 293 deletions.
31 changes: 28 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ https://docs.ansible.com/ansible/latest/user_guide/intro_inventory.html
- [X] Variables
- [X] Host patterns
- [X] Nested groups
- [X] Load variables from `group_vars` and `host_vars`

## Public API
```godoc
Expand All @@ -24,22 +25,30 @@ type Group struct {
Hosts map[string]*Host
Children map[string]*Group
Parents map[string]*Group
// Has unexported fields.
}
Group represents ansible group
func GroupMapListValues(mymap map[string]*Group) []*Group
GroupMapListValues transforms map of Groups into Group list
GroupMapListValues transforms map of Groups into Group list in lexical order
func (group Group) String() string
type Host struct {
Name string
Port int
Vars map[string]string
Groups map[string]*Group
// Has unexported fields.
}
Host represents ansible host
func HostMapListValues(mymap map[string]*Host) []*Host
HostMapListValues transforms map of Hosts into Host list
HostMapListValues transforms map of Hosts into Host list in lexical order
func (host Host) String() string
type InventoryData struct {
Groups map[string]*Group
Expand All @@ -57,6 +66,15 @@ func ParseFile(f string) (*InventoryData, error)
func ParseString(input string) (*InventoryData, error)
ParseString parses Inventory represented as a string
func (inventory *InventoryData) AddVars(path string) error
AddVars take a path that contains group_vars and host_vars directories and
adds these variables to the InventoryData
func (inventory *InventoryData) AddVarsLowerCased(path string) error
AddVarsLowerCased does the same as AddVars, but converts hostnames and
groups name to lowercase Use this function if you've executed
`inventory.HostsToLower` or `inventory.GroupsToLower`
func (inventory *InventoryData) GroupsToLower()
GroupsToLower transforms all group names to lowercase
Expand All @@ -67,7 +85,14 @@ func (inventory *InventoryData) Match(m string) []*Host
Match looks for a hosts that match the pattern
func (inventory *InventoryData) Reconcile()
Reconcile ensures inventory basic rules, run after updates
Reconcile ensures inventory basic rules, run after updates After initial
inventory file processing, only direct relationships are set
This method:
* (re)sets Children and Parents for hosts and groups
* ensures that mandatory groups exist
* calculates variables for hosts and groups
```

Expand Down
38 changes: 35 additions & 3 deletions aini.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"io"
"io/ioutil"
"path"
"sort"
"strings"
)

Expand All @@ -17,13 +18,22 @@ type InventoryData struct {
}

// Group represents ansible group
// Note: Hosts field lists only direct members of the group, members of children groups are not included
type Group struct {
Name string
Vars map[string]string
Hosts map[string]*Host
Children map[string]*Group
Parents map[string]*Group

directParents map[string]*Group
// Vars set in inventory
inventoryVars map[string]string
// Vars set in group_vars
fileVars map[string]string
// Projection of all parent inventory variables
allInventoryVars map[string]string
// Projection of all parent group_vars variables
allFileVars map[string]string
}

// Host represents ansible host
Expand All @@ -32,6 +42,12 @@ type Host struct {
Port int
Vars map[string]string
Groups map[string]*Group

directGroups map[string]*Group
// Vars set in inventory
inventoryVars map[string]string
// Vars set in host_vars
fileVars map[string]string
}

// ParseFile parses Inventory represented as a file
Expand Down Expand Up @@ -72,7 +88,7 @@ func (inventory *InventoryData) Match(m string) []*Host {
return matchedHosts
}

// GroupMapListValues transforms map of Groups into Group list
// GroupMapListValues transforms map of Groups into Group list in lexical order
func GroupMapListValues(mymap map[string]*Group) []*Group {
values := make([]*Group, len(mymap))

Expand All @@ -81,10 +97,13 @@ func GroupMapListValues(mymap map[string]*Group) []*Group {
values[i] = v
i++
}
sort.Slice(values, func(i, j int) bool {
return values[i].Name < values[j].Name
})
return values
}

// HostMapListValues transforms map of Hosts into Host list
// HostMapListValues transforms map of Hosts into Host list in lexical order
func HostMapListValues(mymap map[string]*Host) []*Host {
values := make([]*Host, len(mymap))

Expand All @@ -93,6 +112,9 @@ func HostMapListValues(mymap map[string]*Host) []*Host {
values[i] = v
i++
}
sort.Slice(values, func(i, j int) bool {
return values[i].Name < values[j].Name
})
return values
}

Expand Down Expand Up @@ -120,16 +142,26 @@ func hostMapToLower(hosts map[string]*Host, keysOnly bool) map[string]*Host {
func (inventory *InventoryData) GroupsToLower() {
inventory.Groups = groupMapToLower(inventory.Groups, false)
for _, host := range inventory.Hosts {
host.directGroups = groupMapToLower(host.directGroups, true)
host.Groups = groupMapToLower(host.Groups, true)
}
}

func (group Group) String() string {
return group.Name
}

func (host Host) String() string {
return host.Name
}

func groupMapToLower(groups map[string]*Group, keysOnly bool) map[string]*Group {
newGroups := make(map[string]*Group, len(groups))
for groupname, group := range groups {
groupname = strings.ToLower(groupname)
if !keysOnly {
group.Name = groupname
group.directParents = groupMapToLower(group.directParents, true)
group.Parents = groupMapToLower(group.Parents, true)
group.Children = groupMapToLower(group.Children, true)
}
Expand Down
Loading

0 comments on commit f74a08d

Please sign in to comment.