YAML语言
YAML
是一种直观的能够被电脑识别的数据序列化格式,是一个可读性高并且容易被人类阅读,容易和脚本语言交互,用来表达资料序列的编程语言。
它类似于标准通用标记语言的子集XML的数据描述语言,语法比XML简单很多。
YAML
语言的格式如下:
house:
family:
name: Doe
parents:
- John
- Jane
children:
- Paul
- Mark
- Simone
address:
number: 34
street: Main Street
city: Nowheretown
zipcode: 12345
123456789101112131415
YAML的基本规则:
- 使用缩进来表示层级关系,每层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/salt/
/srv/salt/
├── base
├── dev
├── prod
└── test
4 directories, 0 files
[root@master ~]# systemctl restart salt-master
需要注意:
- base是默认的位置,如果file_roots只有一个,则base是必备的且必须叫base,不能改名
用SaltStack配置一个httpd实例
在Master上部署sls配置文件并执行
[root@master ~]# mkdir -p /srv/salt/base
[root@master ~]# cd /srv/salt/base/
[root@master base]# mkdir -p web/apache
[root@master base]# cd web/apache/
[root@master apache]# touch install.sls //生成一个状态描述文件
[root@master apache]# vim install.sls
httpd-install:
pkg.installed:
- name: httpd
httpd-service:
service.running:
- name: httpd
- enable: True
// YAML 配置文件中顶格写的被称作ID,必须全局唯一,不能重复
// SaltStack 读 YAML 配置文件时是从上往下读,所以要把先执行的写在前面
执行状态文件的技巧:
先用test.ping测试需要执行状态文件的主机是否能正常通信,然后再执行状态文件
[root@master ~]# salt 'minion' state.sls web.apache.install saltenv=base
minion:
----------
ID: httpd-install
Function: pkg.installed
Name: httpd
Result: True
Comment: All specified packages are already installed
Started: 02:49:12.849577
Duration: 1055.507 ms
Changes:
----------
ID: httpd-service
Function: service.running
Name: httpd
Result: True
Comment: The service httpd is already running
Started: 02:49:13.909687
Duration: 48.368 ms
Changes:
Summary for minion
------------
Succeeded: 2
Failed: 0
------------
Total states run: 2
Total run time: 1.104 s
在minion上检查
[root@minion ~]# ss -anlt
State Recv-Q Send-Q Local Address:Port Peer Address:Port
LISTEN 0 128 0.0.0.0:22 0.0.0.0:*
LISTEN 0 128 [::]:22 [::]:*
LISTEN 0 128 *:80 *:*
[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 Wed 2021-11-03 02:07:47 CST; 43min ago
Docs: man:httpd.service(8)
Main PID: 957 (httpd)
Status: "Running, listening on: port 80"
Tasks: 213 (limit: 11300)
Memory: 49.9M
CGroup: /system.slice/httpd.service
├─ 957 /usr/sbin/httpd -DFOREGROUND
├─1052 /usr/sbin/httpd -DFOREGROUND
├─1053 /usr/sbin/httpd -DFOREGROUND
├─1054 /usr/sbin/httpd -DFOREGROUND
└─1055 /usr/sbin/httpd -DFOREGROUND
11月 03 02:07:46 minion systemd[1]: Starting The Apache HTTP Server...
11月 03 02:07:47 minion httpd[957]: AH00558: httpd: Could not reliably determine the server's fu>
11月 03 02:07:47 minion systemd[1]: Started The Apache HTTP Server.
11月 03 02:07:47 minion httpd[957]: Server configured, listening on: port 80
top file
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 ~]# cd /srv/salt/base/
[root@master base]# vim top.sls
[root@master base]# cat top.sls
base:
'minion':
- web.apache.install
//停止p2的httpd服务
[root@minion ~]# systemctl stop httpd.service
//下面这条命令会报错是因为master端的minion没有执行任何操作,不影响结果
[root@master ~]# salt '*' state.highstate
master:
----------
ID: states
Function: no.None
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: httpd-install
Function: pkg.installed
Name: httpd
Result: True
Comment: All specified packages are already installed
Started: 03:31:38.909212
Duration: 604.952 ms
Changes:
----------
ID: httpd-service
Function: service.running
Name: httpd
Result: True
Comment: Service httpd is already enabled, and is running
Started: 03:31:39.516292
Duration: 234.593 ms
Changes:
----------
httpd:
True
Summary for minion
------------
Succeeded: 2 (changed=1)
Failed: 0
------------
Total states run: 2
Total run time: 839.545 ms
//查看p2端的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: active (running) since Wed 2021-11-03 03:31:39 CST; 2min 6s ago
Docs: man:httpd.service(8)
Main PID: 96308 (httpd)
Status: "Running, listening on: port 80"
Tasks: 213 (limit: 11300)
Memory: 27.2M
CGroup: /system.slice/httpd.service
├─96308 /usr/sbin/httpd -DFOREGROUND
├─96656 /usr/sbin/httpd -DFOREGROUND
├─96657 /usr/sbin/httpd -DFOREGROUND
├─96658 /usr/sbin/httpd -DFOREGROUND
└─96659 /usr/sbin/httpd -DFOREGROUND
11月 03 03:31:39 minion systemd[1]: Starting The Apache HTTP Server...
11月 03 03:31:39 minion httpd[96308]: AH00558: httpd: Could not reliably determine the server's >
11月 03 03:31:39 minion systemd[1]: Started The Apache HTTP Server.
11月 03 03:31:49 minion httpd[96308]: Server configured, listening on: port 80
注意:
- 上面让所有人执行高级状态,但实际工作当中,一般不会这么用,工作当中一般都是通知某台或某些台目标主机来执行高级状态,具体是否执行则是由top file来决定的。
- 若在执行高级状态时加上参数
test=True
,则它会告诉我们它将会做什么,但是它不会真的去执行这个操作。
//停掉minion上的httpd服务
[root@minion ~]# systemctl stop httpd.service
[root@minion ~]# systemctl status httpd.service
● httpd.service - The Apache HTTP Server
Loaded: loaded (/usr/lib/systemd/system/httpd.service; enabled; vendor preset: disabled)
Active: inactive (dead) since Wed 2021-11-03 03:35:25 CST; 5s ago
Docs: man:httpd.service(8)
Process: 96308 ExecStart=/usr/sbin/httpd $OPTIONS -DFOREGROUND (code=exited, status=0/SUCCESS)
Main PID: 96308 (code=exited, status=0/SUCCESS)
Status: "Running, listening on: port 80"
11月 03 03:31:39 minion systemd[1]: Starting The Apache HTTP Server...
11月 03 03:31:39 minion httpd[96308]: AH00558: httpd: Could not reliably determine the server's >
11月 03 03:31:39 minion systemd[1]: Started The Apache HTTP Server.
11月 03 03:31:49 minion httpd[96308]: Server configured, listening on: port 80
11月 03 03:35:24 minion systemd[1]: Stopping The Apache HTTP Server...
11月 03 03:35:25 minion systemd[1]: Stopped The Apache HTTP Server.
//在master上执行高级状态的测试
[root@master ~]# salt 'minion' state.highstate test=True
minion:
----------
ID: httpd-install
Function: pkg.installed
Name: httpd
Result: True
Comment: All specified packages are already installed
Started: 03:36:30.711368
Duration: 695.136 ms
Changes:
----------
ID: httpd-service
Function: service.running
Name: httpd
Result: None
Comment: Service httpd is set to start
Started: 03:36:31.408522
Duration: 53.115 ms
Changes:
Summary for minion
------------
Succeeded: 2 (unchanged=1)
Failed: 0
------------
Total states run: 2
Total run time: 748.251 ms
//去minion上查看httpd服务
[root@minion ~]# systemctl status httpd.service
● httpd.service - The Apache HTTP Server
Loaded: loaded (/usr/lib/systemd/system/httpd.service; enabled; vendor preset: disabled)
Active: inactive (dead) since Wed 2021-11-03 03:35:25 CST; 1min 39s ago
Docs: man:httpd.service(8)
Process: 96308 ExecStart=/usr/sbin/httpd $OPTIONS -DFOREGROUND (code=exited, status=0/SUCCESS)
Main PID: 96308 (code=exited, status=0/SUCCESS)
Status: "Running, listening on: port 80"
11月 03 03:31:39 minion systemd[1]: Starting The Apache HTTP Server...
11月 03 03:31:39 minion httpd[96308]: AH00558: httpd: Could not reliably determine the server's >
11月 03 03:31:39 minion systemd[1]: Started The Apache HTTP Server.
11月 03 03:31:49 minion httpd[96308]: Server configured, listening on: port 80
11月 03 03:35:24 minion systemd[1]: Stopping The Apache HTTP Server...
11月 03 03:35:25 minion systemd[1]: Stopped The Apache HTTP Server.
lines 1-14/14 (END)