ansible

package
v0.0.4-alpha.21 Latest Latest
Warning

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

Go to latest
Published: Feb 5, 2026 License: EUPL-1.2 Imports: 18 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var KnownModules = []string{

	"ansible.builtin.shell",
	"ansible.builtin.command",
	"ansible.builtin.raw",
	"ansible.builtin.script",
	"ansible.builtin.copy",
	"ansible.builtin.template",
	"ansible.builtin.file",
	"ansible.builtin.lineinfile",
	"ansible.builtin.blockinfile",
	"ansible.builtin.stat",
	"ansible.builtin.slurp",
	"ansible.builtin.fetch",
	"ansible.builtin.get_url",
	"ansible.builtin.uri",
	"ansible.builtin.apt",
	"ansible.builtin.apt_key",
	"ansible.builtin.apt_repository",
	"ansible.builtin.yum",
	"ansible.builtin.dnf",
	"ansible.builtin.package",
	"ansible.builtin.pip",
	"ansible.builtin.service",
	"ansible.builtin.systemd",
	"ansible.builtin.user",
	"ansible.builtin.group",
	"ansible.builtin.cron",
	"ansible.builtin.git",
	"ansible.builtin.unarchive",
	"ansible.builtin.archive",
	"ansible.builtin.debug",
	"ansible.builtin.fail",
	"ansible.builtin.assert",
	"ansible.builtin.pause",
	"ansible.builtin.wait_for",
	"ansible.builtin.set_fact",
	"ansible.builtin.include_vars",
	"ansible.builtin.add_host",
	"ansible.builtin.group_by",
	"ansible.builtin.meta",
	"ansible.builtin.setup",

	"shell",
	"command",
	"raw",
	"script",
	"copy",
	"template",
	"file",
	"lineinfile",
	"blockinfile",
	"stat",
	"slurp",
	"fetch",
	"get_url",
	"uri",
	"apt",
	"apt_key",
	"apt_repository",
	"yum",
	"dnf",
	"package",
	"pip",
	"service",
	"systemd",
	"user",
	"group",
	"cron",
	"git",
	"unarchive",
	"archive",
	"debug",
	"fail",
	"assert",
	"pause",
	"wait_for",
	"set_fact",
	"include_vars",
	"add_host",
	"group_by",
	"meta",
	"setup",
}

Known Ansible modules

Functions

func GetHostVars

func GetHostVars(inv *Inventory, hostname string) map[string]any

GetHostVars returns variables for a specific host.

func GetHosts

func GetHosts(inv *Inventory, pattern string) []string

GetHosts returns hosts matching a pattern from inventory.

func NormalizeModule

func NormalizeModule(name string) string

NormalizeModule normalizes a module name to its canonical form.

Types

type Executor

type Executor struct {

	// Callbacks
	OnPlayStart func(play *Play)
	OnTaskStart func(host string, task *Task)
	OnTaskEnd   func(host string, task *Task, result *TaskResult)
	OnPlayEnd   func(play *Play)

	// Options
	Limit     string
	Tags      []string
	SkipTags  []string
	CheckMode bool
	Diff      bool
	Verbose   int
	// contains filtered or unexported fields
}

Executor runs Ansible playbooks.

func NewExecutor

func NewExecutor(basePath string) *Executor

NewExecutor creates a new playbook executor.

func (*Executor) Close

func (e *Executor) Close()

Close closes all SSH connections.

func (*Executor) Run

func (e *Executor) Run(ctx context.Context, playbookPath string) error

Run executes a playbook.

func (*Executor) SetInventory

func (e *Executor) SetInventory(path string) error

SetInventory loads inventory from a file.

func (*Executor) SetInventoryDirect

func (e *Executor) SetInventoryDirect(inv *Inventory)

SetInventoryDirect sets inventory directly.

func (*Executor) SetVar

func (e *Executor) SetVar(key string, value any)

SetVar sets a variable.

func (*Executor) TemplateFile

func (e *Executor) TemplateFile(src, host string, task *Task) (string, error)

TemplateFile processes a template file.

type Facts

type Facts struct {
	Hostname     string `json:"ansible_hostname"`
	FQDN         string `json:"ansible_fqdn"`
	OS           string `json:"ansible_os_family"`
	Distribution string `json:"ansible_distribution"`
	Version      string `json:"ansible_distribution_version"`
	Architecture string `json:"ansible_architecture"`
	Kernel       string `json:"ansible_kernel"`
	Memory       int64  `json:"ansible_memtotal_mb"`
	CPUs         int    `json:"ansible_processor_vcpus"`
	IPv4         string `json:"ansible_default_ipv4_address"`
}

Facts holds gathered facts about a host.

type Host

