inventory

package
v0.10.5 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Dec 6, 2021 License: MIT Imports: 19 Imported by: 0

Documentation

Index

Constants

View Source
const (
	// DNS datasource type.
	DNSDatasourceType string = "dns"
)
View Source
const (
	// Etcd datasource type.
	EtcdDatasourceType string = "etcd"
)

Variables

This section is empty.

Functions

This section is empty.

Types

type AnsibleGroup

type AnsibleGroup struct {
	// Group chilren.
	Children []string `json:"children,omitempty"`
	// Hosts belonging to this group.
	Hosts []string `json:"hosts,omitempty"`
}

AnsibleGroup is an Ansible group ready to be marshalled into a JSON representation.

type Config

type Config struct {
	// A logger for the inventory.
	// By default, the global zap.SugaredLogger is used.
	Logger Logger
	// Datasource type.
	// Currently supported: dns, etcd.
	Datasource string `mapstructure:"datasource" default:"dns"`
	// DNS datasource configuration.
	DNS struct {
		// DNS server address.
		Server string `mapstructure:"server" default:"127.0.0.1:53"`
		// Network timeout for DNS requests.
		Timeout time.Duration `mapstructure:"timeout" default:"30s"`
		// DNS zone list.
		Zones []string `mapstructure:"zones" default:"[\"server.local.\"]"`
		// No-transfer mode configuration.
		Notransfer struct {
			// Enable no-transfer data retrieval mode.
			Enabled bool `mapstructure:"enabled" default:"false"`
			// A host whose TXT records contain inventory data.
			Host string `mapstructure:"host" default:"ansible-dns-inventory"`
			// Separator between a hostname and an attribute string in a TXT record.
			Separator string `mapstructure:"separator" default:":"`
		} `mapstructure:"notransfer"`
		// TSIG parameters (used only with zone transfer requests).
		Tsig struct {
			// Enable TSIG.
			Enabled bool `mapstructure:"enabled" default:"false"`
			// TSIG key name.
			Key string `mapstructure:"key" default:"axfr."`
			// TSIG secret (base64-encoded).
			Secret string `mapstructure:"secret" default:"c2VjcmV0Cg=="`
			// TSIG algorithm.
			// Allowed values: 'hmac-sha1', hmac-sha224, 'hmac-sha256', 'hmac-sha384', 'hmac-sha512'. 'hmac-sha256' is used if an invalid value is specified.
			Algo string `mapstructure:"algo" default:"hmac-sha256."`
		} `mapstructure:"tsig"`
	} `mapstructure:"dns"`
	// Etcd datasource configuration.
	Etcd struct {
		// Etcd cluster endpoints.
		Endpoints []string `mapstructure:"endpoints" default:"[\"127.0.0.1:2379\"]"`
		// Network timeout for etcd requests.
		Timeout time.Duration `mapstructure:"timeout" default:"30s"`
		// Etcd k/v path prefix.
		Prefix string `mapstructure:"prefix" default:"ANSIBLE_INVENTORY"`
		// Etcd host zone list.
		Zones []string `mapstructure:"zones" default:"[\"server.local.\"]"`
		// Etcd authentication configuration.
		Auth struct {
			// Username for authentication.
			Username string `mapstructure:"username" default:""`
			// Password for authentication.
			Password string `mapstructure:"password" default:""`
		} `mapstructure:"auth"`
		// Etcd TLS configuration.
		TLS struct {
			// Enable TLS.
			Enabled bool `mapstructure:"enabled" default:"true"`
			// Skip verification of the etcd server's certificate chain and host name.
			Insecure bool `mapstructure:"insecure" default:"false"`
			// Trusted CA bundle.
			CA struct {
				Path string `mapstructure:"path" default:""`
				PEM  string `mapstructure:"pem" default:""`
			} `mapstructure:"ca"`
			// User certificate.
			Certificate struct {
				Path string `mapstructure:"path" default:""`
				PEM  string `mapstructure:"pem" default:""`
			} `mapstructure:"certificate"`
			// User private key.
			Key struct {
				Path string `mapstructure:"path" default:""`
				PEM  string `mapstructure:"pem" default:""`
			} `mapstructure:"key"`
		} `mapstructure:"tls"`
	} `mapstructure:"etcd"`
	// Host records parsing configuration.
	Txt struct {
		// Key/value pair parsing configuration.
		Kv struct {
			// Separator between k/v pairs found in TXT records.
			Separator string `mapstructure:"separator" default:";"`
			// Separator between a key and a value.
			Equalsign string `mapstructure:"equalsign" default:"="`
		} `mapstructure:"kv"`
		// Host variables parsing configuration.
		Vars struct {
			// Enable host variables support.
			Enabled bool `mapstructure:"enabled" default:"false"`
			// Separator between k/v pairs found in the host variables attribute.
			Separator string `mapstructure:"separator" default:","`
			// Separator between a key and a value.
			Equalsign string `mapstructure:"equalsign" default:"="`
		} `mapstructure:"vars"`
		// Host attributes parsing configuration.
		Keys struct {
			// Separator between elements of an Ansible group name.
			Separator string `mapstructure:"separator" default:"_"`
			// Key name of the attribute containing the host operating system identifier.
			Os string `mapstructure:"os" default:"OS"`
			// Key name of the attribute containing the host environment identifier.
			Env string `mapstructure:"env" default:"ENV"`
			// Key name of the attribute containing the host role identifier.
			Role string `mapstructure:"role" default:"ROLE"`
			// Key name of the attribute containing the host service identifier.
			Srv string `mapstructure:"srv" default:"SRV"`
			// Key name of the attribute containing the host variables.
			Vars string `mapstructure:"vars" default:"VARS"`
		} `mapstructure:"keys"`
	} `mapstructure:"txt"`
}

