playbook

package
v0.9.0 Latest Latest
Warning

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

Go to latest
Published: May 30, 2026 License: MIT Imports: 17 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

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) *Engine

NewEngine 创建一个执行引擎实例。

func (*Engine) Run

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

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

type EnsureSpec

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

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

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()

Print 将格式化的执行报告输出到 stdout。

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"`
}

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"`

	// 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
	Output   string
	Err      error
	Duration time.Duration
}

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 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