Akemi

Salt执行模块与常用模块

2025/09/18

salt的在命令行输入的命令都是salt的远程执行模块,也就是一次性远程执行的方法ad-hoc,主要用于临时故障排除、一次性任务、实时查询

在ansible中,ansible的ad-hoc与playbook实际上使用的模块是同一种东西,两种不同的使用方式

salt不一样,salt的执行模块实际上是执行一个动作这样的一次性操作

而“状态模块”是希望其能够保持某种最终的状态,所以这里将常用模块和执行模块放到一块

特性 远程执行模块 (Execution Modules) 状态模块 (State Modules)
哲学 命令式 (Imperative)如何做?“现在就做这件事!” 声明式 (Declarative)应该是什么状态?“系统最终应该是这个样子。”
功能 直接执行操作、查询系统信息。 定义系统的期望配置状态,并确保系统符合该状态。
幂等性 不一定。执行一次 pkg.install 就会安装一次。 必须保证。多次执行同一个状态,只有在不符合期望状态时才会执行操作。
返回值 操作的结果(成功/失败、输出信息等)。 详细的变化报告(发生了什么变化,什么没有变化,为什么)。
常用场景 临时故障排除、一次性任务、实时查询。 配置管理、自动化部署、基础设施即代码 (IaC)。

salt命令的参数

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
salt --help
Usage: salt [options] '<target>' <function> [arguments]
即salt <命令参数> <目标主机> <模块>.<方法> <方法参数>

--summary 输出统计信息

指定目标Minion:
-E --pcre 使用正则表达式匹配 salt -E 'web.*' test.ping
-L --list 逗号分隔的minion列表 salt -L 'minion1,minion2' test.ping
-G --grain 按grains值匹配 salt -G 'os:CentOS' test.ping
-I --pillar 按pillar值匹配 salt -I 'role:webserver' test.ping
-S --ipcidr 按IP地址或CIDR匹配 salt -S '192.168.1.0/24' test.ping
-C --compound 复合匹配器 salt -C 'G@os:CentOS and minion*' test.ping
-N --nodegroup 使用预定义的节点组 salt -N 'web-servers' test.ping

输出格式:
--out --output 指定输出格式 salt '*' test.ping --out=json
--out-indent 输出缩进(JSON/YAML) salt '*' test.ping --out=json --out-indent=4
--out-file 输出到文件 salt '*' disk.usage --out-file=/tmp/disk.txt
--no-color 禁用颜色输出 salt '*' test.ping --no-color
--force-color 强制颜色输出 salt '*' test.ping --force-color
--state-output 状态执行输出格式 salt '*' state.apply --state-output=changes

控制参数:
-t --timeout 设置超时时间(秒) salt '*' test.ping -t 30
-b --batch 批量执行(一次几个minion) salt '*' state.apply -b 10
-v --verbose 显示更详细的信息 salt -v '*' test.ping
--async 异步执行,返回job id salt '*' state.apply --async
--show-jid 显示job id salt '*' test.ping --show-jid

--proxy 指定代理minion salt 'switch1' test.ping --proxy
--show-timeout 显示超时的minion salt '*' test.ping --show-timeout
--hide-timeout 隐藏超时的minion salt '*' test.ping --hide-timeout

--hard-crash 遇到错误时硬崩溃 salt '*' buggy.module --hard-crash
--skip-grains 跳过grains收集 salt '*' test.ping --skip-grains

模块使用与常用技巧

ping测试

1
2
3
4
5
6
7
8
9
10
salt "*" test.ping
minion1:
True
minion2:
True
minion3:
True

"*" 表示匹配所有被管理主机
test.ping 表示远程执行的test模块中的ping方法

cmd模块

跟ansible的shell模块差不多,其效率比ansible高。但是不建议,因为无法保证幂等性,最好还是使用其他模块

它这种使用方式和python subprocess.run也挺像的

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
salt "*" cmd.
cmd.exec_code cmd.retcode cmd.run_stderr cmd.shell_info
cmd.exec_code_all cmd.run cmd.run_stdout cmd.shells
cmd.has_exec cmd.run_all cmd.script cmd.tty
cmd.powershell cmd.run_bg cmd.script_retcode cmd.which
cmd.powershell_all cmd.run_chroot cmd.shell cmd.which_bin

