ansible

package
v0.2.1 Latest Latest
Warning

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

Go to latest
Published: Nov 24, 2021 License: Apache-2.0 Imports: 9 Imported by: 0

README

ansible

Inventory vs group

type Inventory struct {
	items map[string]*Group
	lock  sync.Mutex
}


type Group struct {
	name GroupName

	// for normal group
	Hosts    *[]string               `json:"hosts,omitempty"`
	Vars     *map[string]interface{} `json:"vars,omitempty"`
	Children Inventory               `json:"children,omitempty"`

	// only for "_meta" group
	Hostvars *map[string]map[string]interface{} `json:"hostvars,omitempty"`
}

Inventory Content

{
  "group-a": {
    "hosts": [
      "192.168.28.71",
      "192.168.28.72"
    ],
    "vars": {
      "ansible_ssh_user": "someuser",
      "ansible_ssh_private_key_file": "~/.ssh/mykey",
      "example_variable": "value"
    },
    "children": {
      "group-B": {
        "hosts": [
          "192.168.28.71",
          "192.168.28.72"
        ],
        "vars": {
          "ansible_ssh_user": "someuser",
          "ansible_ssh_private_key_file": "~/.ssh/mykey",
          "example_variable": "value"
        },
        "children": {}
      }
    }
  },
  "this-is-group-name": {
    "hosts": [
      "192.168.28.71",
      "192.168.28.72"
    ],
    "vars": {
      "ansible_ssh_user": "someuser",
      "ansible_ssh_private_key_file": "~/.ssh/mykey",
      "example_variable": "value"
    }
  },
  "_meta": {
    "hostvars": {
      "192.168.28.71": {
        "host_specific_var": "bar"
      },
      "192.168.28.72": {
        "host_specific_var": "foo"
      }
    }
  }
}
  • The above JSON content can be decoded into a Inventory struct, actually the items map of Inventory.
  • The key of items map represents a group name, and the value can be decoded into Group struct.

Documentation

Index

Constants

View Source
const (
	MetaGroupName      string = "_meta"
	AllGroupName       string = "all"
	UngroupedGroupName string = "ungrouped"
)

Variables

This section is empty.

Functions

func PatchAnsibleGroup

func PatchAnsibleGroup(g *Group, ah *ActionHosts)

Types

type ActionHosts

type ActionHosts struct {
	Action string
	Hosts  []string
}

ActionHosts wrapps the Action and Hosts list. Action can be "add", "remove", "update". Hosts is a list of host addresses.

type Group

type Group struct {

	// Hosts store hostvars for each host
	Hosts *map[string]map[string]interface{} `json:"hosts,omitempty" yaml:"hosts,omitempty"`

	// Vars store groupvars for this group
	Vars *map[string]interface{} `json:"vars,omitempty" yaml:"vars,omitempty"`

	Children *Inventory `json:"children,omitempty" yaml:"children,omitempty"`
	// contains filtered or unexported fields
}

func NewGroup

func NewGroup(name string) *Group

NewGroup create a noraml group with name

func (*Group) AddChildGroup

func (g *Group) AddChildGroup(group *Group)

func (*Group) AddChildren

func (g *Group) AddChildren(inventory *Inventory)

AddChildren set the groups in inventory to Children of group.

func (*Group) AddDefaultVars

func (g *Group) AddDefaultVars()

func (*Group) AddHost

func (g *Group) AddHost(host string)

func (*Group) AddHosts

func (g *Group) AddHosts(hosts ...string)

func (*Group) AddVar

func (g *Group) AddVar(key string, value interface{})

func (*Group) AddVars

func (g *Group) AddVars(vars map[string]interface{})

func (*Group) HasHost

func (g *Group) HasHost(host string) bool

func (*Group) HostsList

func (g *Group) HostsList() []string

func (*Group) Name

func (g *Group) Name() string

func (*Group) RemoveHost

func (g *Group) RemoveHost(host string)

func (*Group) RemoveHosts

func (g *Group) RemoveHosts(hosts ...string)

func (*Group) RemoveVar

func (g *Group) RemoveVar(key string)

func (*Group) SetChildren

func (g *Group) SetChildren(inventory *Inventory)

SetChildren set inventory as the Children of group by overriding the Children field

func (*Group) SetDefaultSSHMode

func (g *Group) SetDefaultSSHMode(mode string)

func (*Group) SetDefaultSSHPassword

func (g *Group) SetDefaultSSHPassword(password string)

func (*Group) SetDefaultSSHPort

func (g *Group) SetDefaultSSHPort(port int)

func (*Group) SetDefaultSSHUser

func (g *Group) SetDefaultSSHUser(user string)

func (*Group) SetHostVar

func (m *Group) SetHostVar(host, varName string, varValue interface{})

func (*Group) SetHostVars

func (m *Group) SetHostVars(host string, vars map[string]interface{})

type GroupsMap

type GroupsMap map[string]*Group