Config represents the main inventory configuration.

type DNSDatasource

type DNSDatasource struct {
	// Inventory configuration.
	Config *Config
	// Inventory logger.
	Logger Logger
	// DNS client.
	Client *dns.Client
	// DNS zone transfer parameters.
	Transfer *dns.Transfer
}

DNSDatasource implements a DNS datasource.

func NewDNSDatasource

func NewDNSDatasource(cfg *Config) (*DNSDatasource, error)

NewDNSDatasource creates a DNS datasource.

func (*DNSDatasource) Close

func (d *DNSDatasource) Close()

Close shuts down the datasource and performs other housekeeping.

func (*DNSDatasource) GetAllRecords

func (d *DNSDatasource) GetAllRecords() ([]*DatasourceRecord, error)

GetAllRecords acquires all available host records.

func (*DNSDatasource) GetHostRecords

func (d *DNSDatasource) GetHostRecords(host string) ([]*DatasourceRecord, error)

GetHostRecords acquires all available records for a specific host.

type Datasource

type Datasource interface {
	// GetAllRecords returns all host records.
	GetAllRecords() ([]*DatasourceRecord, error)
	// GetHostRecords returns all records for a specific host.
	GetHostRecords(host string) ([]*DatasourceRecord, error)
	// Close closes datasource clients and performs other housekeeping.
	Close()
}

Datasource provides an interface for all supported datasources.

func NewDatasource

func NewDatasource(cfg *Config) (Datasource, error)

NewDatasource creates a datasource based on the inventory configuration.

type DatasourceRecord

type DatasourceRecord struct {
	// Host name.
	Hostname string
	// Host attributes.
	Attributes string
}

DatasourceRecord represents a single host record returned by a datasource.

type EtcdDatasource

type EtcdDatasource struct {
	// Inventory configuration.
	Config *Config
	// Inventory logger.
	Logger Logger
	// Etcd client.
	Client *etcdv3.Client
}

EtcdDatasource implements an etcd datasource.

func NewEtcdDatasource

func NewEtcdDatasource(cfg *Config) (*EtcdDatasource, error)

NewEtcdDatasource creates an etcd datasource.

func (*EtcdDatasource) Close

func (e *EtcdDatasource) Close()

Close shuts down the datasource and performs other housekeeping.

func (*EtcdDatasource) GetAllRecords

func (e *EtcdDatasource) GetAllRecords() ([]*DatasourceRecord, error)

GetAllRecords acquires all available host records.

func (*EtcdDatasource) GetHostRecords

func (e *EtcdDatasource) GetHostRecords(host string) ([]*DatasourceRecord, error)

GetHostRecords acquires all available records for a specific host.

type ExportNode

type ExportNode struct {
	// Group name.
	Name string `json:"name" yaml:"name"`
	// Group children.
	Children []*Node `json:"children" yaml:"children"`
	// Hosts belonging to this group.
	Hosts []string `json:"hosts" yaml:"hosts"`
}

ExportNode represents an inventory tree node for the tree export mode.

type HostAttributes

type HostAttributes struct {
	// Host operating system identifier.
	OS string `validate:"required,notblank,alphanum"`
	// Host environment identifier.
	Env string `validate:"required,notblank,alphanum"`
	// Host role identifier.
	Role string `validate:"required,notblank,safelist"`
	// Host service identifier.
	Srv string `validate:"safelistsep"`
	// Host variables
	Vars string `validate:"printascii"`
}

HostAttributes represents host attributes found in TXT records.

func (*HostAttributes) MarshalJSON

func (a *HostAttributes) MarshalJSON() ([]byte, error)

MarshalJSON implements a custom JSON Marshaller for host attributes.

