SaltStack之配置管理
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
1.1 YAML的基本规则:
- 使用缩进来表示层级关系,每层2个空格,禁止使用TAB键
- 当冒号不是处于最后时,冒号后面必须有一个空格
- 用 - 表示列表,- 的后面必须有一个空格
- 用 # 表示注释
缩进
YAML使用一个固定的缩进风格表示数据层结构关系。salt需要每个缩进级别使用2个空格。
不要使用table键。
冒号
冒号2边表示key和value
短横线
想要表示列表项,使用一个短横杠加一个空格。多个项使用同样的缩进级别作为同一列表的一部分。
nginx-install:
pkg.installed:
- name: nginx
YAML配置文件要放到SaltStack让我们放的位置,可以在SaltStack的 Master 配置文件中查找file_roots即可看到。
file_roots:
base:
- /srv/salt/base
test:
- /srv/salt/test
dev:
- /srv/salt/dev
...此处省略N行
[root@master ~]# mkdir -p /srv/salt/{base,test,dev}
[root@master ~]# tree /srv/salt/.
/srv/salt/.
├── base
├── dev
└── test
3 directories, 0 files
需要注意:
base是默认的位置,如果file_roots只有一个,则base是必备的且必须叫base,不能改名
2. 用SaltStack配置一个apache实例
2.1 在Master上部署sls配置文件并执行
[root@master ~]# vim /etc/salt/master
file_roots:
base:
- /srv/salt/base
test:
- /srv/salt/test
dev:
- /srv/salt/dev
[root@master ~]# mkdir -p /srv/salt/{base,test,dev}
[root@master ~]# tree /srv/salt
/srv/salt
├── base
├── dev
└── test
3 directories, 0 files
[root@master ~]# systemctl restart salt-master
编写一个sls文件
[root@master ~]# cd /srv/salt/base/
[root@master base]# mkdir -p web/nginx
[root@master base]# tree web
web
└── nginx
1 directory, 0 files
[root@master base]# cd web/nginx/
[root@master nginx]# touch first.sls
[root@master nginx]# vim first.sls
nginx-install:
pkg.installed:
- name: nginx
[root@master ~]# ls /srv/salt/base/web/nginx/
first.sls
[root@master ~]# salt 'minion' state.sls web.nginx.first
minion:
----------
ID: nginx-install
Function: pkg.installed
Name: nginx
Result: True
Comment: The following packages were installed/updated: nginx
Started: 22:04:05.901599
Duration: 17526.621 ms
Changes:
----------
centos-indexhtml:
----------
new:
7-9.el7.centos
old:
dejavu-fonts-common:
----------
new:
2.33-6.el7
old:
dejavu-sans-fonts:
----------
new:
2.33-6.el7
old:
fontconfig:
----------
new:
2.13.0-4.3.el7
old:
fontpackages-filesystem:
----------
new:
1.44-8.el7
old:
freetype:
----------
new:
2.8-14.el7
old:
2.4.11-15.el7
gd:
----------
new:
2.0.35-26.el7
old:
gperftools-libs:
----------
new:
2.6.1-1.el7
old:
libX11:
----------
new:
1.6.7-2.el7
old:
libX11-common:
----------
new:
1.6.7-2.el7
old:
libXau:
----------
new:
1.0.8-2.1.el7
old:
libXpm:
----------
new:
3.5.12-1.el7
old:
libjpeg-turbo:
----------
new:
1.2.90-8.el7
old:
libpng:
----------
new:
2:1.5.13-7.el7_2
old:
libxcb:
----------
new:
1.13-1.el7
old:
nginx:
----------
new:
1:1.16.1-1.el7
old:
nginx-all-modules:
----------
new:
1:1.16.1-1.el7
old:
nginx-filesystem:
----------
new:
1:1.16.1-1.el7
old:
nginx-mod-http-image-filter:
----------
new:
1:1.16.1-1.el7
old:
nginx-mod-http-perl:
----------
new:
1:1.16.1-1.el7
old:
nginx-mod-http-xslt-filter:
----------
new:
1:1.16.1-1.el7
old:
nginx-mod-mail:
----------
new:
1:1.16.1-1.el7
old:
nginx-mod-stream:
----------
new:
1:1.16.1-1.el7
old:
Summary for minion
------------
Succeeded: 1 (changed=1)
Failed: 0
------------
Total states run: 1
Total run time: 17.527 s
2.2 在minion上验证
[root@minion ~]# rpm -qa | grep nginx
nginx-mod-stream-1.16.1-1.el7.x86_64
nginx-all-modules-1.16.1-1.el7.noarch
nginx-filesystem-1.16.1-1.el7.noarch
nginx-mod-http-perl-1.16.1-1.el7.x86_64
nginx-mod-http-xslt-filter-1.16.1-1.el7.x86_64
nginx-mod-http-image-filter-1.16.1-1.el7.x86_64
nginx-mod-mail-1.16.1-1.el7.x86_64
nginx-1.16.1-1.el7.x86_64
2.3 设置开机自启
[root@master ~]# vim /etc/salt/master
nginx-install:
pkg.installed:
- name: nginx
nginx-service:
service.running:
- name: nginx
- enable: True
##指定使用那个sls文件
[root@master ~]# salt 'minion' state.sls web.nginx.first saltenv=base
minion:
----------
ID: nginx-install
Function: pkg.installed
Name: nginx
Result: True
Comment: All specified packages are already installed
Started: 22:10:02.910247
Duration: 523.662 ms
Changes:
----------
ID: nginx-service
Function: service.running
Name: nginx
Result: True
Comment: Service nginx has been enabled, and is running
Started: 22:10:03.434675
Duration: 184.325 ms
Changes:
----------
nginx:
True
Summary for minion
------------
Succeeded: 2 (changed=1)
Failed: 0
------------
Total states run: 2
Total run time: 707.987 ms
3. top file
[root@master ~]# cat /srv/salt/base/web/apache/apache-install.sls
apache-install:
pkg.installed:
- name: httpd
[root@master ~]# cat /srv/salt/prod/web/apache/apache-service.sls
apache-service:
service.running:
- name: httpd
- enable: True
[root@master ~]# cd /srv/salt/base/
[root@master base]# vim top.sls
base:
'192.168.30.245':
- web.apache.apache-install
prod:
'192.168.30.245':
- web.apache.apache-service
[root@master ~]# salt '*' state.highstate
192.168.30.245:
----------
ID: apache-install
Function: pkg.installed
Name: httpd
Result: True
Comment: The following packages were installed/updated: httpd
Started: 20:40:09.341752
Duration: 6317.242 ms
Changes:
----------
apr:
----------
new:
1.4.8-3.el7
old:
apr-util:
----------
new:
1.5.2-6.el7
old:
httpd:
----------
new:
2.4.6-67.el7
old:
httpd-tools:
----------
new:
2.4.6-67.el7
old:
mailcap:
----------
new:
2.1.41-2.el7
old:
----------
ID: apache-service
Function: service.running
Name: httpd
Result: True
Comment: Service httpd has been enabled, and is running
Started: 20:40:15.680524
Duration: 278.429 ms
Changes:
----------
httpd:
True
Summary for 192.168.30.245
------------
Succeeded: 2 (changed=2)
Failed: 0
------------
Total states run: 2
Total run time: 6.596 s
注意 :
salt '*' state.highstate
这里的'*'表示 通知各个机器要开始工作了
而具体哪些机器要干什么事情,就需要看top.sls文件里
base:
'192.168.30.245': #这就代表id为192.168.30.245要执行下面的操作,将这里改为*,就代表所有机器都要执行
- web.apache.apache-install
#当然,如果top.sls文件里,没有指定哪些机器要干什么事,
#那salt '*' state.highstate执行后,这个机器就什么都不需要做
salt '192.168.30.245' state.highstate
#这就只会执行id为192.168.30.245要进行的状态
若top file里面的目标是用 * 表示的,要注意的是,top file里面的 * 表示的是所有要执行状态的目标,
而 salt '*' state.highstate 里面的 * 表示通知所有机器干活,而是否要干活则是由top file来指定的
验证
3.1 高级状态highstate的使用
管理SaltStack时一般最常用的管理操作就是执行高级状态
[root@master ~]# salt '*' state.highstate //生产环境禁止这样使用salt命令
注意
上面让所有人执行高级状态,但实际工作当中,一般不会这么用,工作当中一般都是通知某台或某些台目标主机来执行高级状态,具体是否执行则是由top file来决定的。
若在执行高级状态时加上参数test=True
,则它会告诉我们它将会做什么,但是它不会真的去执行这个操作。
[root@master ~]# salt '*' state.highstate test=Ture
192.168.30.245:
----------
ID: mariadb-install
Function: pkg.installed
Name: mariadb
Result: None
Comment: The following packages would be installed/updated: mariadb
Started: 21:10:22.708428
Duration: 405.146 ms
Changes:
----------
ID: mariadb-service
Function: service.running
Name: mariadb
Result: None
Comment: Service mariadb not present; if created in this state run, it would have been started
Started: 21:10:23.114212
Duration: 17.104 ms
Changes:
Summary for 192.168.30.245
------------
Succeeded: 2 (unchanged=2) ##未改变
Failed: 0
------------
Total states run: 2
Total run time: 422.250 ms