salt "*" cmd.run "pwd"
minion1:
/root
minion3:
/root
minion2:
/root

salt "*" cmd.run "free -h"
minion1:
total used free shared buff/cache available
Mem: 7.8G 290M 7.4G 8.6M 162M 7.3G
Swap: 1.0G 0B 1.0G
minion2:
total used free shared buff/cache available
Mem: 7.6Gi 279Mi 7.0Gi 8.0Mi 309Mi 7.1Gi
Swap: 5.0Gi 0B 5.0Gi
minion3:
total used free shared buff/cache available
Mem: 3.8Gi 234Mi 3.1Gi 1.0Mi 490Mi 3.4Gi
Swap: 4.0Gi 0B 4.0Gi

salt一次性进行多次远程执行

使用逗号,将函数分开,然后顺序输入函数的参数,就可以一次性执行多个函数

1
2
3
4
5
6
7
8
9
10
11
salt "minion1" test.echo,cmd.run,service.status \
"ls","uname -r","sshd"

minion1:
----------
cmd.run:
3.10.0-1160.119.1.el7.x86_64
service.status:
True
test.echo:
ls

列出salt的模块与方法

salt模块类似于python的模块,一个模块中有大量的方法,可以实现多样的功能。

因此salt的模块比ansible更大,数量也就更少

不同的发行版本和python版本会导致模块数量和用法不同

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
列出模块
salt 'minion1' sys.list_modules | wc -l
168

salt 'minion3' sys.list_modules | wc -l
172

列出模块的方法
salt 'minion1' sys.list_functions disk
minion1:
- disk.blkid
- disk.dump
- disk.format
- disk.fstype
- disk.get_fstype_from_path
- disk.hdparms
- disk.hpa
- disk.inodeusage
- disk.iostat
- disk.percent
- disk.resize2fs
- disk.smart_attributes
- disk.tune
- disk.usage
- disk.wipe


列出方法的参数
salt 'minion1' sys.doc disk.dump
disk.dump:

Return all contents of dumpe2fs for a specified device

CLI Example:

salt '*' disk.dump /dev/sda1

salt的常用模块

基本上是现写现查

文件管理模块file

可以看出其实很多都是从python的os模块中拆出来的,甚至都有对应关系

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
salt 'minion1' file.
file.access file.rmdir...
...

增删改查:
file.write 创建或覆盖文件内容
file.append 向文件末尾追加内容
file.read 读取文件内容
file.copy 复制文件(minion节点上的操作,而非master复制过去)
file.move 移动/重命名文件
file.rename 重命名文件
file.remove 删除文件
file.touch 创建空文件或更新文件时间戳

目录操作:
file.mkdir 创建目录
file.makedirs 递归创建目录
file.makedirs_perms 创建目录并设置权限
file.rmdir 删除空目录
file.readdir 读取目录内容

