playbook

package
v0.14.0 Latest Latest
Warning

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

Go to latest
Published: Oct 13, 2026 License: MIT Imports: 22 Imported by: 1 module, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Index

Constants

This section is empty.

Variables

View Source
var ErrNoTargets = errors.New("no target nodes resolved")

ErrNoTargets 表示未解析到任何可执行的目标节点

Functions

This section is empty.

Types

type CopySpec

type CopySpec struct {
	Src  string `yaml:"src"`
	Dest string `yaml:"dest"`
	// Mode 文件权限,如 "0644",为空则保持源文件权限
	Mode string `yaml:"mode,omitempty"`
}

CopySpec 定义文件传输或模板渲染规格

type Duration

type Duration struct {
	time.Duration
}

Duration 是 time.Duration 的 YAML 友好包装,支持 "30s", "5m", "1h" 等格式

func (Duration) MarshalYAML

func (d Duration) MarshalYAML() (any, error)

func (*Duration) UnmarshalYAML

func (d *Duration) UnmarshalYAML(unmarshal func(any) error) error

type Engine

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

Engine 是 Playbook 的执行引擎

func NewEngine

func NewEngine(pb *Playbook, provider config.ConfigProvider, connector *ssh.Connector, opts ...EngineOption) *Engine

NewEngine 创建一个执行引擎实例,支持通过 EngineOption 注入不可变组件。

func (*Engine) Run

func (e *Engine) Run(ctx context.Context) (*Report, error)

Run 执行 Playbook,返回完整的执行报告。

type EngineOption added in v0.12.0

type EngineOption func(*Engine)

EngineOption 用于配置 Engine

func WithEventListener added in v0.12.0

func WithEventListener(l EventListener) EngineOption

WithEventListener 为 Engine 注入事件监听器(必须支持并发调用)

type EnsureSpec

type EnsureSpec struct {
	// Check 检查命令,退出码 0 表示已满足期望状态
	Check string `yaml:"check"`
	// Action 当 Check 未通过时执行的修复命令
	Action string `yaml:"action"`
}

EnsureSpec 定义幂等性检查规格(状态收敛核心)

type EventListener added in v0.12.0

type EventListener interface {
	OnTargetsResolved(count int)
	OnTagEmpty(tag string)
	OnStepRunning(host, stepName string)
	OnStepResult(host, stepName string, r StepResult)
}

EventListener 定义 Playbook 执行过程中的事件通知接口

var NopEventListener EventListener = nopEventListener{}

NopEventListener 是默认的空事件监听器

type HostReport

type HostReport struct {
	NodeID   string
	Host     string
	Steps    []StepResult
	Status   HostStatus
	Duration time.Duration
}

HostReport 是单台主机的完整执行报告

func (*HostReport) Summary

func (h *HostReport) Summary() (ok, changed, skipped, failed int)

Summary 返回该主机各状态的计数摘要

type HostStatus

type HostStatus string

HostStatus 表示单台主机的整体执行状态

const (
	HostStatusSuccess HostStatus = "success"
	HostStatusFailed  HostStatus = "failed"
	HostStatusAborted HostStatus = "aborted"
)

type OnError

type OnError string

OnError 定义步骤失败时的处理策略

const (
	// OnErrorContinue 忽略当前主机的错误,继续执行该主机的后续步骤
	OnErrorContinue OnError = "continue"
	// OnErrorStop 当前主机停止后续步骤,其他主机不受影响(默认)
	OnErrorStop OnError = "stop"
	// OnErrorAbortAll 立即取消所有主机的执行
	OnErrorAbortAll OnError = "abort_all"
)

type Playbook

type Playbook struct {
	Name        string            `yaml:"name"`
	Description string            `yaml:"description,omitempty"`
	Targets     Targets           `yaml:"targets"`
	Settings    Settings          `yaml:"settings,omitempty"`
	Vars        map[string]string `yaml:"vars,omitempty"`
	Steps       []Step            `yaml:"steps"`
}

Playbook 是编排文件的顶层结构

func Load

func Load(path string, extraVars map[string]string) (*Playbook, error)