type Host struct {
	AnsibleHost              string `yaml:"ansible_host,omitempty"`
	AnsiblePort              int    `yaml:"ansible_port,omitempty"`
	AnsibleUser              string `yaml:"ansible_user,omitempty"`
	AnsiblePassword          string `yaml:"ansible_password,omitempty"`
	AnsibleSSHPrivateKeyFile string `yaml:"ansible_ssh_private_key_file,omitempty"`
	AnsibleConnection        string `yaml:"ansible_connection,omitempty"`
	AnsibleBecomePassword    string `yaml:"ansible_become_password,omitempty"`

	// Custom vars
	Vars map[string]any `yaml:",inline"`
}

Host represents a host in inventory.

type Inventory

type Inventory struct {
	All *InventoryGroup `yaml:"all"`
}

Inventory represents Ansible inventory.

type InventoryGroup

type InventoryGroup struct {
	Hosts    map[string]*Host           `yaml:"hosts,omitempty"`
	Children map[string]*InventoryGroup `yaml:"children,omitempty"`
	Vars     map[string]any             `yaml:"vars,omitempty"`
}

InventoryGroup represents a group in inventory.

type LoopControl

type LoopControl struct {
	LoopVar  string `yaml:"loop_var,omitempty"`
	IndexVar string `yaml:"index_var,omitempty"`
	Label    string `yaml:"label,omitempty"`
	Pause    int    `yaml:"pause,omitempty"`
	Extended bool   `yaml:"extended,omitempty"`
}

LoopControl controls loop behavior.

type Parser

type Parser struct {
	// contains filtered or unexported fields
}

Parser handles Ansible YAML parsing.

func NewParser

func NewParser(basePath string) *Parser

NewParser creates a new Ansible parser.

func (*Parser) ParseInventory

func (p *Parser) ParseInventory(path string) (*Inventory, error)

ParseInventory parses an Ansible inventory file.

func (*Parser) ParsePlaybook

func (p *Parser) ParsePlaybook(path string) ([]Play, error)

ParsePlaybook parses an Ansible playbook file.

func (*Parser) ParseRole

func (p *Parser) ParseRole(name string, tasksFrom string) ([]Task, error)

ParseRole parses a role and returns its tasks.

func (*Parser) ParseTasks

func (p *Parser) ParseTasks(path string) ([]Task, error)

ParseTasks parses a tasks file (used by include_tasks).

type Play

type Play struct {
	Name           string            `yaml:"name"`
	Hosts          string            `yaml:"hosts"`
	Connection     string            `yaml:"connection,omitempty"`
	Become         bool              `yaml:"become,omitempty"`
	BecomeUser     string            `yaml:"become_user,omitempty"`
	GatherFacts    *bool             `yaml:"gather_facts,omitempty"`
	Vars           map[string]any    `yaml:"vars,omitempty"`
	PreTasks       []Task            `yaml:"pre_tasks,omitempty"`
	Tasks          []Task            `yaml:"tasks,omitempty"`
	PostTasks      []Task            `yaml:"post_tasks,omitempty"`
	Roles          []RoleRef         `yaml:"roles,omitempty"`
	Handlers       []Task            `yaml:"handlers,omitempty"`
	Tags           []string          `yaml:"tags,omitempty"`
	Environment    map[string]string `yaml:"environment,omitempty"`
	Serial         any               `yaml:"serial,omitempty"` // int or string
	MaxFailPercent int               `yaml:"max_fail_percentage,omitempty"`
}

Play represents a single play in a playbook.

type Playbook

type Playbook struct {
	Plays []Play `yaml:",inline"`
}

Playbook represents an Ansible playbook.

type RoleRef

type RoleRef struct {
	Role      string         `yaml:"role,omitempty"`
	Name      string         `yaml:"name,omitempty"` // Alternative to role
	TasksFrom string         `yaml:"tasks_from,omitempty"`
	Vars      map[string]any `yaml:"vars,omitempty"`
	When      any            `yaml:"when,omitempty"`
	Tags      []string       `yaml:"tags,omitempty"`
}

RoleRef represents a role reference in a play.

func (*RoleRef) UnmarshalYAML

func (r *RoleRef) UnmarshalYAML(unmarshal func(any) error) error

UnmarshalYAML handles both string and struct role refs.

type SSHClient

type SSHClient struct {
	// contains filtered or unexported fields
}

SSHClient handles SSH connections to remote hosts.

func NewSSHClient

func NewSSHClient(cfg SSHConfig) (*SSHClient, error)

NewSSHClient creates a new SSH client.

func (*SSHClient) Close

func (c *SSHClient) Close() error

Close closes the SSH connection.

func (*SSHClient) Connect

func (c *SSHClient) Connect(ctx context.Context) error