文件内容操作:
file.grep 在文件中搜索模式
file.search 在文件中搜索并替换内容
file.sed 使用sed语法编辑文件
file.sed_contains 检查文件是否包含特定模式
file.replace 替换文件中的文本行(支持正则
file.blockreplace 替换文件中的文本块(支持正则
file.line 管理文件中的特定行

权限与属性管理:
file.chown 更改文件所有者
file.lchown 更改符号链接的所有者
file.chgrp 更改文件所属组
file.set_selinux_context 设置SELinux上下文
file.get_selinux_context 获取SELinux上下文
file.restorecon 恢复SELinux默认上下文

文件状态:
file.file_exists 检查文件是否存在
file.directory_exists 检查目录是否存在
file.is_blkdev 检查是否为块设备
file.is_chrdev 检查是否为字符设备
file.is_fifo 检查是否为FIFO
file.access 检查文件访问权限
file.stats 获取文件统计信息

路径处理:
file.basename 获取路径中的文件名部分
file.dirname 获取路径中的目录部分
file.join 连接路径组件

服务管理模块service

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
salt 'minion1' service.
service.available service.get_disabled service.restart
service.disable service.get_enabled service.show
service.disabled service.get_running service.start
service.enable service.get_static service.status
service.enabled service.mask service.stop
service.execs service.masked service.systemctl_reload
service.firstboot service.missing service.unmask
service.force_reload service.offline
service.get_all service.reload

查询服务
service.status 检查服务当前是否正在运行
service.get_all 获取所有可用服务的列表
service.get_running 获取所有正在运行的服务的列表
service.get_enabled 获取所有启用了开机自启的服务的列表
service.show 显示服务的详细信息 (类似于 systemctl show)
service.available 检查服务单元文件是否可用/存在
service.missing 检查服务是否不可用/不存在
service.execs 获取与服务相关的可执行文件路径

生命周期管理
service.start 启动服务
service.stop 停止服务
service.restart 重启服务
service.reload 重载服务
service.systemctl_reload 重载systemd(systemctl daemon-reload)
service.enable 启用开机自启
service.disable 禁用开机自启
service.enabled 检查是否启用开机自启
service.disabled 检查是否禁用开机自启

管理用户模块user

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
salt '*' user.
user.add user.chhomephone user.chuid user.info
user.chfullname user.chloginclass user.chworkphone user.list_groups
user.chgid user.chother user.delete user.list_users
user.chgroups user.chroomnumber user.getent user.primary_group
user.chhome user.chshell user.get_loginclass user.rename

创建与删除用户
salt '*' user.add username comment="User Full Name" home=/home/username shell=/bin/bash
salt '*' user.chgroups username groups=wheel,developers
salt '*' user.delete username remove=True # 删除用户并移除家目录

修改用户属性
salt '*' user.chfullname username "New Full Name"
salt '*' user.chshell username /bin/zsh

管理用户组
salt '*' user.list_groups username # 查看用户所属组
salt '*' user.chgroups username groups=sudo,docker # 更改用户附加组

获取用户信息
salt '*' user.info username # 获取基本用户信息
salt '*' user.getent username # 获取完整用户信息

软件管理模块pkg

不同的linux发行版本的软件包管理器都不一样,所以针对不同发行版的系统,ansible还需要使用facts中的变量来判断是什么系统,然后用yum或是pkg来安装。

但是salt就很方便直接用pkg就行

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
salt '*' pkg.
pkg.add_repo_key pkg.download pkg.group_diff
pkg.autoremove pkg.file_dict pkg.group_info
pkg.available_version pkg.file_list pkg.group_inst
pkg.clean_metadata pkg.get_locked_packages pkg.groupinsta
pkg.del_repo pkg.get_repo pkg.group_list
pkg.del_repo_key pkg.get_repo_keys pkg.hold
pkg.diff pkg.get_selections pkg.info_insta

安装软件包
salt '*' pkg.install nginx
# 批量安装软件包
salt '*' pkg.install pkgs='["vim", "git", "curl", "wget"]'
# 批量移除软件包
salt '*' pkg.remove pkgs='["package1", "package2"]'
# 添加新的软件仓库
salt '*' pkg.mod_repo 'my_repo' 'baseurl=http://repo.example.com'
# 添加GPG密钥
salt '*' pkg.add_repo_key '/path/to/key.gpg'
# 刷新仓库缓存
salt '*' pkg.refresh_db
# 检查软件包信息
salt '*' pkg.info_installed nginx
# 列出软件包文件
salt '*' pkg.file_list nginx

# 更新所有软件包
salt '*' pkg.upgrade
# 查看当前仓库
salt '*' pkg.get_repo
# 清理缓存
salt '*' pkg.clean_metadata
# 查看可用版本
salt '*' pkg.available_version nginx

salt进行远程拷贝

前面说到了既然salt的copy模块没法和ansible一样进行远程拷贝,那应该怎么做呢?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
salt-cp -h
Usage: salt-cp [options] '<target>' SOURCE DEST

salt-cp <参数> <目标主机> <源> <目标>

传送
touch test
salt-cp '*' ./test /tmp/
minion1:
----------
/tmp/test:
True
minion2:
----------
/tmp/test:
True
minion3:
----------
/tmp/test:
True

查看
salt '*' cmd.run 'ls /tmp/test'
minion1:
/tmp/test
minion2:
/tmp/test
minion3:
/tmp/test

CATALOG
  1. 1. salt命令的参数
  2. 2. 模块使用与常用技巧
    1. 2.1. salt一次性进行多次远程执行
    2. 2.2. 列出salt的模块与方法
  3. 3. salt的常用模块
    1. 3.1. 文件管理模块file
    2. 3.2. 服务管理模块service
    3. 3.3. 管理用户模块user
    4. 3.4. 软件管理模块pkg
    5. 3.5. salt进行远程拷贝