远程执行模块的执行是过程式,而状态是对minion的一种描述和定义,管理人员不需要关系部署任务如何完成的,只需要描述minion的状态描述。
它的核心是写sls(Salt State file)文件,sls文件默认格式为YAML格式,并默认使用jinja模板,jinja是根据django的模板语言发展而来的语言,简单并强大,支持for if等循环语句。salt state主要用来描述系统,服务,配置文件的状态,常常被称为配置管理。
1. sls文件
sls呆滞saltstack status,在salt中首先会用到这种类型文件的结构。仅管它们可以用不同的格式进行渲染,默认的YAML格式是最常用的。多种模板引擎同样可以生成YAML(或其他数据结构),最常用的且默认的是Jinja
saltstack和数据息息相关。YAML是Python中指代字典类型数据结构的序列化格式。说道sls如何编写时,只需要记住它们是一组键值对:每项都有一个唯一的键,引用一个值。值可以是一个单项、一个列表项,也可以是其他键值对。
sls文件中每个小节(stanza)的key称为ID,如果小节内没有明显的声明name属性,那么ID就会作为name。ID必须全局唯一,不可重复,重复会报错。
sls文件可以有多层深度,目录层级深度没有限制。当定义多层深度目录结构是,每一个层级将在sls名后加点(如web/nginx/nginx.ssl对应web.nginx.nginx)
mysql-install: #ID声明,必须唯一 pkg.installed: #state状态声明 - pkgs: #选项声明 - mariadb: #选项列表 - mariadb-server 说明: 一个ID只能出现一次 一个ID下相同模块只能使用一次 一个ID下不可以使用多个不同模块 |
实例
master的配置文件如下
file_roots: base: - /srv/salt/base test: - /srv/salt/test dev: - /srv/salt/dev prod: - /srv/salt/prod [root@salt-master ~]# mkdir -p /srv/salt/{base,test,prod,dev} |
编写sls文件
[root@salt-master ~]# salt-key -L Accepted Keys: 192.168.32.130 192.168.32.135 192.168.32.140 Denied Keys: Unaccepted Keys: Rejected Keys: [root@salt-master ~]# cd /srv/salt/base/ [root@salt-master base]# mkdir -p web/nginx [root@salt-master base]# cd web/nginx/ [root@salt-master nginx]# touch nginx.sls [root@salt-master nginx]# pwd /srv/salt/base/web/nginx [root@salt-master nginx]# vim nginx.sls nginx-install: pkg.installed: - name: nginx [root@salt-master ~]# salt '192.168.32.135' state.sls web.nginx.nginx saltenv=base 192.168.32.135: ---------- ID: nginx-install Function: pkg.installed Name: nginx Result: True Comment: The following packages were installed/updated: nginx Started: 19:55:29.481267 Duration: 21423.191 ms Changes: ---------- centos-indexhtml: ---------- new: 7-9.el7.centos old: ...省略... nginx-mod-stream: ---------- new: 1:1.16.1-1.el7 old: Summary for 192.168.32.135 ------------ Succeeded: 1 (changed=1) Failed: 0 ------------ Total states run: 1 Total run time: 21.423 s #minion端查看是否安装nginx [root@node1 ~]# rpm -qa | grep nginx nginx-mod-stream-1.16.1-1.el7.x86_64 nginx-1.16.1-1.el7.x86_64 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-all-modules-1.16.1-1.el7.noarch |
由以上内容可知apache确实已部署成功。
执行状态文件的技巧:
·先用test.ping测试需要执行状态文件的主机是否能正常通信,然后再执行状态文件
2. top file文件
2.1 top file介绍
top file 文件用于将sls文件拉在一起并指定在哪个环境下应该为哪个minion提供sls文件。
top.sls 文件中的每个key都定义一个环境,一般情况下,定义一个叫base的环境,此环境包含基础设置中的所有minion,定义其他环境包含minion的子集。每个环境包含一组sls文件。
top file一定要放在指定环境下,如base就放在base/top.sls
例如下面的官方示例:
在这个top.sls中,声明了一个base环境,base环境指定了所有minion执行vim、scripts、users state,ID包含web的minion执行apache、python、django state,ID包含db的minion执行mysql state
2.2 高级状态highstate的使用
管理SaltStack时一般最常用的管理操作就是执行高级状态
[root@salt-master ~]# salt '*' state.highstate //生产环境禁止这样使用salt命令 |
注意:
上面让所有人执行高级状态,但实际工作当中,一般不会这么用,工作当中一般都是通知某台或某些台目标主机来执行高级状态,具体是否执行则是由top file来决定的。
若在执行高级状态时加上参数test=True,则它会告诉我们它将会做什么,但是它不会真的去执行这个操作。
[root@salt-master nginx]# pwd /srv/salt/base/web/nginx [root@salt-master nginx]# cat nginx.sls nginx-install: pkg.installed: - name: nginx ngins-server: service.running: - name: nginx - enable: Ture #测试运行 [root@salt-master nginx]# cat /srv/salt/base/top.sls base: '*': - web.nginx.nginx [root@salt-master nginx]# salt '192.168.32.140' state.highstate test=Ture 192.168.32.140: ---------- ID: nginx-install Function: pkg.installed Name: nginx Result: None Comment: The following packages would be installed/updated: nginx Started: 10:29:46.551950 Duration: 548.208 ms Changes: ---------- ID: ngins-service Function: service.running Name: nginx Result: None Comment: Service nginx not present; if created in this state run, it would have been started Started: 10:29:47.101215 Duration: 11.729 ms Changes: Summary for 192.168.32.140 ------------ Succeeded: 2 (unchanged=2) Failed: 0 ------------ Total states run: 2 Total run time: 559.937 ms #正式执行 ...... ...... ---------- ID: ngins-service Function: service.running Name: nginx Result: True Comment: Service nginx is already disabled, and is running Started: 10:30:56.680746 Duration: 93.024 ms Changes: ---------- nginx: True Summary for 192.168.32.140 ------------ Succeeded: 2 (changed=2) Failed: 0 ------------ Total states run: 2 Total run time: 10.732 s #在minion端查看 [root@node2 ~]# ss -tanl State Recv-Q Send-Q Local Address:Port Peer Address:Port LISTEN 0 100 127.0.0.1:25 *:* LISTEN 0 128 *:80 *:* LISTEN 0 128 *:22 *:* LISTEN 0 100 [::1]:25 [::]:* LISTEN 0 128 [::]:80 [::]:* LISTEN 0 128 [::]:22 [::]:* [root@node2 ~]# systemctl status nginx ● nginx.service - The nginx HTTP and reverse proxy server Loaded: loaded (/usr/lib/systemd/system/nginx.service; disabled; vendor preset: disabled) Active: active (running) since Mon 2020-08-17 10:30:56 EDT; 1min 1s ago Process: 10528 ExecStart=/usr/sbin/nginx (code=exited, status=0/SUCCESS) Process: 10523 ExecStartPre=/usr/sbin/nginx -t (code=exited, status=0/SUCCESS) Process: 10521 ExecStartPre=/usr/bin/rm -f /run/nginx.pid (code=exited, status=0/SUCCESS) CGroup: /system.slice/nginx.service ├─10532 nginx: master process /usr/sbin/nginx └─10536 nginx: worker process Aug 17 10:30:56 node2 systemd[1]: Starting The nginx HTTP and reverse proxy server... Aug 17 10:30:56 node2 nginx[10523]: nginx: the configuration file /etc/nginx/nginx.conf syntax is ok Aug 17 10:30:56 node2 nginx[10523]: nginx: configuration file /etc/nginx/nginx.conf test is su...sful Aug 17 10:30:56 node2 systemd[1]: Failed to parse PID from file /run/nginx.pid: Success Aug 17 10:30:56 node2 systemd[1]: Started The nginx HTTP and reverse proxy server. Hint: Some lines were ellipsized, use -l to show in full. |
本文内容不用于商业目的,如涉及知识产权问题,请权利人联系51Testing小编(021-64471599-8017),我们将立即处理