Connect establishes the SSH connection.

func (*SSHClient) Download

func (c *SSHClient) Download(ctx context.Context, remote string) ([]byte, error)

Download copies a file from the remote host.

func (*SSHClient) FileExists

func (c *SSHClient) FileExists(ctx context.Context, path string) (bool, error)

FileExists checks if a file exists on the remote host.

func (*SSHClient) Run

func (c *SSHClient) Run(ctx context.Context, cmd string) (stdout, stderr string, exitCode int, err error)

Run executes a command on the remote host.

func (*SSHClient) RunScript

func (c *SSHClient) RunScript(ctx context.Context, script string) (stdout, stderr string, exitCode int, err error)

RunScript runs a script on the remote host.

func (*SSHClient) SetBecome

func (c *SSHClient) SetBecome(become bool, user, password string)

SetBecome enables privilege escalation.

func (*SSHClient) Stat

func (c *SSHClient) Stat(ctx context.Context, path string) (map[string]any, error)

Stat returns file info from the remote host.

func (*SSHClient) Upload

func (c *SSHClient) Upload(ctx context.Context, local io.Reader, remote string, mode os.FileMode) error

Upload copies a file to the remote host.

type SSHConfig

type SSHConfig struct {
	Host       string
	Port       int
	User       string
	Password   string
	KeyFile    string
	Become     bool
	BecomeUser string
	BecomePass string
	Timeout    time.Duration
	Insecure   bool
}

SSHConfig holds SSH connection configuration.

type Task

type Task struct {
	Name         string            `yaml:"name,omitempty"`
	Module       string            `yaml:"-"` // Derived from the module key
	Args         map[string]any    `yaml:"-"` // Module arguments
	Register     string            `yaml:"register,omitempty"`
	When         any               `yaml:"when,omitempty"` // string or []string
	Loop         any               `yaml:"loop,omitempty"` // string or []any
	LoopControl  *LoopControl      `yaml:"loop_control,omitempty"`
	Vars         map[string]any    `yaml:"vars,omitempty"`
	Environment  map[string]string `yaml:"environment,omitempty"`
	ChangedWhen  any               `yaml:"changed_when,omitempty"`
	FailedWhen   any               `yaml:"failed_when,omitempty"`
	IgnoreErrors bool              `yaml:"ignore_errors,omitempty"`
	NoLog        bool              `yaml:"no_log,omitempty"`
	Become       *bool             `yaml:"become,omitempty"`
	BecomeUser   string            `yaml:"become_user,omitempty"`
	Delegate     string            `yaml:"delegate_to,omitempty"`
	RunOnce      bool              `yaml:"run_once,omitempty"`
	Tags         []string          `yaml:"tags,omitempty"`
	Block        []Task            `yaml:"block,omitempty"`
	Rescue       []Task            `yaml:"rescue,omitempty"`
	Always       []Task            `yaml:"always,omitempty"`
	Notify       any               `yaml:"notify,omitempty"` // string or []string
	Retries      int               `yaml:"retries,omitempty"`
	Delay        int               `yaml:"delay,omitempty"`
	Until        string            `yaml:"until,omitempty"`

	// Include/import directives
	IncludeTasks string `yaml:"include_tasks,omitempty"`
	ImportTasks  string `yaml:"import_tasks,omitempty"`
	IncludeRole  *struct {
		Name      string         `yaml:"name"`
		TasksFrom string         `yaml:"tasks_from,omitempty"`
		Vars      map[string]any `yaml:"vars,omitempty"`
	} `yaml:"include_role,omitempty"`
	ImportRole *struct {
		Name      string         `yaml:"name"`
		TasksFrom string         `yaml:"tasks_from,omitempty"`
		Vars      map[string]any `yaml:"vars,omitempty"`
	} `yaml:"import_role,omitempty"`
	// contains filtered or unexported fields
}

Task represents an Ansible task.

func (*Task) UnmarshalYAML

func (t *Task) UnmarshalYAML(node *yaml.Node) error

UnmarshalYAML implements custom YAML unmarshaling for Task.

type TaskResult

type TaskResult struct {
	Changed  bool           `json:"changed"`
	Failed   bool           `json:"failed"`
	Skipped  bool           `json:"skipped"`
	Msg      string         `json:"msg,omitempty"`
	Stdout   string         `json:"stdout,omitempty"`
	Stderr   string         `json:"stderr,omitempty"`
	RC       int            `json:"rc,omitempty"`
	Results  []TaskResult   `json:"results,omitempty"` // For loops
	Data     map[string]any `json:"data,omitempty"`    // Module-specific data
	Duration time.Duration  `json:"duration,omitempty"`
}

TaskResult holds the result of executing a task.

Jump to

Keyboard shortcuts

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