Saltstack-02-使用sls剧本批量安装软件

121 阅读2分钟

本文已参与「新人创作礼」活动,一起开启掘金创作之路。

一、目标

如何使用salt批量给客户端安装apahce、vsftpd软件

二、说明

接上一个帖子继续写

slat主配置文件位置:/etc/salt/master

三、修改salt-master的主配置文件,使其能支持sls剧本

1.修改服务器端的主配置文件

vim /etc/salt/master ##----在配置文件的第676行添加如下3行,注意该格式是yaml。 ##----意思是全局剧本基本目录是/srv/salt,当然你可以自定义路径 file_roots: base: - /srv/salt/ 2.创建剧本文件夹

mkdir /srv/salt 3.重启salt-master

systemctl restart salt-master

四、创建安装软件的sls剧本

1、创建安装apache的sls剧本

vim /srv/salt/apache.sls #---名字随便取 apache-pkg: #---salt的安装包模块 pkg.installed: #---安装包名 - names: #---安装httpd和mod_ssl模块 - httpd - mod_ssl #---第二个模块名,随意取 apache-service: #---salt的运行服务模块 service.running: #---运行httpd服务,并且设置开机自启 - name: httpd - enable: True

注释:如果客户端上已经有安装过目标软件了,那么salt将不会安装。

2.创建安装vsftpd的sls剧本

vim /srv/salt/xvsftpd.sls #---剧本名叫什么不重要,随意取 xvsftpd-service: #---执行salt的包安装模块 pkg.installed: #---安装软件的名字 - names: #---安装vsftpd - vsftpd

五、创建总的sls剧本入口top.sls vim /srv/salt/top.sls base: '*': - apache - xvsftpd 注释:

  1. base:刚才在修改salt-master主配置文件的时候就只写了个base段,什么dev,prod都还没写,所以这次写的只对base生效

  2. ‘*’:代表所有base主机

    • apache 代表读取剧本目录里的apache.sls文件
    • xvsftpd 代表读取剧本目录里的xvsftpd.sls文件

六、执行sls剧本

1.(可略)测试剧本是否有问题

salt '*' state.highstate test=True 2.执行sls剧本

salt '*' state.highstate 注释:状态同步。比如本次有的客户端已经安装过了apache,那么本次就不会再给它安装了,仅仅给没有安装httpd的客户端去安装,如果apache是停止的,那么本次也根据剧本里的要求给启动起来。

效果如下:

[root@salt-master ~]# salt '*' state.highstate salt-client2:

      ID: apache-service
Function: pkg.installed
    Name: httpd
  Result: True
 Comment: The following packages were installed/updated: httpd
 Started: 22:27:56.356170
Duration: 18566.519 ms
 Changes:   
          ----------
          apr:
              ----------
              new:
                  1.4.8-7.el7
              old:
          apr-util:
              ----------
              new:
                  1.5.2-6.el7
              old:
          httpd:
              ----------
              new:
                  2.4.6-97.el7.centos.1
              old:
          httpd-tools:
              ----------
              new:
                  2.4.6-97.el7.centos.1
              old:
          mailcap:
              ----------
              new:
                  2.1.41-2.el7
              old:

      ID: apache-service
Function: pkg.installed
    Name: mod_ssl
  Result: True
 Comment: The following packages were installed/updated: mod_ssl
 Started: 22:28:14.981973
Duration: 5879.023 ms
 Changes:   
          ----------
          mod_ssl:
              ----------
              new:
                  1:2.4.6-97.el7.centos.1
              old:

Summary for salt-client2

Succeeded: 2 (changed=2) Failed: 0

Total states run: 2 Total run time: 24.446 s salt-client3:

      ID: apache-service
Function: pkg.installed
    Name: httpd
  Result: True
 Comment: The following packages were installed/updated: httpd
 Started: 22:27:57.436935
Duration: 17519.738 ms
 Changes:   
          ----------
          apr:
              ----------
              new:
                  1.4.8-7.el7
              old:
          apr-util:
              ----------
              new:
                  1.5.2-6.el7
              old:
          httpd:
              ----------
              new:
                  2.4.6-97.el7.centos.1
              old:
          httpd-tools:
              ----------
              new:
                  2.4.6-97.el7.centos.1
              old:
          mailcap:
              ----------
              new:
                  2.1.41-2.el7
              old:

      ID: apache-service
Function: pkg.installed
    Name: mod_ssl
  Result: True
 Comment: The following packages were installed/updated: mod_ssl
 Started: 22:28:14.999958
Duration: 6631.766 ms
 Changes:   
          ----------
          mod_ssl:
              ----------
              new:
                  1:2.4.6-97.el7.centos.1
              old:

Summary for salt-client3

Succeeded: 2 (changed=2)

3.去客户端证实是否安装并启动了apache

rpm -q httpd systemctl status httpd rpm -q vsftpd -------------------kahn-----ok--------------2021年10月31日23:10:19-----------------------