文章目录
1. YAML语言
YAML是一种直观的能够被电脑识别的数据序列化格式,是一个可读性高并且容易被人类阅读,容易和脚本语言交互,用来表达资料序列的编程语言。
它类似于标准通用标记语言的子集XML的数据描述语言,语法比XML简单很多。
AML的基本规则:
- 使用缩进来表示层级关系,每层2个空格,禁止使用TAB键
- 当冒号不是处于最后时,冒号后面必须有一个空格
- 用 - 表示列表,- 的后面必须有一个空格
- 用 # 表示注释
YAML配置文件要放到SaltStack让我们放的位置,可以在SaltStack的 Master 配置文件中查找file_roots即可看到。
[root@master ~]# vim /etc/salt/master
...此处省略N行
file_roots:
base:
- /srv/salt/base
test:
- /srv/salt/test
dev:
- /srv/salt/dev
prod:
- /srv/salt/prod
...此处省略N行
[root@master ~]# mkdir -p /srv/salt/{base,test,dev,prod}
[root@master ~]# tree /srv
/srv
└── salt
├── base
├── dev
├── prod
└── test
5 directories, 0 files
[root@master ~]# systemctl restart salt-master
各环境说明:
- base:基础环境
- test:测试环境
- dev:开发环境
- prod:生产环境
需要注意:
- base是默认的位置,如果file_roots只有一个,则base是必备的且必须叫base,不能改名
2. 用SaltStack配置一个apache实例
2.1 在Master上部署sls配置文件并执行
[root@master ~]# cd /srv/salt/base/
[root@master base]# ls
[root@master base]# mkdir -p /web/apache
[root@master base]# cd /web/apache/
[root@master apache]# touch apache.sls
[root@master apache]# vim apache.sls
apache-install:
pkg.installed:
- name: httpd
apache-service:
service.running:
- name: httpd
- enable: True
YAML 配置文件中顶格写的被称作ID,必须全局唯一,不能重复 SaltStack 读 YAML
配置文件时是从上往下读,所以要把先执行的写在前面
执行状态描述文件
[root@master salt]# salt 'minion' state.sls web.apache.apache
minion:
----------
ID: apache-insall
Function: pkg.installed
Name: httpd
Result: True
Comment: The following packages were installed/updated: httpd
Started: 15:56:52.291205
Duration: 48802.394 ms
Changes:
----------
apr:
----------
new:
1.6.3-12.el8
old:
apr-util:
----------
new:
1.6.1-6.el8
old:
apr-util-bdb:
----------
new:
1.6.1-6.el8
old:
apr-util-openssl:
----------
new:
1.6.1-6.el8
old:
centos-logos-httpd:
----------
new:
85.8-1.el8
old:
httpd:
----------
new:
2.4.37-40.module_el8.5.0+852+0aafc63b
old:
httpd-filesystem:
----------
new:
2.4.37-40.module_el8.5.0+852+0aafc63b
old:
httpd-tools:
----------
new:
2.4.37-40.module_el8.5.0+852+0aafc63b
old:
mod_http2:
----------
new:
1.15.7-3.module_el8.4.0+778+c970deab
old:
----------
ID: apache-service
Function: service.running
Name: httpd
Result: True
Comment: Service httpd has been enabled, and is running
Started: 15:57:41.119739
Duration: 512.161 ms
Changes:
----------
httpd:
True
Summary for minion
------------
Succeeded: 2 (changed=2)
Failed: 0
------------
Total states run: 2
Total run time: 49.315 s
2.2 在Minion上检查
[root@minion ~]# systemctl status httpd
● httpd.service - The Apache HTTP Server
Loaded: loaded (/usr/lib/systemd/system/httpd.service; enabled; vendor preset: disabled)
Active: active (running) since Tue 2021-11-02 15:57:41 CST; 1min 31s ago
Docs: man:httpd.service(8)
由以上内容可知apache确实已部署成功。
执行状态文件的技巧:
- 先用test.ping测试需要执行状态文件的主机是否能正常通信,然后再执行状态文件
2.3 当在其他环境下执行时
当在除base外环境下执行任务时,需在后面跟上“saltenv=测试环境”
[root@master salt]# mv base/web/ test/
[root@master salt]# tree test
test
└── web
└── apache
└── apache.sls
2 directories, 1 file
[root@master salt]# salt 'minion' state.sls web.apache.apache saltenv=test
minion:
----------
ID: apache-insall
Function: pkg.installed
Name: httpd
Result: True
Comment: All specified packages are already installed
Started: 16:03:41.065613
Duration: 1344.854 ms
Changes:
----------
ID: apache-service
Function: service.running
Name: httpd
Result: True
Comment: The service httpd is already running
Started: 16:03:42.412110
Duration: 40.868 ms
Changes:
Summary for minion
------------
Succeeded: 2
Failed: 0
------------
Total states run: 2
Total run time: 1.386 s
3. top file
3.1 top file介绍
直接通过命令执行sls文件时够自动化吗?答案是否定的,因为我们还要告诉某台主机要执行某个任务,自动化应该是我们让它干活时,它自己就知道哪台主机要干什么活,但是直接通过命令执行sls文件并不能达到这个目的,为了解决这个问题,top file 应运而生。
top file就是一个入口,top file的文件名可通过在 Master的配置文件中搜索top.sls找出,且此文件必须在 base 环境中,默认情况下此文件必须叫top.sls。
top file的作用就是告诉对应的主机要干什么活,比如让web服务器启动web服务,让数据库服务器安装mysql等等。
top file 实例:
//检查环境
[root@master base]# salt '*' test.ping
master:
True
node2:
True
minion:
True
//创建两个需要安装的任务
[root@master base]# mkdir -p database/mariadb
[root@master base]# vim database/mariadb/install.sls
mysql-install:
pkg.installed:
- name: mariadb-server
mysql-service:
service.running:
- name: mariadb
- enable: Ture
[root@master base]# mkdir -p web/apache
[root@master base]# vim web/apache/install.sls
apache-insall:
pkg.installed:
- name: httpd
apache-service:
service.running:
- name: httpd
- enable: True
[root@master base]# tree
.
├── database
│ └── mariadb
│ └── install.sls
└── web
└── apache
└── install.sls
4 directories, 2 files
[root@master base]# vim top.sls //创建top文件
base: //要执行状态文件的环境
minion: //要执行状态文件的目标
- web.apache.install //要执行的状态文件
node2:
- database.mariadb.install
[root@master base]# tree
.
├── database
│ └── mariadb
│ └── install.sls
├── top.sls
└── web
└── apache
└── install.sls
4 directories, 3 files
[root@master base]# salt '*' state.highstate //使用高级状态来执行
master:
----------
ID: states
Function: no.None //在top file里没找到它要干啥
Result: False
Comment: No Top file or master_tops data matches found. Please see master log for details.
Changes:
Summary for master
------------
Succeeded: 0
Failed: 1
------------
Total states run: 1
Total run time: 0.000 ms
minion:
----------
ID: apache-insall
Function: pkg.installed
Name: httpd
Result: True
Comment: The following packages were installed/updated: httpd
Started: 16:44:02.095545
Duration: 32090.535 ms
Changes:
----------
apr:
----------
new:
1.6.3-12.el8
old:
apr-util:
----------
new:
1.6.1-6.el8
old:
apr-util-bdb:
----------
new:
1.6.1-6.el8
old:
apr-util-openssl:
----------
new:
1.6.1-6.el8
old:
centos-logos-httpd:
----------
new:
85.8-1.el8
old:
httpd:
----------
new:
2.4.37-40.module_el8.5.0+852+0aafc63b
old:
httpd-filesystem:
----------
new:
2.4.37-40.module_el8.5.0+852+0aafc63b
old:
httpd-tools:
----------
new:
2.4.37-40.module_el8.5.0+852+0aafc63b
old:
mod_http2:
----------
new:
1.15.7-3.module_el8.4.0+778+c970deab
old:
----------
ID: apache-service
Function: service.running
Name: httpd
Result: True
Comment: Service httpd has been enabled, and is running
Started: 16:44:34.200523
Duration: 416.931 ms
Changes:
----------
httpd:
True
Summary for minion
------------
Succeeded: 2 (changed=2)
Failed: 0
------------
Total states run: 2
Total run time: 32.507 s
node2:
----------
ID: mysql-install
Function: pkg.installed
Name: mariadb-server
Result: True
Comment: The following packages were installed/updated: mariadb-server
Started: 16:44:02.154907
Duration: 73259.03 ms
Changes:
----------
mariadb:
----------
new:
3:10.3.28-1.module_el8.3.0+757+d382997d
old:
mariadb-backup:
----------
new:
3:10.3.28-1.module_el8.3.0+757+d382997d
old:
mariadb-common:
----------
new:
3:10.3.28-1.module_el8.3.0+757+d382997d
old:
mariadb-connector-c:
----------
new:
3.1.11-2.el8_3
old:
mariadb-connector-c-config:
----------
new:
3.1.11-2.el8_3
old:
mariadb-errmsg:
----------
new:
3:10.3.28-1.module_el8.3.0+757+d382997d
old:
mariadb-gssapi-server:
----------
new:
3:10.3.28-1.module_el8.3.0+757+d382997d
old:
mariadb-server:
----------
new:
3:10.3.28-1.module_el8.3.0+757+d382997d
old:
mariadb-server-utils:
----------
new:
3:10.3.28-1.module_el8.3.0+757+d382997d
old:
perl-DBD-MySQL:
----------
new:
4.046-3.module_el8.3.0+419+c2dec72b
old:
----------
ID: mysql-service
Function: service.running
Name: mariadb
Result: True
Comment: Service mariadb is already disabled, and is running
Started: 16:45:15.433392
Duration: 359.886 ms
Changes:
----------
mariadb:
True
Summary for node2
------------
Succeeded: 2 (changed=2)
Failed: 0
------------
Total states run: 2
Total run time: 73.619 s
ERROR: Minions returned with non-zero exit code
// 查看另外两个客户端服务是否安装
[root@minion ~]# systemctl status httpd
● httpd.service - The Apache HTTP Server
Loaded: loaded (/usr/lib/systemd/system/httpd.service; enabled; vendor preset: disabled)
Active: active (running) since Tue 2021-11-02 16:44:34 CST; 11min ago
Docs: man:httpd.service(8)
[root@localhost ~]# systemctl status mariadb.service
● mariadb.service - MariaDB 10.3 database server
Loaded: loaded (/usr/lib/systemd/system/mariadb.service; disabled; vendor preset: disabled)
Active: active (running) since Tue 2021-11-02 16:45:15 CST; 10min ago
Docs: man:mysqld(8)
注意:
若top file里面的目标是用 * 表示的,要注意的是,top file里面的 * 表示的是所有要执行状态的目标,而 salt ‘*’ state.highstate 里面的 * 表示通知所有机器干活,而是否要干活则是由top file来指定的
3.2 高级状态highstate的使用
管理SaltStack时一般最常用的管理操作就是执行高级状态
[root@master ~]# salt '*' state.highstate //生产环境禁止这样使用salt命令
注意:
上面让所有人执行高级状态,但实际工作当中,一般不会这么用,工作当中一般都是通知某台或某些台目标主机来执行高级状态,具体是否执行则是由top file来决定的。
若在执行高级状态时加上参数test=True,则它会告诉我们它将会做什么,但是它不会真的去执行这个操作。
//停掉minon上的httpd服务
[root@minion ~]# systemctl stop httpd.service
[root@minion ~]# systemctl status httpd
● httpd.service - The Apache HTTP Server
Loaded: loaded (/usr/lib/systemd/system/httpd.service; enabled; vendor preset: disabled)
Active: inactive (dead) since Tue 2021-11-02 17:13:56 CST; 6s ago
Docs: man:httpd.service(8)
Process: 250275 ExecStart=/usr/sbin/httpd $OPTIONS -DFOREGROUND (code=exited, status=0/SUCCE>
Main PID: 250275 (code=exited, status=0/SUCCESS)
Status: "Running, listening on: port 80"
//在master上执行高级状态的测试
[root@master base]# salt 'minion' state.highstate test=True
minion:
----------
ID: apache-insall
Function: pkg.installed
Name: httpd
Result: True
Comment: All specified packages are already installed
Started: 17:15:21.012368
Duration: 1406.057 ms
Changes:
----------
ID: apache-service
Function: service.running
Name: httpd
Result: None
Comment: Service httpd is set to start
Started: 17:15:22.420278
Duration: 49.561 ms
Changes:
Summary for minion
------------
Succeeded: 2 (unchanged=1)
Failed: 0
------------
Total states run: 2
Total run time: 1.456 s
//在minion上查看httpd是否启动
[root@minion ~]# systemctl status httpd
● httpd.service - The Apache HTTP Server
Loaded: loaded (/usr/lib/systemd/system/httpd.service; enabled; vendor preset: disabled)
Active: inactive (dead) since Tue 2021-11-02 17:13:56 CST; 2min 8s ago
Docs: man:httpd.service(8)
Process: 250275 ExecStart=/usr/sbin/httpd $OPTIONS -DFOREGROUND (code=exited, status=0/SUCCE>
Main PID: 250275 (code=exited, status=0/SUCCESS)
Status: "Running, listening on: port 80"
//由此可见高级状态并没有执行,因为httpd并没有启动