Load 从文件路径加载并解析 Playbook。 支持:

  • 绝对路径或相对路径(带 .yaml/.yml 扩展名)
  • ~/.xops/playbooks/<name> 名称查找(会自动补全 .yaml/.yml 扩展名)

func (*Playbook) Validate

func (p *Playbook) Validate() error

Validate 校验 Playbook 结构合法性。

type Report

type Report struct {
	PlaybookName string
	StartTime    time.Time
	EndTime      time.Time
	Hosts        []HostReport
}

Report 是整个 Playbook 执行的汇总报告

func (*Report) Duration

func (r *Report) Duration() time.Duration

Duration 返回整个 Playbook 的执行耗时

func (*Report) Print

func (r *Report) Print() error

Print writes the report to stdout.

func (*Report) RenderTo added in v0.12.0

func (r *Report) RenderTo(w io.Writer) error

RenderTo writes the formatted execution report to w.

type Settings

type Settings struct {
	// Concurrency 控制最大并发主机数,0 表示使用默认值 1
	Concurrency uint                 `yaml:"concurrency,omitempty"`
	Sudo        bool                 `yaml:"sudo,omitempty"`
	OnError     OnError              `yaml:"on_error,omitempty"`
	Timeout     Duration             `yaml:"timeout,omitempty"`
	Execution   *ssh.ExecutionConfig `yaml:"execution,omitempty"`
}

Settings 定义全局执行配置

type Step

type Step struct {
	Name     string      `yaml:"name"`
	Shell    string      `yaml:"shell,omitempty"`
	Script   string      `yaml:"script,omitempty"`
	Copy     *CopySpec   `yaml:"copy,omitempty"`
	Ensure   *EnsureSpec `yaml:"ensure,omitempty"`
	Template *CopySpec   `yaml:"template,omitempty"`

	// Sudo 步骤级别的提权覆盖;nil 表示继承全局 Settings.Sudo
	Sudo *bool `yaml:"sudo,omitempty"`

	// Execution 步骤级别的执行配置覆盖;nil 表示继承 Playbook Settings.Execution
	Execution *ssh.ExecutionConfig `yaml:"execution,omitempty"`

	// Retries 失败重试次数,默认 0(不重试)
	Retries int `yaml:"retries,omitempty"`
	// RetryDelay 重试间隔,默认 1s
	RetryDelay Duration `yaml:"retry_delay,omitempty"`
	// IgnoreError 即使步骤失败也继续,优先级高于 Settings.OnError
	IgnoreError bool `yaml:"ignore_error,omitempty"`
}

Step 定义单个执行步骤 每个 Step 必须恰好指定 shell/script/copy/ensure/template 之一

type StepResult

type StepResult struct {
	StepName   string
	Status     StepStatus
	Outcome    ssh.ExecutionOutcome
	ExitCode   *uint32
	Signal     string
	PlanDigest string
	Output     string
	Truncated  bool
	Err        error
	CleanupErr error
	IOErr      error
	Duration   time.Duration
	Retryable  bool
}

StepResult 表示单个步骤在单台主机上的执行结果

type StepStatus

type StepStatus string

StepStatus 表示单个步骤的执行状态

const (
	// StatusOK 步骤成功且无变更(ensure: check 通过直接返回)
	StatusOK StepStatus = "ok"
	// StatusChanged 步骤成功并产生了实际变更
	StatusChanged StepStatus = "changed"
	// StatusSkipped ensure 步骤中 check 通过,跳过 action
	StatusSkipped StepStatus = "skipped"
	// StatusFailed 步骤执行失败
	StatusFailed StepStatus = "failed"
)

type TargetNotFoundError added in v0.12.0

type TargetNotFoundError struct {
	Target string
}

TargetNotFoundError 表示指定的目标节点或主机在清单中不存在

func (*TargetNotFoundError) Error added in v0.12.0

func (e *TargetNotFoundError) Error() string

type Targets

type Targets struct {
	Tags  []string `yaml:"tags,omitempty"`
	Nodes []string `yaml:"nodes,omitempty"`
	Hosts []string `yaml:"hosts,omitempty"`
}

Targets 定义目标主机选择(三种方式任选其一)

Jump to

Keyboard shortcuts

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