feat: docs for modules (#2623)

Signed-off-by: joyceliu <joyceliu@yunify.com>
This commit is contained in:
liujian 2025-06-17 17:40:31 +08:00 committed by GitHub
parent 2cb9d8d4c4
commit e3f2372ee0
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
16 changed files with 485 additions and 142 deletions

View File

@ -26,9 +26,9 @@ kk precheck
### 本地
执行命令示例:
```shell
kk run playbooks/demo.yaml --project-addr=$(ProjectDir)
kk run demo.yaml
```
执行`$(ProjectDir)`目录中的`playbooks/demo.yaml`流程文件.
执行当前目录的`demo.yaml`流程文件.
### git
执行命令示例:
```shell

View File

@ -1,7 +1,7 @@
# 任务
task分为单层级task,和多层级task
单层级task: 包含[module](005-module.md)相关字段, 不包含. 一个task只能包含一个module.
多层级task: 不包含[module](005-module.md)相关字段, 包含block字段.
单层级task: 包含 module 相关字段, 不包含block字段. 一个task只能包含一个module.
多层级task: 不包含 module 相关字段, 包含block字段.
task执行时, 会在定义的host分别上执行.
## 文件定义
```yaml
@ -20,19 +20,19 @@ task执行时, 会在定义的host分别上执行.
vars: {a: b}
block:
- name: Task Name
# [module]
[module]
rescue:
- name: Task Name
# [module]
[module]
always:
- name: Task Name
# [module]
[module]
- name: Task Name
tags: ["always"]
when: true
loop: [""]
#[module]
[module]
```
**include_tasks**: 该任务中引用其他任务模板文件.
**name**: task名称, 非必填.
@ -42,12 +42,27 @@ task执行时, 会在定义的host分别上执行.
**run_once**: 是否只执行一次, 非必填, 默认false, 会在第一个hosts上执行.
**ignore_errors**: 是否忽略失败, 非必填, 默认false.
**vars**: 配置默认参数, 非必填, yaml格式.
**[module相关字段](005-module.md)**: task实际要执行的操作, 非必填(当未block字段时, 必填).
**loop**: 循环执行module中定义的操作, 每次执行时,以`item: loop-value`的形式将值传递给module. 可以定义单个值(字符串)或多个值(数组), 非必填, 值采用[模板语法](101-syntax.md)编写, 对每个的host单独计算值.
**retries**: task执行失败时. 需要重新尝试几次.
**register**: 值为字符串, 将执行结果注册到[variable](201-variable.md)中, 传递给后续的task. 如果结果为json字符串, 会尝试将该字符串转成json结构层级存入variable中(key为register的值, value为输出值, 输出值包含: stderr和stdout两个字段)
**register**: 值为字符串, 将任务执行结果注册到[variable](201-variable.md)中, 传递给后续的task. register包含两个子字段
- stderr: 失败输出
- stdout: 成功输出
**register_type**: 注册register中stderr和stdout的格式。
- string: 默认,以字符串的格式注册。
- json: 以json的格式注册。
- yaml: 以yaml的格式注册。
**block**: task集合, 非必填(当未定义module相关字段时, 必填), 一定会执行.
**rescue**: task集合, 非必填, 当block执行失败(task集合有一个执行失败即为该block失败)时,执行该task集合.
**always**: task集合, 非必填, 当block和rescue执行完毕后(无论成功失败)都会执行该task集合.
**module**: task实际要执行的操作, 非必填(当未block字段时, 必填).map格式的数据, key为module_name, value为args. 可用的module需提前在项目中进行注册。已注册的module如下
- [assert](modules/assert.md)
- [command](modules/command.md)
- [copy](modules/copy.md)
- [debug](modules/debug.md)
- [fetch](modules/fetch.md)
- [gen_cert](modules/gen_cert.md)
- [image](modules/image.md)
- [prometheus](modules/prometheus.md)
- [set_fact](modules/set_fact.md)
- [setup](modules/setup.md)
- [template](modules/template.md)

View File

@ -1,116 +0,0 @@
# 任务执行模块
module定义了一个任务实际要执行的操作
## assert
用于断言host上的variable是否满足某个条件
```yaml
assert:
that: I'm assertion statement
success_msg: I'm success message
fail_msg: I'm failed message
msg: I'm failed message
```
**that**: 断言语句, 必填.值采用[模板语法](101-syntax.md)编写, 对每个的host单独计算值.
**success_msg**: 成功时输出, 非必填, 默认值为"True".
**fail_msg**: 失败时输出, 非必填, 默认值为"True".
**msg**: 失败时输出, 非必填, 默认值为"True". 优先输出`fail_msg`.
## command/shell
执行命令, command和shell的用法相同
```yaml
command: I'm command statement
```
值采用[模板语法](101-syntax.md)编写, 对每个的host单独计算值.
## copy
复制本地文件到host.
```yaml
copy:
src: srcpath
content: srcpath
dest: destpath
mode: 0755
```
**src**: 来源地址, 可以为绝对路径或相对路径, 可以是目录或者文件, 非必填(`content`未定义时, 必填). 值采用[模板语法](101-syntax.md)编写, 对每个的host单独计算值.
- 绝对路径: 从执行命令的机器上的绝对路径上获取.
- 相对路径: 从`project_dir`中获取, 获取顺序: $(project_dir)/roles/roleName/files/$(srcpath) > $(project_dir)/playbooks/.../$(current_playbook)/roles/$(roleName)/files/$(srcpath) > $(project_dir)/files/$(srcpath).
**content**: 来源文件内容, 非必填(`src`未定义时, 必填). 值采用[模板语法](101-syntax.md)编写, 对每个的host单独计算值.
**dest**: 目标地址, host上的绝对路径, 可以是目录或者文件(与`src`对应, 如果为文件,需要在末尾添加"/"), 必填. 值采用[模板语法](101-syntax.md)编写, 对每个的host单独计算值.
**mode**: 复制到host上的文件权限, 非必填, 默认源文件权限.
## fetch
从host上获取文件到本地.
```yaml
fetch:
src: srcpath
dest: destpath
```
**src**: 来源文件地址, host上的绝对路径, 必填. 值采用[模板语法](101-syntax.md)编写, 对每个的host单独计算值.
**dest**: 目标文件地址, 本地绝对路径, 可以是目录或者文件(与`src`对应, 如果为文件,需要在末尾添加"/"), 必填. 值采用[模板语法](101-syntax.md)编写, 对每个的host单独计算值.
## debug
打印信息
```yaml
debug:
var: I'm variable statement
msg: I'm message statement
```
**var**: 打印变量, 非必填, 值采用[模板语法](101-syntax.md)编写, 对每个的host单独计算值.
**msg**: 打印信息, 非必填, 值采用[模板语法](101-syntax.md)编写, 对每个的host单独计算值.
## template
templates中的文件内容采用[模板语法](101-syntax.md)编写, 对每个的host单独计算值.
将文件内容转换为实际文件后,复制本地文件到host.
```yaml
template:
src: srcpath
dest: destpath
mode: 0755
```
**src**: 来源地址, 可以为绝对路径或相对路径, 可以是目录或者文件, 必填. 值采用[模板语法](101-syntax.md)编写, 对每个的host单独计算值.
- 绝对路径: 从执行命令的机器上的绝对路径上获取.
- 相对路径: 从`project_dir`中获取, 获取顺序: $(project_dir)/roles/roleName/templates/$(srcpath) > $(project_dir)/playbooks/.../$(current_playbook)/roles/$(roleName)/templates/$(srcpath) > $(project_dir)/templates/$(srcpath).
**dest**: 目标地址, host上的绝对路径, 可以是目录或者文件(与`src`对应, 如果为文件,需要在末尾添加"/"), 必填. 值采用[模板语法](101-syntax.md)编写, 对每个的host单独计算值.
**mode**: 复制到host上的文件权限, 非必填, 默认源文件权限.
## set_fact
给所有host设置variable. 层级结构保持不变
```yaml
set_fact:
key: value
```
**key**: 必填, 可以为可以为多级结构(比如{k1:{k2:value}}).
**value**: 值采用[模板语法](101-syntax.md)编写, 对每个的host单独计算值.
## gen_cert
在工作目录生成证书, $(work_dir)/kubekey/pki/
```yaml
gen_cert:
root_key: keypath
root_cert: certpath
date: 87600h
sans: ["ip1","dns1"]
cn: common name
out_key: keypath
out_cert: certpath
```
**root_key**: 父证书的key文件绝对路径, 用于生成子证书, 非必填. 值采用[模板语法](101-syntax.md)编写, 对每个的host单独计算值.
**root_cert**: 父证书的cert文件绝对路径, 用于生成子证书, 非必填, 值采用[模板语法](101-syntax.md)编写, 对每个的host单独计算值.
**date**: 证书失效时间, 时间间隔格式(单位: s,m,h), 非必填, 默认10年.
**sans**: Subject Alternate Names, 支持数组或数组类型的json字符串格式, 非必填.
**cn**: Common Name. 必填.
**out_key**: 输出的证书key文件绝对路径, 必填, 值采用[模板语法](101-syntax.md)编写, 对每个的host单独计算值.
**out_cert**: 输出的证书cert文件绝对路径, 必填, 值采用[模板语法](101-syntax.md)编写, 对每个的host单独计算值.
当root_key或root_cert未定义时, 生成自签名证书.
## image
拉取镜像到本地目录, 或推送镜像到远程服务器
```yaml
image:
skip_tls_verify: true
pull: ["image1", "image2"]
push:
registry: local.kubekey
username: username
password: password
namespace_override: new_namespace
```
**skip_tls_verify**: 跳过证书认证. 默认true.
**pull**: 拉取镜像到本地工作目录, 非必填. 值采用[模板语法](101-syntax.md)编写, 对每个的host单独计算值.
**push**: 推送工作目录中的镜像到远程仓库, 非必填. 值采用[模板语法](101-syntax.md)编写, 对每个的host单独计算值.
**registry**: 远程仓库地址, 必填. 值采用[模板语法](101-syntax.md)编写, 对每个的host单独计算值.
**username**: 远程仓库认证用户, 非必填. 值采用[模板语法](101-syntax.md)编写, 对每个的host单独计算值.
**password**: 远程仓库认证密码, 非必填. 值采用[模板语法](101-syntax.md)编写, 对每个的host单独计算值.
**namespace_override**: 是否用新的路径, 覆盖镜像原来的路径, 非必填. 值采用[模板语法](101-syntax.md)编写, 对每个的host单独计算值.

View File

@ -1,19 +1,42 @@
# 语法
语法遵循`go template`规范.引用[sprig](https://github.com/Masterminds/sprig)进行函数扩展.
# 自定义函数
## toYaml
将参数转换成yaml字符串. 参数为左移空格数, 值为字符串
```yaml
{{ .yaml_variable | toYaml }}
```
## fromYaml
将yaml字符串转成参数格式
```yaml
{{ .yaml_string | fromYaml }}
```
## ipInCIDR
获取IP范围(cidr)内特定下标的IP地址
```yaml
{{ .cidr_variable | ipInCIDR 1 }}
```
## ipFamily
获取IP或IP_CIDR所属的family。返回值为Invalid, IPv4, IPv6
```yaml
{{ .ip | ipFamily }}
```
## pow
幂运算.
```yaml
# 2的3次方, 2 ** 3
{{ 2 | pow 3 }}
```
## toYaml
将参数转换成yaml字符串. 参数为左移空格数, 值为字符串
## subtractList
数组不包含
```yaml
{{ .yaml_variable | toYaml }}
```
## ipInCIDR
获取IP范围(cidr)内特定下标的IP地址
```yaml
{{ .cidr_variable | ipInCIDR 1 }}
# 返回一个新列表该列表中的元素在a中存在但在b中不存在
{{ .b | subtractList .a }}
```

View File

@ -14,13 +14,13 @@ metadata:
name: default
spec:
hosts:
- hostname1:
k1: v1
#...
- hostname2:
k2: v2
#...
- hostname3:
hostname1:
k1: v1
#...
hostname2:
k2: v2
#...
hostname3:
#...
groups:
groupname1:

59
docs/zh/modules/assert.md Normal file
View File

@ -0,0 +1,59 @@
# assert 模块
assert模块允许用户对参数条件进行断言。
## 参数
| 参数 | 说明 | 类型 | 必填 | 默认值 |
|------|------|------|------|-------|
| that | 断言的条件.需采用[模板语法](../101-syntax.md)编写 | 数组或字符串 | 是 | - |
| success_msg | 断言结果为true时输出到任务结果的 stdout 信息 | 字符串 | 否 | True |
| fail_msg | 断言结果为false时输出到任务结果的 stderr 信息 | 字符串 | 否 | False |
| msg | 通fail_msg. 优先级低于fail_msg | 字符串 | 否 | False |
## 使用示例
1. 断言条件为字符串
```yaml
- name: assert single condition
assert:
that: eq 1 1
```
任务执行结果:
stdout: "True"
stderr: ""
2. 断言条件为数组
```yaml
- name: assert multi-condition
assert:
that:
- eq 1 1
- eq 1 2
```
任务执行结果:
stdout: "False"
stderr: "False"
3. 设置成功输出
```yaml
- name: assert is succeed
assert:
that: eq 1 1
success_msg: "It's succeed"
```
任务执行结果:
stdout: "It's succeed"
stderr: ""
1. 设置失败输出
```yaml
- name: assert is failed
assert:
that: eq 1 2
fail_msg: "It's failed"
msg: "It's failed!"
```
任务执行结果:
stdout: "False"
stderr: "It's failed"

View File

@ -0,0 +1,25 @@
# command(shell) 模块
command或shell模块允许用户执行特定命令。执行何种命令由相关的connector实现
## 参数
| 参数 | 说明 | 类型 | 必填 | 默认值 |
|------|------|------|------|-------|
| command | 执行的命令.可使用[模板语法](../101-syntax.md) | 字符串 | 是 | - |
## 使用示例
1. 执行shell命令
connector.type 为 `local``ssh`
```yaml
- name: execute shell command
command: echo "aaa"
```
1. 执行kubernetes命令
connector.type 为 `kubernetes`
```yaml
- name: executor kubernetes command
command: kubectl get pod
```

48
docs/zh/modules/copy.md Normal file
View File

@ -0,0 +1,48 @@
# copy 模块
copy模块允许用户复制文件或文件夹到连接的目标主机。
## 参数
| 参数 | 说明 | 类型 | 必填 | 默认值 |
|------|------|------|------|-------|
| src | 原始文件或文件夹路径 | 字符串 | 否content空时必填 | - |
| content | 原始文件或文件夹内容 | 字符串 | 否src空时必填 | - |
| dest | 复制到目标主机的文件目录 | 字符串 | 是 | - |
## 使用示例
1. 复制相对路径文件到目标主机
相对路径是当前任务对应的`files`目录中。当前任务路径由task的annotations `kubesphere.io/rel-path` 指定
```yaml
- name: copy relative path
copy:
src: a.yaml
dest: /tmp/b.yaml
```
2. 复制绝对路径文件到目标主机
本地绝对路径的文件
```yaml
- name: copy absolute path
copy:
src: /tmp/a.yaml
dest: /tmp/b.yaml
```
3. 复制目录到目标主机
复制该目录下所有的文件和目录到目标主机
```yaml
- name: copy dir
copy:
src: /tmp
dest: /tmp
```
4. 复制文件内容到目标主机
```yaml
- name: copy content
copy:
content: hello
dest: /tmp/b.txt
```

54
docs/zh/modules/debug.md Normal file
View File

@ -0,0 +1,54 @@
# debug 模块
debug模块允许用户打印变量
## 参数
| 参数 | 说明 | 类型 | 必填 | 默认值 |
|------|------|------|------|-------|
| msg | 原始文件或文件夹路径 | 字符串 | 是 | - |
## 使用示例
1. 打印字符串
```yaml
- name: debug string
debug:
msg: I'm {{ .name }}
```
当变量`name`为kubekey时打印内容如下
```txt
DEBUG:
I'm kubekey
```
2. 打印map
```yaml
- name: debug map
debug:
msg: >-
{{ .product }}
```
当变量`product`为map时比如`{"name":"kubekey"}`,打印内容如下
```txt
DEBUG:
{
"name": "kubekey"
}
```
3. 打印数组
```yaml
- name: debug array
debug:
msg: >-
{{ .version }}
```
当变量`version`为array时比如`["1","2"]`,打印内容如下
```txt
DEBUG:
[
"1",
"2"
]
```

20
docs/zh/modules/fetch.md Normal file
View File

@ -0,0 +1,20 @@
# fetch 模块
fetch模块允许用户从远程主机上拉取文件到本地。
## 参数
| 参数 | 说明 | 类型 | 必填 | 默认值 |
|------|------|------|------|-------|
| src | 远程主机上要拉取的文件路径 | 字符串 | 是 | - |
| dest | 拉取到本地的文件路径 | 字符串 | 是 | - |
## 使用示例
1. 拉取文件
```yaml
- name: fetch file
fetch:
src: /tmp/src.yaml
dest: /tmp/dest.yaml
```

View File

@ -0,0 +1,52 @@
# gen_cert 模块
gen_cert模块允许用户校验或生成证书文件。
## 参数
| 参数 | 说明 | 类型 | 必填 | 默认值 |
|------|------|------|------|-------|
| root_key | CA证书的key路径 | 字符串 | 否 | - |
| root_cert | CA证书路径 | 字符串 | 否 | - |
| date | 证书过期时间 | 字符串 | 否 | 1y |
| policy | 证书生成策略Always, IfNotPresent | 字符串 | 否 | IfNotPresent |
| sans | Subject Alternative Names. 允许的IP和DNS | 字符串 | 否 | - |
| cn | Common Name | 字符串 | 是 | - |
| out_key | 生成的证书key路径 | 字符串 | 是 | - |
| out_cert | 生成的证书路径 | 字符串 | 是 | - |
证书生成策略:
Always: 无论`out_key`和`out_cert`指向的证书路径是否存在,都会生成新的证书进行覆盖。
IfNotPresent: 当`out_key`和`out_cert`指向的证书路径存在时,只对该证书进行校验,并不会生成新的证书。
## 使用示例
1. 生成自签名的CA证书
生成CA证书时`root_key`和`root_cert`需为空
```yaml
- name: Generate root ca file
gen_cert:
cn: root
date: 87600h
policy: IfNotPresent
out_key: /tmp/pki/root.key
out_cert: /tmp/pki/root.crt
```
2. 校验或签发证书
对于非CA证书。`root_key`和`root_cert`需指向已存在CA证书。
```yaml
- name: Generate registry image cert file
gen_cert:
root_key: /tmp/pki/root.key
root_cert: /tmp/pki/root.crt
cn: server
sans:
- 127.0.0.1
- localhost
date: 87600h
policy: IfNotPresent
out_key: /tmp/pki/server.key
out_cert: /tmp/pki/server.crt
when: .groups.image_registry | default list | empty | not
```

67
docs/zh/modules/image.md Normal file
View File

@ -0,0 +1,67 @@
# image 模块
image模块允许用户下载镜像到本地目录或上传镜像到远程目录。
## 参数
| 参数 | 说明 | 类型 | 必填 | 默认值 |
|------|------|------|------|-------|
| pull | 把镜像从远程仓库中拉取到本地目录 | map | 否 | - |
| pull.images_dir | 镜像存放的本地目录 | 字符串 | 否 | - |
| pull.manifests | 需要拉取的镜像列表 | 字符串数组 | 是 | - |
| pull.username | 用于认证远程仓库的用户 | 字符串 | 否 | - |
| pull.password | 用于认证远程仓库的密码 | 字符串 | 否 | - |
| pull.skip_tls_verify | 是否跳过远程仓库的tls认证 | bool | 否 | - |
| push | 从本地目录中推送镜像到远程仓库 | map | 否 | - |
| push.images_dir | 镜像存放的本地目录 | 字符串 | 否 | - |
| push.username | 用于认证远程仓库的用户 | 字符串 | 否 | - |
| push.password | 用于认证远程仓库的密码 | 字符串 | 否 | - |
| push.skip_tls_verify | 是否跳过远程仓库的tls认证 | bool | 否 | - |
| push.src_pattern | 正则表达式,过滤本地目录中存放的镜像 | map | 否 | - |
| push.dest | 模版语法,从本地目录镜像推送到的远程仓库镜像 | map | 否 | - |
每个本地目录存放的镜像对应一个dest镜像。
```txt
|-- images_dir/
| |-- registry1/
| | |-- image1/
| | | |-- manifests/
| | | | |-- reference
| | |-- image2/
| | | |-- manifests/
| | | | |-- reference
| |-- registry2/
| | |-- image1/
| | | |-- manifests/
| | | | |-- reference
```
对于每个src镜像对应有一个dest。dest中有如下默认的模板变量
{{ .module.image.src.reference.registry }}: 本地目录中单个镜像的registry
{{ .module.image.src.reference.repository }}: 本地目录中单个镜像的repository
{{ .module.image.src.reference.reference }}: 本地目录中单个镜像的reference
## 使用示例
1. 拉取镜像
```yaml
- name: pull images
image:
pull:
images_dir: /tmp/images/
manifests:
- "docker.io/kubesphere/ks-apiserver:v4.1.3"
- "docker.io/kubesphere/ks-controller-manager:v4.1.3"
- "docker.io/kubesphere/ks-console:3.19"
```
2. 推送镜像到远程仓库
```yaml
- name: push images
push:
images_dir: /tmp/images/
dest: hub.kubekey/{{ .module.image.src.reference.repository }}:{{ .module.image.src.reference.reference }}
```
即:
docker.io/kubesphere/ks-apiserver:v4.1.3 => hub.kubekey/kubesphere/ks-apiserver:v4.1.3
docker.io/kubesphere/ks-controller-manager:v4.1.3 => hub.kubekey/kubesphere/ks-controller-manager:v4.1.3
docker.io/kubesphere/ks-console:3.19 => hub.kubekey/kubesphere/ks-console:v4.1.3

View File

@ -0,0 +1,27 @@
# set_fact 模块
set_fact模块允许用户将变量设置到所有的主机中生效。
## 参数
| 参数 | 说明 | 类型 | 必填 | 默认值 |
|------|------|------|------|-------|
| any | 需要设置的任意参数 | 字符串或map | 否 | - |
## 使用示例
1. 设置字符串参数
```yaml
- name: set string
set_fact:
a: b
c: d
```
2. 设置map参数
```yaml
- name: set map
set_fact:
a:
b: c
```

22
docs/zh/modules/setup.md Normal file
View File

@ -0,0 +1,22 @@
# setup 模块
gather_fact的底层实现setup模块允许用户获取主机的信息
## 参数
null
## 使用示例
1. 在playbook中使用gather_fact
```yaml
- name: playbook
hosts: localhost
gather_fact: true
```
2. 在task中使用setup
```yaml
- name: setup
setup: {}
```

View File

@ -0,0 +1,47 @@
# template 模块
template模块允许用户将模板文件解析后复制到连接的目标主机。
## 参数
| 参数 | 说明 | 类型 | 必填 | 默认值 |
|------|------|------|------|-------|
| src | 原始文件或文件夹路径 | 字符串 | 否content空时必填 | - |
| dest | 复制到目标主机的文件目录 | 字符串 | 是 | - |
## 使用示例
1. 复制相对路径文件到目标主机
相对路径是当前任务对应的`templates`目录中。当前任务路径由task的annotations `kubesphere.io/rel-path` 指定
```yaml
- name: copy relative path
template:
src: a.yaml
dest: /tmp/b.yaml
```
2. 复制绝对路径文件到目标主机
本地模板文件绝对路径的文件
```yaml
- name: copy absolute path
template:
src: /tmp/a.yaml
dest: /tmp/b.yaml
```
3. 复制目录到目标主机
将目录下所有的模板文件解析后复制到目标主机
```yaml
- name: copy dir
template:
src: /tmp
dest: /tmp
```
1. 复制文件内容到目标主机
```yaml
- name: copy content
template:
content: hello
dest: /tmp/b.txt
```

View File

@ -232,7 +232,7 @@ func newImageArgs(_ context.Context, raw runtime.RawExtension, vars map[string]a
ipl.username, _ = variable.StringVar(vars, pull, "username")
ipl.password, _ = variable.StringVar(vars, pull, "password")
ipl.imagesDir, _ = variable.StringVar(vars, pull, "images_dir")
ipl.skipTLSVerify, _ = variable.BoolVar(vars, pull, "skipTLSVerify")
ipl.skipTLSVerify, _ = variable.BoolVar(vars, pull, "skip_tls_verify")
if ipl.skipTLSVerify == nil {
ipl.skipTLSVerify = ptr.To(false)
}