parser

package
v0.0.0-...-b5b5a37 Latest Latest
Warning

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

Go to latest
Published: Jan 19, 2026 License: Apache-2.0 Imports: 32 Imported by: 0

Documentation

Index

Constants

View Source
const (
	ModuleIncludeRole  = "include_role"
	ModuleImportRole   = "import_role"
	ModuleIncludeTasks = "include_tasks"
	ModuleImportTasks  = "import_tasks"
)
View Source
const (
	NullTag = "!!null"
	StrTag  = "!!str"
)

Variables

View Source
var ErrModuleNotFound = errors.New("module not found")

Functions

func FindProjects

func FindProjects(fsys fs.FS, root string) ([]string, error)

FindProjects locates Ansible project roots within fsys starting from root. A directory is recognized as a project root if it contains key files or directories like ansible.cfg, inventory, group_vars, host_vars, roles, playbooks, or YAML playbooks.

Returns a slice of project root paths.

Types

type AnsibleConfig

type AnsibleConfig struct {
	Inventory string
}

func LoadConfig

func LoadConfig(fsys fs.FS, dir string) (AnsibleConfig, error)

type AnsibleProject

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

func ParseProject

func ParseProject(fsys fs.FS, root string, opts ...Option) (*AnsibleProject, error)

func (*AnsibleProject) ListTasks

func (p *AnsibleProject) ListTasks() ResolvedTasks

TODO(nikita): some tasks do not contain metadata

func (*AnsibleProject) Path

func (p *AnsibleProject) Path() string

type GalaxyManifest

type GalaxyManifest struct {
	Namespace string `yaml:"namespace"`
	Name      string `yaml:"name"`
}

type Mapping

type Mapping struct {
	Fields *orderedmap.OrderedMap[string, *Node]
}

func (*Mapping) MarshalYAML

func (m *Mapping) MarshalYAML() (any, error)

type Module

type Module struct {
	*Node

	Name string
}

Module represents a logical module in a playbook or task. It wraps a Node and provides module-specific utility methods.

All the data and metadata for the module is stored in the embedded Node.

func (*Module) IsFreeForm

func (m *Module) IsFreeForm() bool

IsFreeForm returns true if the module is a free-form Ansible module. In Ansible, a free-form module is called using a single scalar value instead of a key-value mapping.

Example:

# Free-form
- command: echo "Hello"
  # IsFreeForm() -> true

# Structured
- ansible.builtin.yum:
    name: vim
    state: present
  # IsFreeForm() -> false

type Node

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

func (*Node) AsBool

func (n *Node) AsBool() (bool, bool)

func (*Node) AsString

func (n *Node) AsString() (string, bool)

func (*Node) BoolValue

func (n *Node) BoolValue(path string) iacTypes.BoolValue

func (*Node) IsBool

func (n *Node) IsBool() bool

func (*Node) IsKnown

func (n *Node) IsKnown() bool

func (*Node) IsList

func (n *Node) IsList() bool

func (*Node) IsMap

func (n *Node) IsMap() bool

func (*Node) IsNil

func (n *Node) IsNil() bool

func (*Node) IsString

func (n *Node) IsString() bool

func (*Node) MarshalYAML

func (n *Node) MarshalYAML() (any, error)

func (*Node) Metadata

func (n *Node) Metadata() iacTypes.Metadata

func (*Node) NodeAt

func (n *Node) NodeAt(path string) *Node

func (*Node) Render

func (n *Node) Render(variables vars.Vars) (*Node, error)

func (*Node) StringValue

func (n *Node) StringValue(path string) iacTypes.StringValue

func (*Node) Subtree

func (n *Node) Subtree(r Range) *Node

func (*Node) ToList

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

func (*Node) ToMap

func (n *Node) ToMap() map[string]*Node

func (*Node) UnmarshalYAML

func (n *Node) UnmarshalYAML(node *yaml.Node) error

func (*Node) Value

func (n *Node) Value() any

type NodeValue

type NodeValue interface {
	MarshalYAML() (any, error)
	// contains filtered or unexported methods
}

type Option

type Option func(p *Parser)

func WithExtraVars

func WithExtraVars(v map[string]any) Option

func WithInventories

func WithInventories(inventories ...string) Option

func WithPlaybooks

