项目作者: relex

项目描述 :
Go library for Parsing Ansible inventory files
高级语言: Go
项目地址: git://github.com/relex/aini.git
创建时间: 2020-01-15T13:08:41Z
项目社区:https://github.com/relex/aini

开源协议:MIT License

下载


aini

Go library for Parsing Ansible inventory files.
We are trying to follow the logic of Ansible parser as close as possible.

Documentation on ansible inventory files can be found here:
https://docs.ansible.com/ansible/latest/user_guide/intro_inventory.html

Supported features:

  • [X] Variables
  • [X] Host patterns
  • [X] Nested groups
  • [X] Load variables from group_vars and host_vars

Public API

  1. package aini // import "github.com/relex/aini"
  2. FUNCTIONS
  3. func MatchGroups(groups map[string]*Group, pattern string) (map[string]*Group, error)
  4. MatchGroups looks for groups that match the pattern
  5. func MatchHosts(hosts map[string]*Host, pattern string) (map[string]*Host, error)
  6. MatchHosts looks for hosts that match the pattern
  7. func MatchVars(vars map[string]string, pattern string) (map[string]string, error)
  8. MatchVars looks for vars that match the pattern
  9. TYPES
  10. type Group struct {
  11. Name string
  12. Vars map[string]string
  13. Hosts map[string]*Host
  14. Children map[string]*Group
  15. Parents map[string]*Group
  16. // Has unexported fields.
  17. }
  18. Group represents ansible group
  19. func GroupMapListValues(mymap map[string]*Group) []*Group
  20. GroupMapListValues transforms map of Groups into Group list in lexical order
  21. func (group *Group) MatchHosts(pattern string) (map[string]*Host, error)
  22. MatchHosts looks for hosts that match the pattern
  23. func (group *Group) MatchVars(pattern string) (map[string]string, error)
  24. MatchVars looks for vars that match the pattern
  25. func (group Group) String() string
  26. type Host struct {
  27. Name string
  28. Port int
  29. Vars map[string]string
  30. Groups map[string]*Group
  31. // Has unexported fields.
  32. }
  33. Host represents ansible host
  34. func HostMapListValues(mymap map[string]*Host) []*Host
  35. HostMapListValues transforms map of Hosts into Host list in lexical order
  36. func (host *Host) MatchGroups(pattern string) (map[string]*Group, error)
  37. MatchGroups looks for groups that match the pattern
  38. func (host *Host) MatchVars(pattern string) (map[string]string, error)
  39. MatchVars looks for vars that match the pattern
  40. func (host Host) String() string
  41. type InventoryData struct {
  42. Groups map[string]*Group
  43. Hosts map[string]*Host
  44. }
  45. InventoryData contains parsed inventory representation Note: Groups and
  46. Hosts fields contain all the groups and hosts, not only top-level
  47. func Parse(r io.Reader) (*InventoryData, error)
  48. Parse using some Reader
  49. func ParseFile(f string) (*InventoryData, error)
  50. ParseFile parses Inventory represented as a file
  51. func ParseString(input string) (*InventoryData, error)
  52. ParseString parses Inventory represented as a string
  53. func (inventory *InventoryData) AddVars(path string) error
  54. AddVars take a path that contains group_vars and host_vars directories and
  55. adds these variables to the InventoryData
  56. func (inventory *InventoryData) AddVarsLowerCased(path string) error
  57. AddVarsLowerCased does the same as AddVars, but converts hostnames and
  58. groups name to lowercase. Use this function if you've executed
  59. `inventory.HostsToLower` or `inventory.GroupsToLower`
  60. func (inventory *InventoryData) GroupsToLower()
  61. GroupsToLower transforms all group names to lowercase
  62. func (inventory *InventoryData) HostsToLower()
  63. HostsToLower transforms all host names to lowercase
  64. func (inventory *InventoryData) Match(pattern string) []*Host
  65. Match looks for hosts that match the pattern Deprecated: Use `MatchHosts`,
  66. which does proper error handling
  67. func (inventory *InventoryData) MatchGroups(pattern string) (map[string]*Group, error)
  68. MatchGroups looks for groups that match the pattern
  69. func (inventory *InventoryData) MatchHosts(pattern string) (map[string]*Host, error)
  70. MatchHosts looks for hosts that match the pattern
  71. func (inventory *InventoryData) Reconcile()
  72. Reconcile ensures inventory basic rules, run after updates. After initial
  73. inventory file processing, only direct relationships are set.
  74. This method:
  75. * (re)sets Children and Parents for hosts and groups
  76. * ensures that mandatory groups exist
  77. * calculates variables for hosts and groups

Usage example

  1. import (
  2. "strings"
  3. "github.com/relex/aini"
  4. )
  5. func main() {
  6. // Load from string example
  7. inventoryReader := strings.NewReader(`
  8. host1:2221
  9. [web]
  10. host2 ansible_ssh_user=root
  11. `)
  12. var inventory InventoryData = aini.Parse(inventoryReader)
  13. // Querying hosts
  14. _ = inventory.Hosts["host1"].Name == "host1" // true
  15. _ = inventory.Hosts["host1"].Port == 2221 // true
  16. _ = inventory.Hosts["host2"].Name == "host2"] // true
  17. _ = inventory.Hosts["host2"].Post == 22] // true
  18. _ = len(inventory.Hosts["host1"].Groups) == 2 // all, ungrouped
  19. _ = len(inventory.Hosts["host2"].Groups) == 2 // all, web
  20. _ = len(inventory.Match("host*")) == 2 // host1, host2
  21. _ = // Querying groups
  22. _ = inventory.Groups["web"].Hosts[0].Name == "host2" // true
  23. _ = len(inventory.Groups["all"].Hosts) == 2 // true
  24. }

Command-line Tool

  1. go install github.com/relex/aini/cmd/ainidump@latest

Dump entire inventory

  1. ainidump ~/my-playbook/inventory/ansible-hosts

Host and group variable files in the inventory directory are always loaded. The result is in JSON:

  • Host’s groups and Group’s parents are ordered by level from bottom to top
  • Rest are ordered by names
  1. {
  2. "Hosts": [
  3. {
  4. "Name": "myhost1.domain",
  5. "Groups": [
  6. "myhosts",
  7. "companyhosts",
  8. "india",
  9. "all"
  10. ],
  11. "Vars": {
  12. "ansible_host": "1.2.3.4",
  13. "region": "india"
  14. }
  15. },
  16. {
  17. "Name": "myhost2.domain",
  18. // ...
  19. }
  20. ],
  21. "Groups": [
  22. {
  23. "Name": "companyhosts",
  24. "Parents": [
  25. "india",
  26. "all"
  27. ],
  28. "Descendants": [
  29. "myhosts",
  30. "otherhosts"
  31. ],
  32. "Hosts": [
  33. "myhost1.domain",
  34. "myhost2.domain",
  35. "myhost3.domain"
  36. ],
  37. "Vars": {
  38. "region": "india",
  39. }
  40. }
  41. ]
  42. }

Match hosts by patterns

Find hosts matched by Ansible target patterns, works for both hostnames and group names.

  1. ainidump ~/my-playbook/inventory/ansible-hosts 'recent[1-3]:extrahost*:&eu:!finland'

The result is a dictionary of hosts in the same format above.