Akemi

Salt状态管理与高级状态文件

2025/09/19

后面改名叫配置管理了,但是还是感觉状态管理更好理解。和playbook比较像

远程执行可以节省时间,但是有一些缺点,各个命令之间有一些细微的区别,这些区别会导致,如果将远程执行的命令写成脚本,那就会存在很多问题

状态管理介绍

为了解决上面这个问题,salt可以创建一个可以重用的配置模板,将其称为state

状态文件的存放位置,取决于配置文件

1
2
3
4
5
6
7
8
9
10
11
12
13
14
/etc/salt/master
...
file_roots:
base:
- /srv/salt/base
dev:
- /srv/salt/dev
prod:
- /srv/salt/prod
...
mkdir /srv/salt/{base,dev,prod} -p
systemctl restart salt-master.service

定义了3个环境,需要将状态文件放进对应的文件夹

状态文件写法

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
cd /srv/salt/base/
vim example.sls # 需要以sls结尾
格式:
ID:
module.function:
- name: name
- argument:
- xxx
- xxx

显式命名:
install_nginx: # 状态ID:安装nginx
pkg.installed: # 使用pkg模块的installed函数
- name: nginx # 要安装的软件包名是nginx

隐式命名: name如果不定义,会自动使用ID
nginx:
pkg.installed:

enable_and_start_nginx: # 状态ID:描述这个状态是“启用并启动nginx”
service.running: # 确保服务在运行
- name: nginx # 服务名:nginx
- enable: True # 参数:设置开机自启
- require: # 依赖关系:需要先完成install_nginx这个状态
- pkg: install_nginx

运行状态文件

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 <目标minion> state.sls saltenv=<环境名称> <sls的名称>

cat > /srv/salt/base/example.sls <<EOF
nginx:
pkg.installed:
enable_and_start_nginx:
service.running:
- name: nginx
- enable: True
- require:
- pkg: nginx
EOF

如果环境复杂,一定需要在使用的时候指定环境,默认环境为base
salt 'minion1' state.sls saltenv="base" example

minion1:
----------(自带的注释)
ID: nginx
Function: pkg.installed
Result: True
Comment: The following packages were installed/updated: nginx
Started: 14:05:08.049181
Duration: 9614.023 ms
Summary for minion1
------------
Succeeded: 2 (changed=2)
Failed: 0
------------
Total states run: 2
Total run time: 9.717 s

状态模块的查询

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
查看有哪些状态模块
salt 'minion1' sys.list_state_modules

查看file状态模块下,有哪些方法
salt 'minion1' sys.list_state_functions file
minion1:
- file.absent
- file.accumulated
- file.append
- file.blockreplace
- file.cached
- file.comment
- file.copy
- file.decode
- file.directory
- file.exists
- file.hardlink
- file.keyvalue
- file.line
- file.managed
- file.missing
- file.mknod
- file.mod_beacon
- file.mod_run_check_cmd
- file.not_cached
- file.patch
- file.prepend
- file.pruned
- file.recurse
- file.rename
- file.replace
- file.retention_schedule
- file.serialize
- file.shortcut
- file.symlink
- file.tidied
- file.touch
- file.uncomment

查询某一个状态模块的方法的用法
salt 'minion1' sys.state_doc file.absent
file.absent:
Make sure that the named file or directory is absent. If it exists, it will
be deleted. This will work to reverse any of the functions in the file
state module. If a directory is supplied, it will be recursively deleted.
If only the contents of the directory need to be deleted but not the directory
itself, use :mod:`file.directory <salt.states.file.directory>` with ``clean=True``
name
The path which should be deleted

相比于低级的状态文件,多了对于主机的定义,所以与playbook更加相似。

而低级状态文件更类似于role的tasks.yml

高级状态文件名字叫top.sls,一般放在base环境下

高级状态文件可以最大程度的复用低级状态文件,只要定义了合理功能的低级状态文件

高级状态文件定义

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
修改配置文件,打开state_top
grep state_top /etc/salt/master
# use and what modules to use. The state_top file is defined relative to the
state_top: top.sls

systemctl restart salt-master.service

先准备两个低级状态文件
/srv/salt/dev/httpd.sls
httpd:
pkg.installed

/srv/salt/dev/apache2.sls
apache2:
pkg.installed

写一个高级状态文件
vim /srv/salt/base/top.sls
base:
"*":
- example
dev:
"minion[12]":
- httpd
"minion3":
- apache2

高级状态文件使用

1
2
3
4
5
6
7
salt \* state.apply
...

即使指定的是\*,也不会对结果有影响
因为其将playbook的tasks和hosts解耦

对于不同环境的不同主机能给出不同的清单
CATALOG
  1. 1. 状态管理介绍
  2. 2. 状态文件写法
  3. 3. 运行状态文件
  4. 4. 状态模块的查询
  5. 5. 高级状态文件定义
  6. 6. 高级状态文件使用