Documentation
¶
Overview ¶
Package vars implements Ansible's variable precedence: a fixed ladder of named layers (role defaults, inventory, facts, play/role/block/task vars, registered vars, role params, extra vars) merged low-to-high so that a value set in a higher layer always wins.
This does not itself know about roles, plays, or tasks — playbook assigns each layer's content as it walks the play; this package only owns the merge order.
Index ¶
- func InjectFacts(facts map[string]any) map[string]any
- type Context
- func (c *Context) Child() *Context
- func (c *Context) Get(key string) (any, bool)
- func (c *Context) Layer(l Layer) map[string]any
- func (c *Context) Merged() map[string]any
- func (c *Context) Set(layer Layer, vals map[string]any)
- func (c *Context) SetVar(layer Layer, key string, value any)
- func (c *Context) Which(key string) (Layer, bool)
- type Layer
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func InjectFacts ¶
InjectFacts returns facts both nested under "ansible_facts" (the canonical form) and flattened as top-level "ansible_<name>" aliases (e.g. ansible_facts["os_family"] also becomes "ansible_os_family"), matching Ansible's fact-injection behavior so both spellings resolve in templates.
Types ¶
type Context ¶
type Context struct {
// contains filtered or unexported fields
}
Context holds one host/task's variable state across every precedence layer. The zero value is not usable — use New.
func (*Context) Child ¶
Child returns a copy of c suitable for a nested scope (a block inside a play, a task inside a block, one iteration of a loop): mutating the child's TaskVars/BlockVars/Registered layers never affects the parent.
func (*Context) Layer ¶ added in v0.1.1
Layer returns a copy of one layer's variables — for a caller (like a role's temporary variable push) that needs to save a layer's current content before overwriting it, to restore afterward.
func (*Context) Merged ¶
Merged flattens every layer into one map, low precedence first so higher layers overwrite lower ones on key conflict.
type Layer ¶
type Layer int
Layer names one rung of Ansible's variable precedence ladder, lowest first. This is a simplified but order-faithful subset of the ~22 levels documented for ansible-core: it keeps every distinction that changes real-world playbook behavior and collapses the rest (e.g. vars_prompt/vars_files fold into PlayVars — they all resolve to the same play-scoped map before a task runs).
const ( RoleDefaults Layer = iota // role defaults/main.yml — the floor Inventory // merged inventory group_vars + host_vars Facts // gathered facts (ansible_facts.*) + flattened ansible_* aliases PlayVars // play vars:, vars_files:, vars_prompt: RoleVars // role vars/main.yml BlockVars // block-level vars: TaskVars // task-level vars:, loop item vars Registered // register:, set_fact RoleParams // role/include_role parameters ExtraVars // -e / --extra-vars — always wins )