type Inventory

type Inventory struct {
	GroupsMap `json:",inline" yaml:",inline"`
	// contains filtered or unexported fields
}

func NewAnsibleInventory

func NewAnsibleInventory() *Inventory

NewAnsibleInventory create a top level inventory. You should call NewInventory to create other level inventory.

func NewInventory

func NewInventory() *Inventory

func (*Inventory) AddAllGroup

func (i *Inventory) AddAllGroup()

AddAllGroup adds a "all" group to the inventory. Only the top level inventory should call this method.

func (*Inventory) AddGroup

func (i *Inventory) AddGroup(group *Group) error

AddGroup add group to inventory, return error if group already exist

func (*Inventory) FillAll

func (i *Inventory) FillAll()

FillAll adds default vars to the 'all' group of the inventory. Only the top level inventory should have 'all' group, so should call this method.

func (*Inventory) FilterOutIP

func (i *Inventory) FilterOutIP(ipnet *net.IPNet)

func (*Inventory) GetAllHosts

func (i *Inventory) GetAllHosts() []string

func (*Inventory) GetGroup

func (i *Inventory) GetGroup(groupName string) (*Group, error)

func (*Inventory) HasAllGroup

func (i *Inventory) HasAllGroup() bool

func (*Inventory) HasGroup

func (i *Inventory) HasGroup(groupName string) bool

func (*Inventory) HasMetaGroup

func (i *Inventory) HasMetaGroup() bool

func (*Inventory) MarshalJSON

func (i *Inventory) MarshalJSON() ([]byte, error)

MarshalJSON provides custom method when json.Marshal(i). 'inline' is not an official option of JSON struct tags.

func (*Inventory) Merge

func (i *Inventory) Merge(new *Inventory) error

func (*Inventory) MergeWithFuncs

func (i *Inventory) MergeWithFuncs(inventoryFinderFuncs ...InventoryFinderFunc) error

func (*Inventory) RemoveGroup

func (i *Inventory) RemoveGroup(groupName string)

SetGroup set group to inventory, override if group already exist

func (*Inventory) SetDefaultSSHPassword

func (i *Inventory) SetDefaultSSHPassword(password string)

func (*Inventory) SetDefaultSSHPort

func (i *Inventory) SetDefaultSSHPort(port int)

func (*Inventory) SetDefaultSSHUser

func (i *Inventory) SetDefaultSSHUser(user string)

func (*Inventory) SetGroup

func (i *Inventory) SetGroup(group *Group)

SetGroup set group to inventory, override if group already exist

func (*Inventory) UnmarshalJSON

func (i *Inventory) UnmarshalJSON(data []byte) error

func (*Inventory) UnmarshalYAML

func (i *Inventory) UnmarshalYAML(value *yaml.Node) error

type InventoryFinderFunc

type InventoryFinderFunc func() (*Inventory, error)

type Module added in v0.1.7

type Module map[string]ModuleArgs

type ModuleArgs added in v0.1.7

type ModuleArgs map[string]interface{}

type Play

type Play struct {
	Name           string    `yaml:"name"`
	Hosts          yaml.Node `yaml:"hosts"`
	AnyErrorsFatal bool      `yaml:"any_errors_fatal"`
	GatherFacts    bool      `yaml:"gather_facts"`
	Become         bool      `yaml:"become"`
	Tasks          []Task    `yaml:"tasks,omitempty"`
	Roles          []Role    `yaml:"roles,omitempty"`
	Tags           []string  `yaml:"tags,omitempty"`
}

func NewPlay

func NewPlay(name string, hostsstr string) *Play

func (*Play) AddRoles added in v0.1.7

func (p *Play) AddRoles(roles ...Role) *Play

func (*Play) AddTags added in v0.1.7

func (p *Play) AddTags(tags ...string) *Play

func (*Play) AddTasks added in v0.1.7

func (p *Play) AddTasks(tasks ...Task) *Play

func (*Play) SetAnyErrorsFatal added in v0.1.7

func (p *Play) SetAnyErrorsFatal(flag bool) *Play

func (*Play) SetGatherFacts added in v0.1.7

func (p *Play) SetGatherFacts(flag bool) *Play

type Playbook

type Playbook []Play

func NewPlaybookFromFile

func NewPlaybookFromFile(file string) (*Playbook, error)

func (*Playbook) PlaysTags

func (p *Playbook) PlaysTags() []string

func (*Playbook) PlaysTagsStartAt

func (p *Playbook) PlaysTagsStartAt(tag string) []string

type Role

type Role struct {
	Role string   `yaml:"role,omitempty"`
	Tags []string `yaml:"tags,omitempty"`
}

func (*Role) WithTags

func (r *Role) WithTags(tags ...string) *Role

type Task added in v0.1.7

type Task struct {
	Name   string `yaml:"name"`
	Module `json:",inline" yaml:",inline"`
}

Jump to

Keyboard shortcuts

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