mirror of
https://github.com/kubesphere/kubekey.git
synced 2025-12-26 01:22:51 +00:00
doc: Add details to the module comments. (#2587)
Signed-off-by: joyceliu <joyceliu@yunify.com>
This commit is contained in:
parent
d979c92066
commit
de5cc690e2
|
|
@ -23,6 +23,7 @@ import (
|
|||
"github.com/cockroachdb/errors"
|
||||
kkprojectv1 "github.com/kubesphere/kubekey/api/project/v1"
|
||||
"k8s.io/apimachinery/pkg/runtime"
|
||||
utilruntime "k8s.io/apimachinery/pkg/util/runtime"
|
||||
"k8s.io/klog/v2"
|
||||
ctrlclient "sigs.k8s.io/controller-runtime/pkg/client"
|
||||
|
||||
|
|
@ -30,6 +31,60 @@ import (
|
|||
"github.com/kubesphere/kubekey/v4/pkg/variable"
|
||||
)
|
||||
|
||||
/*
|
||||
The Assert module evaluates boolean conditions and returns success/failure messages.
|
||||
This module allows users to perform assertions in playbooks and control flow based on conditions.
|
||||
|
||||
Configuration:
|
||||
Users can specify conditions to evaluate and customize success/failure messages:
|
||||
|
||||
assert:
|
||||
that: # List of conditions to evaluate (required)
|
||||
- "{{ condition1 }}"
|
||||
- "{{ condition2 }}"
|
||||
success_msg: "Success" # optional: message to return on success (default: "True")
|
||||
fail_msg: "Failed" # optional: high priority failure message
|
||||
msg: "Error" # optional: fallback failure message (default: "False")
|
||||
|
||||
Usage Examples in Playbook Tasks:
|
||||
1. Basic assertion:
|
||||
```yaml
|
||||
- name: Check if service is running
|
||||
assert:
|
||||
that:
|
||||
- "{{ service_status == 'running' }}"
|
||||
register: check_result
|
||||
```
|
||||
|
||||
2. Custom messages:
|
||||
```yaml
|
||||
- name: Verify deployment
|
||||
assert:
|
||||
that:
|
||||
- "{{ deployment_ready }}"
|
||||
- "{{ pods_running }}"
|
||||
success_msg: "Deployment is healthy"
|
||||
fail_msg: "Deployment check failed"
|
||||
register: verify_result
|
||||
```
|
||||
|
||||
3. Multiple conditions:
|
||||
```yaml
|
||||
- name: Validate configuration
|
||||
assert:
|
||||
that:
|
||||
- "{{ config_valid }}"
|
||||
- "{{ required_fields_present }}"
|
||||
- "{{ values_in_range }}"
|
||||
msg: "Configuration validation failed"
|
||||
register: config_check
|
||||
```
|
||||
|
||||
Return Values:
|
||||
- On success: Returns success_msg (or "True") in stdout
|
||||
- On failure: Returns fail_msg (or msg or "False") in stderr
|
||||
*/
|
||||
|
||||
type assertArgs struct {
|
||||
that []string
|
||||
successMsg string
|
||||
|
|
@ -63,7 +118,7 @@ func newAssertArgs(_ context.Context, raw runtime.RawExtension, vars map[string]
|
|||
return aa, nil
|
||||
}
|
||||
|
||||
// ModuleAssert deal "assert" module
|
||||
// ModuleAssert handles the "assert" module, evaluating boolean conditions and returning appropriate messages
|
||||
func ModuleAssert(ctx context.Context, options ExecOptions) (string, string) {
|
||||
// get host variable
|
||||
ha, err := options.getAllVariables()
|
||||
|
|
@ -109,3 +164,7 @@ func ModuleAssert(ctx context.Context, options ExecOptions) (string, string) {
|
|||
|
||||
return StdoutFalse, "False"
|
||||
}
|
||||
|
||||
func init() {
|
||||
utilruntime.Must(RegisterModule("assert", ModuleAssert))
|
||||
}
|
||||
|
|
|
|||
|
|
@ -21,10 +21,48 @@ import (
|
|||
"fmt"
|
||||
"strings"
|
||||
|
||||
utilruntime "k8s.io/apimachinery/pkg/util/runtime"
|
||||
|
||||
"github.com/kubesphere/kubekey/v4/pkg/variable"
|
||||
)
|
||||
|
||||
// ModuleCommand deal "command" module.
|
||||
/*
|
||||
The Command module executes shell commands on remote hosts and returns their output.
|
||||
This module allows users to run arbitrary shell commands and capture their output.
|
||||
|
||||
Configuration:
|
||||
Users can specify the command to execute:
|
||||
|
||||
command: "ls -l" # The shell command to execute
|
||||
|
||||
Usage Examples in Playbook Tasks:
|
||||
1. Basic command execution:
|
||||
```yaml
|
||||
- name: List directory contents
|
||||
command: ls -l
|
||||
register: ls_result
|
||||
```
|
||||
|
||||
2. Command with variables:
|
||||
```yaml
|
||||
- name: Check service status
|
||||
command: systemctl status {{ service_name }}
|
||||
register: service_status
|
||||
```
|
||||
|
||||
3. Complex command:
|
||||
```yaml
|
||||
- name: Get disk usage
|
||||
command: df -h | grep /dev/sda1
|
||||
register: disk_usage
|
||||
```
|
||||
|
||||
Return Values:
|
||||
- On success: Returns command output in stdout
|
||||
- On failure: Returns error message in stderr
|
||||
*/
|
||||
|
||||
// ModuleCommand handles the "command" module, executing shell commands on remote hosts
|
||||
func ModuleCommand(ctx context.Context, options ExecOptions) (string, string) {
|
||||
// get host variable
|
||||
ha, err := options.getAllVariables()
|
||||
|
|
@ -54,3 +92,8 @@ func ModuleCommand(ctx context.Context, options ExecOptions) (string, string) {
|
|||
|
||||
return stdout, stderr
|
||||
}
|
||||
|
||||
func init() {
|
||||
utilruntime.Must(RegisterModule("command", ModuleCommand))
|
||||
utilruntime.Must(RegisterModule("shell", ModuleCommand))
|
||||
}
|
||||
|
|
|
|||
|
|
@ -28,6 +28,7 @@ import (
|
|||
"github.com/cockroachdb/errors"
|
||||
kkcorev1alpha1 "github.com/kubesphere/kubekey/api/core/v1alpha1"
|
||||
"k8s.io/apimachinery/pkg/runtime"
|
||||
utilruntime "k8s.io/apimachinery/pkg/util/runtime"
|
||||
"k8s.io/klog/v2"
|
||||
"k8s.io/utils/ptr"
|
||||
|
||||
|
|
@ -37,6 +38,55 @@ import (
|
|||
"github.com/kubesphere/kubekey/v4/pkg/variable"
|
||||
)
|
||||
|
||||
/*
|
||||
The Copy module copies files or content from local to remote hosts.
|
||||
This module allows users to transfer files or create files with specified content on remote hosts.
|
||||
|
||||
Configuration:
|
||||
Users can specify either a source file or content to copy.
|
||||
|
||||
copy:
|
||||
src: /path/to/file # optional: local file path to copy
|
||||
content: "text" # optional: content to write to file
|
||||
dest: /remote/path # required: destination path on remote host
|
||||
mode: 0644 # optional: file permissions (default: 0644)
|
||||
|
||||
Usage Examples in Playbook Tasks:
|
||||
1. Copy local file:
|
||||
```yaml
|
||||
- name: Copy configuration file
|
||||
copy:
|
||||
src: config.yaml
|
||||
dest: /etc/app/config.yaml
|
||||
mode: 0644
|
||||
register: copy_result
|
||||
```
|
||||
|
||||
2. Create file with content:
|
||||
```yaml
|
||||
- name: Create config file
|
||||
copy:
|
||||
content: |
|
||||
server: localhost
|
||||
port: 8080
|
||||
dest: /etc/app/config.yaml
|
||||
register: config_result
|
||||
```
|
||||
|
||||
3. Copy directory:
|
||||
```yaml
|
||||
- name: Copy application files
|
||||
copy:
|
||||
src: app/
|
||||
dest: /opt/app/
|
||||
register: app_files
|
||||
```
|
||||
|
||||
Return Values:
|
||||
- On success: Returns "Success" in stdout
|
||||
- On failure: Returns error message in stderr
|
||||
*/
|
||||
|
||||
type copyArgs struct {
|
||||
src string
|
||||
content string
|
||||
|
|
@ -67,7 +117,7 @@ func newCopyArgs(_ context.Context, raw runtime.RawExtension, vars map[string]an
|
|||
return ca, nil
|
||||
}
|
||||
|
||||
// ModuleCopy deal "copy" module
|
||||
// ModuleCopy handles the "copy" module, copying files or content to remote hosts
|
||||
func ModuleCopy(ctx context.Context, options ExecOptions) (string, string) {
|
||||
// get host variable
|
||||
ha, err := options.getAllVariables()
|
||||
|
|
@ -270,3 +320,7 @@ func (ca copyArgs) absDir(ctx context.Context, conn connector.Connector) error {
|
|||
return conn.PutFile(ctx, data, dest, mode)
|
||||
})
|
||||
}
|
||||
|
||||
func init() {
|
||||
utilruntime.Must(RegisterModule("copy", ModuleCopy))
|
||||
}
|
||||
|
|
|
|||
|
|
@ -25,11 +25,45 @@ import (
|
|||
"strings"
|
||||
|
||||
kkprojectv1 "github.com/kubesphere/kubekey/api/project/v1"
|
||||
utilruntime "k8s.io/apimachinery/pkg/util/runtime"
|
||||
|
||||
"github.com/kubesphere/kubekey/v4/pkg/variable"
|
||||
)
|
||||
|
||||
// ModuleDebug deal "debug" module
|
||||
/*
|
||||
The Debug module provides debugging capabilities by printing variable values and messages.
|
||||
This module allows users to inspect variable values and debug playbook execution.
|
||||
|
||||
Configuration:
|
||||
Users can specify either a message or a variable path to debug.
|
||||
|
||||
debug:
|
||||
msg: "message" # optional: direct message to print
|
||||
msg: "{{ .var }}" # optional: template syntax to print variable value
|
||||
|
||||
Usage Examples in Playbook Tasks:
|
||||
1. Print direct message:
|
||||
```yaml
|
||||
- name: Debug message
|
||||
debug:
|
||||
msg: "Starting deployment"
|
||||
register: debug_result
|
||||
```
|
||||
|
||||
2. Print variable value:
|
||||
```yaml
|
||||
- name: Debug variable
|
||||
debug:
|
||||
msg: "{{ .config.version }}"
|
||||
register: var_debug
|
||||
```
|
||||
|
||||
Return Values:
|
||||
- On success: Returns formatted message/variable value in stdout
|
||||
- On failure: Returns error message in stderr
|
||||
*/
|
||||
|
||||
// ModuleDebug handles the "debug" module, printing debug information
|
||||
func ModuleDebug(_ context.Context, options ExecOptions) (string, string) {
|
||||
// get host variable
|
||||
ha, err := options.getAllVariables()
|
||||
|
|
@ -43,7 +77,7 @@ func ModuleDebug(_ context.Context, options ExecOptions) (string, string) {
|
|||
return "", "\"msg\" is not found"
|
||||
case reflect.String:
|
||||
if !kkprojectv1.IsTmplSyntax(v.String()) {
|
||||
return FormatOutput([]byte(v.String()), options.LogOutput), ""
|
||||
return formatOutput([]byte(v.String()), options.LogOutput), ""
|
||||
}
|
||||
val := kkprojectv1.TrimTmplSyntax(v.String())
|
||||
if !strings.HasPrefix(val, ".") {
|
||||
|
|
@ -53,20 +87,20 @@ func ModuleDebug(_ context.Context, options ExecOptions) (string, string) {
|
|||
if err != nil {
|
||||
return "", err.Error()
|
||||
}
|
||||
return FormatOutput(data, options.LogOutput), ""
|
||||
return formatOutput(data, options.LogOutput), ""
|
||||
default:
|
||||
// do not parse by ctx
|
||||
data, err := json.Marshal(v.Interface())
|
||||
if err != nil {
|
||||
return "", err.Error()
|
||||
}
|
||||
return FormatOutput(data, options.LogOutput), ""
|
||||
return formatOutput(data, options.LogOutput), ""
|
||||
}
|
||||
}
|
||||
|
||||
// FormatOutput formats data as pretty JSON and logs it with DEBUG prefix if output is provided
|
||||
// formatOutput formats data as pretty JSON and logs it with DEBUG prefix if output is provided
|
||||
// Returns the formatted string
|
||||
func FormatOutput(data any, output io.Writer) string {
|
||||
func formatOutput(data any, output io.Writer) string {
|
||||
var msg string
|
||||
prettyJSON, err := json.MarshalIndent(data, "", " ")
|
||||
if err == nil {
|
||||
|
|
@ -77,3 +111,7 @@ func FormatOutput(data any, output io.Writer) string {
|
|||
}
|
||||
return msg
|
||||
}
|
||||
|
||||
func init() {
|
||||
utilruntime.Must(RegisterModule("debug", ModuleDebug))
|
||||
}
|
||||
|
|
|
|||
|
|
@ -22,10 +22,47 @@ import (
|
|||
"os"
|
||||
"path/filepath"
|
||||
|
||||
utilruntime "k8s.io/apimachinery/pkg/util/runtime"
|
||||
|
||||
"github.com/kubesphere/kubekey/v4/pkg/variable"
|
||||
)
|
||||
|
||||
// ModuleFetch deal fetch module
|
||||
/*
|
||||
The Fetch module retrieves files from remote hosts to the local machine.
|
||||
This module allows users to download files from remote hosts to specified local destinations.
|
||||
|
||||
Configuration:
|
||||
Users can specify the source and destination paths:
|
||||
|
||||
fetch:
|
||||
src: /remote/path/file # required: source file path on remote host
|
||||
dest: /local/path/file # required: destination path on local machine
|
||||
|
||||
Usage Examples in Playbook Tasks:
|
||||
1. Basic file fetch:
|
||||
```yaml
|
||||
- name: Download configuration file
|
||||
fetch:
|
||||
src: /etc/app/config.yaml
|
||||
dest: ./configs/
|
||||
register: fetch_result
|
||||
```
|
||||
|
||||
2. Fetch with variables:
|
||||
```yaml
|
||||
- name: Download log file
|
||||
fetch:
|
||||
src: /var/log/{{ app_name }}.log
|
||||
dest: ./logs/
|
||||
register: log_file
|
||||
```
|
||||
|
||||
Return Values:
|
||||
- On success: Returns "Success" in stdout
|
||||
- On failure: Returns error message in stderr
|
||||
*/
|
||||
|
||||
// ModuleFetch handles the "fetch" module, retrieving files from remote hosts
|
||||
func ModuleFetch(ctx context.Context, options ExecOptions) (string, string) {
|
||||
// get host variable
|
||||
ha, err := options.getAllVariables()
|
||||
|
|
@ -69,3 +106,7 @@ func ModuleFetch(ctx context.Context, options ExecOptions) (string, string) {
|
|||
|
||||
return StdoutSuccess, ""
|
||||
}
|
||||
|
||||
func init() {
|
||||
utilruntime.Must(RegisterModule("fetch", ModuleFetch))
|
||||
}
|
||||
|
|
|
|||
|
|
@ -18,6 +18,7 @@ import (
|
|||
|
||||
"github.com/cockroachdb/errors"
|
||||
"k8s.io/apimachinery/pkg/runtime"
|
||||
utilruntime "k8s.io/apimachinery/pkg/util/runtime"
|
||||
"k8s.io/apimachinery/pkg/util/sets"
|
||||
"k8s.io/apimachinery/pkg/util/validation"
|
||||
cgutilcert "k8s.io/client-go/util/cert"
|
||||
|
|
@ -28,6 +29,56 @@ import (
|
|||
"github.com/kubesphere/kubekey/v4/pkg/variable"
|
||||
)
|
||||
|
||||
/*
|
||||
The GenCert module generates SSL/TLS certificates for secure communication.
|
||||
This module can create both self-signed certificates and certificates signed by a root CA.
|
||||
|
||||
Configuration:
|
||||
Users can specify various certificate parameters:
|
||||
|
||||
gen_cert:
|
||||
cn: example.com # required: Common Name for the certificate
|
||||
out_key: /path/to/key # required: output path for private key
|
||||
out_cert: /path/to/cert # required: output path for certificate
|
||||
root_key: /path/to/ca.key # optional: root CA private key path
|
||||
root_cert: /path/to/ca.crt # optional: root CA certificate path
|
||||
sans: # optional: Subject Alternative Names
|
||||
- example.com
|
||||
- www.example.com
|
||||
policy: IfNotPresent # optional: certificate generation policy
|
||||
date: 8760h # optional: certificate validity period
|
||||
|
||||
Usage Examples in Playbook Tasks:
|
||||
1. Generate self-signed certificate:
|
||||
```yaml
|
||||
- name: Generate self-signed certificate
|
||||
gen_cert:
|
||||
cn: example.com
|
||||
out_key: /etc/ssl/private/example.key
|
||||
out_cert: /etc/ssl/certs/example.crt
|
||||
sans:
|
||||
- example.com
|
||||
- www.example.com
|
||||
register: cert_result
|
||||
```
|
||||
|
||||
2. Generate certificate signed by root CA:
|
||||
```yaml
|
||||
- name: Generate signed certificate
|
||||
gen_cert:
|
||||
cn: example.com
|
||||
root_key: /etc/ssl/private/ca.key
|
||||
root_cert: /etc/ssl/certs/ca.crt
|
||||
out_key: /etc/ssl/private/example.key
|
||||
out_cert: /etc/ssl/certs/example.crt
|
||||
register: signed_cert
|
||||
```
|
||||
|
||||
Return Values:
|
||||
- On success: Returns "Success" in stdout
|
||||
- On failure: Returns error message in stderr
|
||||
*/
|
||||
|
||||
const (
|
||||
// DefaultSignCertAfter defines the default timeout for sign certificates.
|
||||
defaultSignCertAfter = time.Hour * 24 * 365 * 10
|
||||
|
|
@ -164,8 +215,7 @@ func newGenCertArgs(_ context.Context, raw runtime.RawExtension, vars map[string
|
|||
return gca, nil
|
||||
}
|
||||
|
||||
// ModuleGenCert generate cert file.
|
||||
// if root_key and root_cert is empty, generate Self-signed certificate.
|
||||
// ModuleGenCert handles the "gen_cert" module, generating SSL/TLS certificates
|
||||
func ModuleGenCert(ctx context.Context, options ExecOptions) (string, string) {
|
||||
// get host variable
|
||||
ha, err := options.getAllVariables()
|
||||
|
|
@ -465,3 +515,7 @@ func validateCertificateWithConfig(cert *x509.Certificate, baseName string, cfg
|
|||
|
||||
return nil
|
||||
}
|
||||
|
||||
func init() {
|
||||
utilruntime.Must(RegisterModule("gen_cert", ModuleGenCert))
|
||||
}
|
||||
|
|
|
|||
|
|
@ -33,6 +33,7 @@ import (
|
|||
"github.com/containerd/containerd/images"
|
||||
imagev1 "github.com/opencontainers/image-spec/specs-go/v1"
|
||||
"k8s.io/apimachinery/pkg/runtime"
|
||||
utilruntime "k8s.io/apimachinery/pkg/util/runtime"
|
||||
"k8s.io/klog/v2"
|
||||
"k8s.io/utils/ptr"
|
||||
"oras.land/oras-go/v2"
|
||||
|
|
@ -44,6 +45,56 @@ import (
|
|||
"github.com/kubesphere/kubekey/v4/pkg/variable"
|
||||
)
|
||||
|
||||
/*
|
||||
The Image module handles container image operations including pulling images from registries and pushing images to private registries.
|
||||
|
||||
Configuration:
|
||||
Users can specify image operations through the following parameters:
|
||||
|
||||
image:
|
||||
pull: # optional: pull configuration
|
||||
manifests: []string # required: list of image manifests to pull
|
||||
images_dir: string # optional: directory to store pulled images
|
||||
skipTLSVerify: bool # optional: skip TLS verification
|
||||
push: # optional: push configuration
|
||||
registry: string # required: target registry URL
|
||||
username: string # optional: registry username
|
||||
password: string # optional: registry password
|
||||
namespace_override: string # optional: override image namespace
|
||||
images_dir: string # optional: directory containing images to push
|
||||
skipTLSVerify: bool # optional: skip TLS verification
|
||||
|
||||
Usage Examples in Playbook Tasks:
|
||||
1. Pull images from registry:
|
||||
```yaml
|
||||
- name: Pull container images
|
||||
image:
|
||||
pull:
|
||||
manifests:
|
||||
- nginx:latest
|
||||
- prometheus:v2.45.0
|
||||
images_dir: /path/to/images
|
||||
register: pull_result
|
||||
```
|
||||
|
||||
2. Push images to private registry:
|
||||
```yaml
|
||||
- name: Push images to private registry
|
||||
image:
|
||||
push:
|
||||
registry: registry.example.com
|
||||
username: admin
|
||||
password: secret
|
||||
namespace_override: custom-ns
|
||||
images_dir: /path/to/images
|
||||
register: push_result
|
||||
```
|
||||
|
||||
Return Values:
|
||||
- On success: Returns "Success" in stdout
|
||||
- On failure: Returns error message in stderr
|
||||
*/
|
||||
|
||||
type imageArgs struct {
|
||||
pull *imagePullArgs
|
||||
push *imagePushArgs
|
||||
|
|
@ -211,7 +262,7 @@ func newImageArgs(_ context.Context, raw runtime.RawExtension, vars map[string]a
|
|||
return ia, nil
|
||||
}
|
||||
|
||||
// ModuleImage deal "image" module
|
||||
// ModuleImage handles the "image" module, managing container image operations including pulling and pushing images
|
||||
func ModuleImage(ctx context.Context, options ExecOptions) (string, string) {
|
||||
// get host variable
|
||||
ha, err := options.getAllVariables()
|
||||
|
|
@ -525,3 +576,7 @@ func (i imageTransport) get(request *http.Request) *http.Response {
|
|||
|
||||
return responseNotAllowed
|
||||
}
|
||||
|
||||
func init() {
|
||||
utilruntime.Must(RegisterModule("image", ModuleImage))
|
||||
}
|
||||
|
|
|
|||
|
|
@ -24,7 +24,6 @@ import (
|
|||
kkcorev1 "github.com/kubesphere/kubekey/api/core/v1"
|
||||
kkcorev1alpha1 "github.com/kubesphere/kubekey/api/core/v1alpha1"
|
||||
"k8s.io/apimachinery/pkg/runtime"
|
||||
utilruntime "k8s.io/apimachinery/pkg/util/runtime"
|
||||
|
||||
"github.com/kubesphere/kubekey/v4/pkg/connector"
|
||||
"github.com/kubesphere/kubekey/v4/pkg/variable"
|
||||
|
|
@ -93,19 +92,6 @@ func FindModule(moduleName string) ModuleExecFunc {
|
|||
return module[moduleName]
|
||||
}
|
||||
|
||||
func init() {
|
||||
utilruntime.Must(RegisterModule("assert", ModuleAssert))
|
||||
utilruntime.Must(RegisterModule("command", ModuleCommand))
|
||||
utilruntime.Must(RegisterModule("shell", ModuleCommand))
|
||||
utilruntime.Must(RegisterModule("copy", ModuleCopy))
|
||||
utilruntime.Must(RegisterModule("fetch", ModuleFetch))
|
||||
utilruntime.Must(RegisterModule("debug", ModuleDebug))
|
||||
utilruntime.Must(RegisterModule("template", ModuleTemplate))
|
||||
utilruntime.Must(RegisterModule("set_fact", ModuleSetFact))
|
||||
utilruntime.Must(RegisterModule("gen_cert", ModuleGenCert))
|
||||
utilruntime.Must(RegisterModule("image", ModuleImage))
|
||||
}
|
||||
|
||||
type key struct{}
|
||||
|
||||
// ConnKey for connector which store in context
|
||||
|
|
|
|||
|
|
@ -21,11 +21,46 @@ import (
|
|||
"fmt"
|
||||
|
||||
"gopkg.in/yaml.v3"
|
||||
utilruntime "k8s.io/apimachinery/pkg/util/runtime"
|
||||
|
||||
"github.com/kubesphere/kubekey/v4/pkg/variable"
|
||||
)
|
||||
|
||||
// ModuleSetFact deal "set_fact" module
|
||||
/*
|
||||
The SetFact module allows setting variables during playbook execution.
|
||||
This module enables users to define and update variables that can be used in subsequent tasks.
|
||||
|
||||
Configuration:
|
||||
Users can specify key-value pairs to set as variables:
|
||||
|
||||
set_fact:
|
||||
key1: value1 # required: variable name and value
|
||||
key2: value2 # optional: additional variables
|
||||
|
||||
Usage Examples in Playbook Tasks:
|
||||
1. Set single variable:
|
||||
```yaml
|
||||
- name: Set version variable
|
||||
set_fact:
|
||||
app_version: "1.0.0"
|
||||
register: version_result
|
||||
```
|
||||
|
||||
2. Set multiple variables:
|
||||
```yaml
|
||||
- name: Set configuration variables
|
||||
set_fact:
|
||||
db_host: "localhost"
|
||||
db_port: 5432
|
||||
register: config_vars
|
||||
```
|
||||
|
||||
Return Values:
|
||||
- On success: Returns "Success" in stdout
|
||||
- On failure: Returns error message in stderr
|
||||
*/
|
||||
|
||||
// ModuleSetFact handles the "set_fact" module, setting variables during playbook execution
|
||||
func ModuleSetFact(_ context.Context, options ExecOptions) (string, string) {
|
||||
var node yaml.Node
|
||||
// Unmarshal the YAML document into a root node.
|
||||
|
|
@ -38,3 +73,7 @@ func ModuleSetFact(_ context.Context, options ExecOptions) (string, string) {
|
|||
|
||||
return StdoutSuccess, ""
|
||||
}
|
||||
|
||||
func init() {
|
||||
utilruntime.Must(RegisterModule("set_fact", ModuleSetFact))
|
||||
}
|
||||
|
|
|
|||
|
|
@ -28,6 +28,7 @@ import (
|
|||
"github.com/cockroachdb/errors"
|
||||
kkcorev1alpha1 "github.com/kubesphere/kubekey/api/core/v1alpha1"
|
||||
"k8s.io/apimachinery/pkg/runtime"
|
||||
utilruntime "k8s.io/apimachinery/pkg/util/runtime"
|
||||
"k8s.io/klog/v2"
|
||||
"k8s.io/utils/ptr"
|
||||
|
||||
|
|
@ -38,6 +39,43 @@ import (
|
|||
"github.com/kubesphere/kubekey/v4/pkg/variable"
|
||||
)
|
||||
|
||||
/*
|
||||
The Template module processes files using Go templates before copying them to remote hosts.
|
||||
This module allows users to template files with variables before transferring them.
|
||||
|
||||
Configuration:
|
||||
Users can specify source and destination paths:
|
||||
|
||||
template:
|
||||
src: /path/to/file # required: local file path to template
|
||||
dest: /remote/path # required: destination path on remote host
|
||||
mode: 0644 # optional: file permissions (default: 0644)
|
||||
|
||||
Usage Examples in Playbook Tasks:
|
||||
1. Basic file templating:
|
||||
```yaml
|
||||
- name: Template configuration file
|
||||
template:
|
||||
src: config.yaml.tmpl
|
||||
dest: /etc/app/config.yaml
|
||||
mode: 0644
|
||||
register: template_result
|
||||
```
|
||||
|
||||
2. Template with variables:
|
||||
```yaml
|
||||
- name: Template with variables
|
||||
template:
|
||||
src: app.conf.tmpl
|
||||
dest: /etc/app/app.conf
|
||||
register: app_config
|
||||
```
|
||||
|
||||
Return Values:
|
||||
- On success: Returns "Success" in stdout
|
||||
- On failure: Returns error message in stderr
|
||||
*/
|
||||
|
||||
type templateArgs struct {
|
||||
src string
|
||||
dest string
|
||||
|
|
@ -73,7 +111,7 @@ func newTemplateArgs(_ context.Context, raw runtime.RawExtension, vars map[strin
|
|||
return ta, nil
|
||||
}
|
||||
|
||||
// ModuleTemplate deal "template" module
|
||||
// ModuleTemplate handles the "template" module, processing files with Go templates
|
||||
func ModuleTemplate(ctx context.Context, options ExecOptions) (string, string) {
|
||||
ha, ta, conn, err := prepareTemplate(ctx, options)
|
||||
if err != nil {
|
||||
|
|
@ -271,3 +309,7 @@ func (ta templateArgs) absDir(ctx context.Context, conn connector.Connector, var
|
|||
|
||||
return nil
|
||||
}
|
||||
|
||||
func init() {
|
||||
utilruntime.Must(RegisterModule("template", ModuleTemplate))
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in New Issue