func WithPlaybooks(playbooks ...string) Option

type Parser

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

func New

func New(fsys fs.FS, root string, opts ...Option) *Parser

func (*Parser) Parse

func (p *Parser) Parse() (*AnsibleProject, error)

type Play

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

Play represents a single play in an Ansible playbook.

An Ansible playbook is a list of such plays, where each play defines settings and tasks for a specific group of hosts.

Example playbook YAML:

  • name: My first play hosts: myhosts tasks:
  • name: Ping my hosts ping:

This play contains a name, target hosts, and a list of tasks.

func (*Play) Hosts

func (p *Play) Hosts() string

func (*Play) UnmarshalYAML

func (p *Play) UnmarshalYAML(node *yaml.Node) error

func (*Play) Variables

func (p *Play) Variables() vars.Vars

type Playbook

type Playbook struct {
	Src   fsutils.FileSource
	Plays []*Play
	Tasks []*Task
}

Playbook represents a sequence of plays in an Ansible playbook.

A playbook is typically loaded from a YAML file and contains a list of plays that are executed in order.

The playbook corresponds to a YAML list, for example:

  • name: First play hosts: all tasks:

  • ...

  • name: Second play hosts: dbservers tasks:

  • ...

type Range

type Range struct {
	Start int
	End   int
}

func (Range) Covers

func (r Range) Covers(other Range) bool

Covers returns true if 'r' fully contains 'other'.

func (Range) Overlaps

func (r Range) Overlaps(o Range) bool

type ResolvedTask

type ResolvedTask struct {
	Name   string
	Fields orderedmap.OrderedMap[string, *Node]
	Vars   vars.Vars

	Metadata iacTypes.Metadata
	Range    Range
}

ResolvedTask represents an Ansible task with all variables resolved.

It holds only the essential data needed for execution and ensures the original Task remains unmodified.

func (*ResolvedTask) GetFieldsByRange

func (t *ResolvedTask) GetFieldsByRange(r Range) map[string]*Node

func (*ResolvedTask) MarshalYAML

func (t *ResolvedTask) MarshalYAML() (any, error)

func (*ResolvedTask) ResolveModule

func (t *ResolvedTask) ResolveModule(keys []string, strict bool) (Module, error)

ResolveModule searches for the first module from given keys in task fields, renders its parameters using task variables, and returns the module. The module can be either structured (map of parameters) or free-form (string). Returns an error if no module is found or if rendering fails.

type ResolvedTasks

type ResolvedTasks []*ResolvedTask

func (ResolvedTasks) FilterByState

func (t ResolvedTasks) FilterByState(exclude ...string) ResolvedTasks

func (ResolvedTasks) GetModules

func (t ResolvedTasks) GetModules(keys ...string) []Module

type Role

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

Role represent project role

type RoleDefinition

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

RoleDefinition represents a role reference within a play.

It typically contains the role name and optional parameters that customize how the role is applied.

Example usage in a playbook:

roles:
  - common
  - role: webserver
    vars:
      http_port: 80

func (*RoleDefinition) UnmarshalYAML

func (r *RoleDefinition) UnmarshalYAML(node *yaml.Node) error

type RoleIncludeModule

type RoleIncludeModule struct {
	Name         string
	TasksFrom    string
	DefaultsFrom string
	VarsFrom     string
}

RoleIncludeModule represents the "include_role" or "import_role" module

type RoleMeta

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

func (*RoleMeta) UnmarshalYAML

func (m *RoleMeta) UnmarshalYAML(node *yaml.Node) error

type Scalar

type Scalar struct {
	Val any
}

func (*Scalar) MarshalYAML

func (s *Scalar) MarshalYAML() (any, error)

type Sequence

type Sequence struct {
	Items []*Node
}

func (*Sequence) MarshalYAML

func (s *Sequence) MarshalYAML() (any, error)

type Task

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

Task represents a single Ansible task.

A task defines a single unit of work, which may include running a module, calling a role, or including other task files.

Tasks can contain parameters, conditions (when), loops, and other Ansible constructs.

Example task:

  • name: Install nginx apt: name: nginx state: present

func (*Task) UnmarshalYAML

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

func (*Task) Variables

func (t *Task) Variables() vars.Vars

Jump to

Keyboard shortcuts

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