Code Monkey home page Code Monkey logo

aini's Introduction

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:

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

Public API

package aini // import "github.com/relex/aini"


FUNCTIONS

func MatchGroups(groups map[string]*Group, pattern string) (map[string]*Group, error)
    MatchGroups looks for groups that match the pattern

func MatchHosts(hosts map[string]*Host, pattern string) (map[string]*Host, error)
    MatchHosts looks for hosts that match the pattern

func MatchVars(vars map[string]string, pattern string) (map[string]string, error)
    MatchVars looks for vars that match the pattern


TYPES

type Group struct {
        Name     string
        Vars     map[string]string
        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 in lexical order

func (group *Group) MatchHosts(pattern string) (map[string]*Host, error)
    MatchHosts looks for hosts that match the pattern

func (group *Group) MatchVars(pattern string) (map[string]string, error)
    MatchVars looks for vars that match the pattern

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 in lexical order

func (host *Host) MatchGroups(pattern string) (map[string]*Group, error)
    MatchGroups looks for groups that match the pattern

func (host *Host) MatchVars(pattern string) (map[string]string, error)
    MatchVars looks for vars that match the pattern

func (host Host) String() string

type InventoryData struct {
        Groups map[string]*Group
        Hosts  map[string]*Host
}
    InventoryData contains parsed inventory representation Note: Groups and
    Hosts fields contain all the groups and hosts, not only top-level

func Parse(r io.Reader) (*InventoryData, error)
    Parse using some Reader

func ParseFile(f string) (*InventoryData, error)
    ParseFile parses Inventory represented as a file

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

func (inventory *InventoryData) HostsToLower()
    HostsToLower transforms all host names to lowercase

func (inventory *InventoryData) Match(pattern string) []*Host
    Match looks for hosts that match the pattern Deprecated: Use `MatchHosts`,
    which does proper error handling

func (inventory *InventoryData) MatchGroups(pattern string) (map[string]*Group, error)
    MatchGroups looks for groups that match the pattern

func (inventory *InventoryData) MatchHosts(pattern string) (map[string]*Host, error)
    MatchHosts looks for hosts that match the pattern

func (inventory *InventoryData) Reconcile()
    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

Usage example

import (
    "strings"
    
    "github.com/relex/aini"
)

func main() {
    // Load from string example
    inventoryReader := strings.NewReader(`
	host1:2221
	[web]
	host2 ansible_ssh_user=root
    `)
    var inventory InventoryData = aini.Parse(inventoryReader)

    // Querying hosts
    _ = inventory.Hosts["host1"].Name == "host1"  // true
    _ = inventory.Hosts["host1"].Port == 2221     // true
    _ = inventory.Hosts["host2"].Name == "host2"] // true
    _ = inventory.Hosts["host2"].Post == 22]      // true
    
    _ = len(inventory.Hosts["host1"].Groups) == 2 // all, ungrouped
    _ = len(inventory.Hosts["host2"].Groups) == 2 // all, web
    
    _ = len(inventory.Match("host*")) == 2        // host1, host2

    _ = // Querying groups
    _ = inventory.Groups["web"].Hosts[0].Name == "host2" // true
    _ = len(inventory.Groups["all"].Hosts) == 2          // true
}

Command-line Tool

go install github.com/relex/aini/cmd/ainidump@latest
ainidump ~/my-playbook/inventory/ansible-hosts myhost*

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
{
    "Hosts": [
        {
            "Name": "myhost1.domain",
            "Groups": [
                "myhosts",
                "companyhosts",
                "india",
                "all"
            ],
            "Vars": {
                "ansible_host": "1.2.3.4",
                "region": "india"
            }
        },
        {
            "Name": "myhost2.domain",
            // ...
        }
    ],
    "Groups": [
        {
            "Name": "companyhosts",
            "Parents": [
                "india",
                "all"
            ],
            "Descendants": [
                "myhosts",
                "otherhosts"
            ],
            "Hosts": [
                "myhost1.domain",
                "myhost2.domain",
                "myhost3.domain"
            ],
            "Vars": {
                "region": "india",
            }
        }
    ]
}

aini's People

Contributors

bemyak avatar jiping-s avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

aini's Issues

How to use your library in go < 1.16?

I'm on a mainstream release that only supports Go 1.15. It seems that your library has a go 1.16 dependency somewhere given the error when trying to get the library installed:

go get github.com/relex/aini
package io/fs: unrecognized import path "io/fs": import path does not begin with hostname

Can you consider making the library compatible to Go versions < 1.16?
(not sure how much you depend on io/fs, but if you could remove the dependency without huge effort would be great?)

Host ranges not working as expected

I stumbled upon your library when working on a little inventory CLI and I think I found an error.

To illustrate I wrote a minimal example:

package main

import (
	"fmt"
	"strings"

	"github.com/relex/aini"
)

func main() {
	// Load from string example
	inventoryReader := strings.NewReader(`foo-[1:2]-[a:c]-bar`)
	inventory, _ := aini.Parse(inventoryReader)

	for host, _ := range inventory.Hosts {
		fmt.Println(host)
	}
}

The output of this when run through go run main.go is as follows:

foo-1-[a:c]-bar
foo-2-[a:c]-bar

I think this is incorrect and should result in this output instead:

foo-1-a-bar
foo-1-b-bar
foo-1-c-bar
foo-2-a-bar
foo-2-b-bar
foo-2-c-bar

"ungrouped" always contains all hosts even though they belong to another group.

Hi,

great library!

For my use case I don't need "all" and "ungrouped" - so its not a major thing for me BUT...

I am iterating over the groups and outputting all group names and their respective hosts, I noticed that ungrouped is output with all hosts. According to the docs at ansible, ungrouped should only contain hosts that don't belong to any other group.

Here is a short sample of my quick and dirty method for demonstrating..

	for _, g := range inventory.Groups {
		_, _ = datawriter.WriteString("[" + g.Name + "]\n")
		for _, h := range g.Hosts {
			_, _ = datawriter.WriteString(h.Name + "\n")

.....

So basically I get a list of groups and their respective hosts, works great! But I also get an "ungrouped" group but it contains ALL hosts

Is this correct? I would have suspected that it should be empty as all my hosts belong to another group.

Thanks in advance

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.