func (*HostAttributes) MarshalYAML

func (a *HostAttributes) MarshalYAML() (interface{}, error)

MarshalYAML implements a custom YAML Marshaller for host attributes.

type Inventory

type Inventory struct {
	// Inventory configuration.
	Config *Config
	// Inventory logger.
	Logger Logger
	// Inventory validator.
	Validator *validator.Validate
	// Inventory datasource.
	Datasource Datasource
	// Inventory tree.
	Tree *Node
}

Inventory implements a dynamic inventory for Ansible.

func New

func New(cfg *Config) (*Inventory, error)

New creates an instance of the DNS inventory with user-supplied configuration.

func NewDefault added in v0.10.3

func NewDefault() (*Inventory, error)

NewDefault creates an instance of the DNS inventory with the default configuration.

func (*Inventory) ExportGroups

func (i *Inventory) ExportGroups(groups map[string][]string)

ExportGroups exports the inventory tree into a map of groups and hosts they contain.

func (*Inventory) ExportHosts

func (i *Inventory) ExportHosts(hosts map[string][]string)

ExportHosts exports the inventory tree into a map of hosts and groups they belong to.

func (*Inventory) ExportInventory

func (i *Inventory) ExportInventory(inventory map[string]*AnsibleGroup)

ExportInventory exports the inventory tree into a map ready to be marshalled into a JSON representation of a dynamic Ansible inventory.

func (*Inventory) GetHostVariables

func (i *Inventory) GetHostVariables(host string) (map[string]string, error)

GetHostVariables acquires a map of host variables specified via the 'VARS' attribute.

func (*Inventory) GetHosts

func (i *Inventory) GetHosts() (map[string][]*HostAttributes, error)

GetHosts acquires a map of all hosts and their attributes.

func (*Inventory) ImportHosts

func (i *Inventory) ImportHosts(hosts map[string][]*HostAttributes)

ImportHosts loads a map of hosts and their attributes into the inventory tree.

func (*Inventory) ParseAttributes

func (i *Inventory) ParseAttributes(raw string) (*HostAttributes, error)

ParseAttributes parses host attributes.

type Logger

type Logger interface {
	Info(args ...interface{})
	Infof(template string, args ...interface{})
	Warn(args ...interface{})
	Warnf(template string, args ...interface{})
	Error(args ...interface{})
	Errorf(template string, args ...interface{})
	Fatal(args ...interface{})
	Fatalf(template string, args ...interface{})
	Debug(args ...interface{})
	Debugf(template string, args ...interface{})
}

Logger provides a logging interface for the inventory and its datasources.

type Node

type Node struct {
	// Group name.
	Name string
	// Group Parent
	Parent *Node `json:"-" yaml:"-"`
	// Group children.
	Children []*Node
	// Hosts belonging to this group.
	Hosts map[string]bool
}

Node represents and inventory tree node.

func NewTree

func NewTree() *Node

NewTree initializes an empty inventory tree

func (*Node) AddChild

func (n *Node) AddChild(name string) *Node

AddChild adds a child to this node if it doesn't exist and return a pointer to the child.

func (*Node) AddHost

func (n *Node) AddHost(host string)

AddHost adds a host to this node.

func (*Node) ExportGroups

func (n *Node) ExportGroups(groups map[string][]string)

ExportGroups exports the inventory tree into a map of groups and hosts they contain, starting from this node.

func (*Node) ExportHosts

func (n *Node) ExportHosts(hosts map[string][]string)

ExportHosts exports the inventory tree into a map of hosts and groups they belong to, starting from this node.

func (*Node) ExportInventory

func (n *Node) ExportInventory(inventory map[string]*AnsibleGroup)

ExportInventory exports the inventory tree into a map ready to be marshalled into a JSON representation of an Ansible inventory, starting from this node.

func (*Node) GetAllHosts

func (n *Node) GetAllHosts() map[string]bool

GetAllHosts returns all hosts from descendant groups, starting from this node.

func (*Node) GetAncestors

func (n *Node) GetAncestors() []*Node

GetAncestors returns all ancestor nodes, starting from this node.

func (*Node) ImportHosts

func (n *Node) ImportHosts(hosts map[string][]*HostAttributes, sep string)

ImportHosts loads a map of hosts and their attributes into the inventory tree, using this node as root.

func (*Node) MarshalJSON

func (n *Node) MarshalJSON() ([]byte, error)

MarshalJSON implements a custom JSON Marshaller for tree nodes.

func (*Node) MarshalYAML

func (n *Node) MarshalYAML() (interface{}, error)

MarshalYAML implements a custom YAML Marshaller for tree nodes.

func (*Node) SortChildren

func (n *Node) SortChildren()

SortChildren sorts children by name recursively, starting from this node.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL