1. 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
YAML的基本规则:
使用缩进来表示层级关系,每层2个空格,禁止使用TAB键
当冒号不是处于最后时,冒号后面必须有一个空格
用 - 表示列表,- 的后面必须有一个空格
用 # 表示注释
YAML配置文件要放到SaltStack让我们放的位置,可以在SaltStack的 Master 配置文件中查找file_roots即可看到。
[root@node1 ~]# vim /etc/salt/master
···
#
file_roots:
base:
- /srv/salt/base
test:
- /srv/salt/test
prod:
- /srv/salt/prod
# The hash_type is the hash to use when discovering the hash of a file on
···
[root@node1 ~]# systemctl restart salt-master
需要注意:
base是默认的位置,如果file_roots只有一个,则base是必备的且必须叫base,不能改名
2. 用SaltStack配置一个apache实例
2.1 在Master上部署sls配置文件并执行
[root@node1 ~]# cd /srv/salt/
[root@node1 salt]# mkdir -p {base,test,prod}
[root@node1 salt]# ls
base prod test
[root@node1 salt]# cd base/
[root@node1 base]# mkdir -p web/apache
[root@node1 base]# tree
.
└── web
└── apache
2 directories, 0 files
[root@node1 base]# cd web/apache/
[root@node1 apache]# vim apache.sls
apache-install:
pkg.installed:
- name: httpd
apache-service:
service.running:
- name: httpd
- enable: True
// YAML 配置文件中顶格写的被称作ID,必须全局唯一,不能重复
// SaltStack 读 YAML 配置文件时是从上往下读,所以要把先执行的写在前面
//执行状态描述文件
[root@node1 ~]# salt '*' state.sls web.apache.apache
node1:
----------
ID: apache-install
Function: pkg.installed
Name: httpd
Result: True
Comment: Package httpd is already installed.
Started: 13:07:13.141316
Duration: 686.007 ms
Changes:
----------
ID: apache-service
Function: service.running
Name: httpd
Result: True
Comment: Service httpd is already enabled, and is in the desired state
Started: 13:07:13.827930
Duration: 221.279 ms
Changes:
Summary
------------
Succeeded: 2
Failed: 0
------------
Total states run: 2
node2:
----------
ID: apache-install
Function: pkg.installed
Name: httpd
Result: True
Comment: Package httpd is already installed.
Started: 13:07:13.141312
Duration: 746.454 ms
Changes:
----------
ID: apache-service
Function: service.running
Name: httpd
Result: True
Comment: Service httpd is already enabled, and is in the desired state
Started: 13:07:13.888345
Duration: 252.284 ms
Changes:
Summary
------------
Succeeded: 2
Failed: 0
------------
Total states run: 2
2.2 在Minion上检查
[root@node2 ~]# rpm -qa|grep httpd
httpd-tools-2.4.6-90.el7.centos.x86_64
httpd-2.4.6-90.el7.centos.x86_64
[root@node2 ~]# 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 三 2020-02-12 12:57:05 CST; 13min ago
Docs: man:httpd(8)
man:apachectl(8)
Main PID: 3309 (httpd)
Status: "Total requests: 0; Current requests/sec: 0; Current traffic: 0 B/sec"
CGroup: /system.slice/httpd.service
├─3309 /usr/sbin/httpd -DFOREGROUND
├─3314 /usr/sbin/httpd -DFOREGROUND
├─3318 /usr/sbin/httpd -DFOREGROUND
├─3319 /usr/sbin/httpd -DFOREGROUND
├─3320 /usr/sbin/httpd -DFOREGROUND
└─3321 /usr/sbin/httpd -DFOREGROUND
2月 12 12:56:40 node2 systemd[1]: Starting The Apache HTTP Server...
2月 12 12:56:55 node2 httpd[3309]: AH00558: httpd: Could not reliably determine the server's fully qua...ssage
2月 12 12:57:05 node2 systemd[1]: Started The Apache HTTP Server.
Hint: Some lines were ellipsized, use -l to show in full.
3. 用SaltStack配置一个samba实例
3.1 在Master上部署sls配置文件并执行
[root@node1 ~]# cd /srv/salt/base/web/
[root@node1 web]# ls
apache
[root@node1 web]# mkdir samba
[root@node1 web]# cd samba/
[root@node1 samba]# vim samba.sls
samba-install:
pkg.installed:
- name: samba
samba-service:
service.running:
- name: smb
- enable: True
//执行状态描述文件
[root@node1 samba]# salt '*' state.sls web.samba.samba
node1:
----------
ID: samba-install
Function: pkg.installed
Name: samba
Result: True
Comment: Package samba is already installed.
Started: 13:27:59.797626
Duration: 672.139 ms
Changes:
----------
ID: samba-service
Function: service.running
Name: smb
Result: True
Comment: Service smb has been enabled, and is running
Started: 13:28:00.470407
Duration: 702.618 ms
Changes:
----------
smb:
True
Summary
------------
Succeeded: 2 (changed=1)
Failed: 0
------------
Total states run: 2
node2:
----------
ID: samba-install
Function: pkg.installed
Name: samba
Result: True
Comment: Package samba is already installed.
Started: 13:27:59.923575
Duration: 724.701 ms
Changes:
----------
ID: samba-service
Function: service.running
Name: smb
Result: True
Comment: Service smb has been enabled, and is running
Started: 13:28:00.648808
Duration: 728.88 ms
Changes:
----------
smb:
True
Summary
------------
Succeeded: 2 (changed=1)
Failed: 0
------------
Total states run: 2
3.2 在Minion上检查
[root@node2 ~]# rpm -qa|grep samba
samba-common-libs-4.9.1-10.el7_7.x86_64
samba-common-tools-4.9.1-10.el7_7.x86_64
samba-client-libs-4.9.1-10.el7_7.x86_64
samba-libs-4.9.1-10.el7_7.x86_64
samba-4.9.1-10.el7_7.x86_64
samba-common-4.9.1-10.el7_7.noarch
[root@node2 ~]# systemctl status smb
● smb.service - Samba SMB Daemon
Loaded: loaded (/usr/lib/systemd/system/smb.service; enabled; vendor preset: disabled)
Active: active (running) since 三 2020-02-12 13:28:01 CST; 1min 1s ago
Docs: man:smbd(8)
man:samba(7)
man:smb.conf(5)
Main PID: 3963 (smbd)
Status: "smbd: ready to serve connections..."
CGroup: /system.slice/smb.service
├─3963 /usr/sbin/smbd --foreground --no-process-group
├─3965 /usr/sbin/smbd --foreground --no-process-group
├─3966 /usr/sbin/smbd --foreground --no-process-group
└─3970 /usr/sbin/smbd --foreground --no-process-group
4.top file
4.1 top file介绍
直接通过命令执行sls文件时够自动化吗?答案是否定的,因为我们还要告诉某台主机要执行某个任务,自动化应该是我们让它干活时,它自己就知道哪台主机要干什么活,但是直接通过命令执行sls文件并不能达到这个目的,为了解决这个问题,top file 应运而生。
top file就是一个入口,top file的文件名可通过在 Master的配置文件中搜索top.sls找出,且此文件必须在 base 环境中,默认情况下此文件必须叫top.sls。
top file的作用就是告诉对应的主机要干什么活,比如让web服务器启动web服务,让数据库服务器安装mysql等等。
[root@node1 base]# vim top.sls
base:
'node1*':
- web.apache.apache
'node2*':
- share.samba.samba
[root@node1 base]# salt '*' state.highstate
node2:
----------
ID: samba-install
Function: pkg.installed
Name: samba
Result: True
Comment: Package samba is already installed.
Started: 12:25:00.765071
Duration: 661.743 ms
Changes:
----------
ID: samba-service
Function: service.running
Name: nmb
Result: True
Comment: Service nmb is already enabled, and is in the desired state
Started: 12:25:01.427415
Duration: 231.025 ms
Changes:
----------
ID: samba-service
Function: service.running
Name: smb
Result: True
Comment: Service smb is already enabled, and is in the desired state
Started: 12:25:01.658663
Duration: 216.018 ms
Changes:
Summary
------------
Succeeded: 3
Failed: 0
------------
Total states run: 3
node1:
----------
ID: samba-install
Function: pkg.installed
Name: samba
Result: True
Comment: Package samba is already installed.
Started: 12:25:00.770215
Duration: 743.533 ms
Changes:
----------
ID: samba-service
Function: service.running
Name: nmb
Result: True
Comment: Service nmb is already enabled, and is in the desired state
Started: 12:25:01.514706
Duration: 236.418 ms
Changes:
----------
ID: samba-service
Function: service.running
Name: smb
Result: True
Comment: Service smb is already enabled, and is in the desired state
Started: 12:25:01.751345
Duration: 213.953 ms
Changes:
Summary
------------
Succeeded: 3
Failed: 0
------------
Total states run: 3
//查看minion
[root@node2 ~]# rpm -qa|grep samba
samba-client-libs-4.9.1-10.el7_7.x86_64
samba-4.9.1-10.el7_7.x86_64
samba-common-4.9.1-10.el7_7.noarch
samba-common-libs-4.9.1-10.el7_7.x86_64
samba-common-tools-4.9.1-10.el7_7.x86_64
samba-libs-4.9.1-10.el7_7.x86_64
[root@node2 ~]# systemctl status smb
● smb.service - Samba SMB Daemon
Loaded: loaded (/usr/lib/systemd/system/smb.service; enabled; vendor preset: disabled)
Active: active (running) since 五 2020-02-14 12:20:14 CST; 9min ago
Docs: man:smbd(8)
man:samba(7)
man:smb.conf(5)
Main PID: 2028 (smbd)
Status: "smbd: ready to serve connections..."
CGroup: /system.slice/smb.service
├─2028 /usr/sbin/smbd --foreground --no-process-group
├─2030 /usr/sbin/smbd --foreground --no-process-group
├─2031 /usr/sbin/smbd --foreground --no-process-group
└─2033 /usr/sbin/smbd --foreground --no-process-group
4.2 高级状态highstate的使用
管理SaltStack时一般最常用的管理操作就是执行高级状态
[root@master ~]# salt '*' state.highstate //生产环境禁止这样使用salt命令
注意:
上面让所有人执行高级状态,但实际工作当中,一般不会这么用,工作当中一般都是通知某台或某些台目标主机来执行高级状态,具体是否执行则是由top file来决定的。
若在执行高级状态时加上参数test=True,则它会告诉我们它将会做什么,但是它不会真的去执行这个操作。
//停掉minon上的samba服务
[root@node2 ~]# systemctl stop smb nmb
[root@node2 ~]# systemctl status smb
● smb.service - Samba SMB Daemon
Loaded: loaded (/usr/lib/systemd/system/smb.service; enabled; vendor preset: disabled)
Active: inactive (dead) since 五 2020-02-14 12:31:13 CST; 10s ago
Docs: man:smbd(8)
man:samba(7)
man:smb.conf(5)
Process: 2028 ExecStart=/usr/sbin/smbd --foreground --no-process-group $SMBDOPTIONS (code=killed, signal=TERM)
Main PID: 2028 (code=killed, signal=TERM)
Status: "smbd: ready to serve connections..."
2月 14 12:20:13 node2 systemd[1]: Starting Samba SMB Daemon...
2月 14 12:20:14 node2 smbd[2028]: [2020/02/14 12:20:14.086061, 0] ../lib/util/become_daemon.c:138(dae...eady)
2月 14 12:20:14 node2 systemd[1]: Started Samba SMB Daemon.
2月 14 12:20:14 node2 smbd[2028]: daemon_ready: STATUS=daemon 'smbd' finished starting up and ready ...tions
2月 14 12:31:13 node2 systemd[1]: Stopping Samba SMB Daemon...
2月 14 12:31:13 node2 systemd[1]: Stopped Samba SMB Daemon.
Hint: Some lines were ellipsized, use -l to show in full.
//在master上执行高级状态的测试
[root@node1 base]# salt '*' state.highstate test=True
node2:
----------
ID: samba-install
Function: pkg.installed
Name: samba
Result: True
Comment: Package samba is already installed.
Started: 12:35:01.292522
Duration: 659.305 ms
Changes:
----------
ID: samba-service
Function: service.running
Name: nmb
Result: None
Comment: Service nmb is set to start
Started: 12:35:01.952372
Duration: 153.21 ms
Changes:
----------
ID: samba-service
Function: service.running
Name: smb
Result: None
Comment: Service smb is set to start
Started: 12:35:02.105796
Duration: 160.567 ms
Changes:
Summary
------------
Succeeded: 3 (unchanged=2)
Failed: 0
------------
Total states run: 3
node1:
----------
ID: samba-install
Function: pkg.installed
Name: samba
Result: True
Comment: Package samba is already installed.
Started: 12:35:01.312157
Duration: 745.623 ms
Changes:
----------
ID: samba-service
Function: service.running
Name: nmb
Result: True
Comment: Service nmb is already enabled, and is in the desired state
Started: 12:35:02.058563
Duration: 233.71 ms
Changes:
----------
ID: samba-service
Function: service.running
Name: smb
Result: True
Comment: Service smb is already enabled, and is in the desired state
Started: 12:35:02.292573
Duration: 202.784 ms
Changes:
Summary
------------
Succeeded: 3
Failed: 0
------------
Total states run: 3
//在minion上查看samba是否启动
[root@node2 ~]# systemctl status smb
● smb.service - Samba SMB Daemon
Loaded: loaded (/usr/lib/systemd/system/smb.service; enabled; vendor preset: disabled)
Active: inactive (dead) since 五 2020-02-14 12:31:13 CST; 3min 56s ago
Docs: man:smbd(8)
man:samba(7)
man:smb.conf(5)
Process: 2028 ExecStart=/usr/sbin/smbd --foreground --no-process-group $SMBDOPTIONS (code=killed, signal=TERM)
Main PID: 2028 (code=killed, signal=TERM)
Status: "smbd: ready to serve connections..."
2月 14 12:20:13 node2 systemd[1]: Starting Samba SMB Daemon...
2月 14 12:20:14 node2 smbd[2028]: [2020/02/14 12:20:14.086061, 0] ../lib/util/become_daemon.c:138(dae...eady)
2月 14 12:20:14 node2 systemd[1]: Started Samba SMB Daemon.
2月 14 12:20:14 node2 smbd[2028]: daemon_ready: STATUS=daemon 'smbd' finished starting up and ready ...tions
2月 14 12:31:13 node2 systemd[1]: Stopping Samba SMB Daemon...
2月 14 12:31:13 node2 systemd[1]: Stopped Samba SMB Daemon.
Hint: Some lines were ellipsized, use -l to show in full.
//由此可见高级状态并没有执行,因为httpd并没有启动