百分百源码网-让建站变得如此简单! 登录 注册 签到领金币!

主页 | 如何升级VIP | TAG标签

当前位置: 主页>网站教程>服务器> linux中使用salt 批量安装apache服务
分享文章到:

linux中使用salt 批量安装apache服务

发布时间:01/15 来源: 浏览: 关键词:
使用salt 批量安装apache服务要如何来配置安装,下面我们来看一篇安装配置的过程,希望这篇文章能够给大家带来帮助。

top.sls 是配置管理的入口文件,一切都是从这里开始,在master 主机上,默认存放在/srv/salt/目录. top.sls 默认从 base 标签开始解析执行,下一级是操作的目标,可以通过正则,grain模块,或分组名,来进行匹配,再下一级是要执行的state文件,不包换扩展名。

一、创建 /srv/salt/top.sls

[root@localhost salt]# cat top.sls
base:
  '*':
    - apache
二、定义pillar文件

[root@localhost pillar]# cat apache/init.sls
apache:
  {% if grains.id == "salt-minion-01" %}
  port: 8090
  {% elif grains.id == "salt-minion-02" %}
  port: 8093
  {% else %}
  port:80
  {% endif %}
[root@localhost pillar]# pwd
/srv/pillar
#刷新pillar数据
[root@localhost pillar]# salt '*' saltutil.refresh_pillar
salt '*' saltutil.refresh_pillar
salt-minion-01:
    True
salt-minion-02:
    True
#查看pillar中定义的所有的值
[root@localhost salt]# salt '*' pillar.data
salt-minion-01:
    ----------
    apache:
        ----------
        port:
            8090
salt-minion-02:
    ----------
    apache:
        ----------
        port:
            8093
#只是查看apache 的值
[root@localhost salt]# salt '*' pillar.get apache
salt-minion-02:
    ----------
    port:
        8093
salt-minion-01:
    ----------
    port:
        8090
[root@localhost salt]#
pillar VS grains

   pillar是在master上定义变量,grains是获取minion端的值

三、编写apache文件

[root@localhost salt]# pwd
/srv/salt
[root@localhost salt]# cat apache/init.sls
apache_install:
  pkg:
    - name: httpd
    - installed

  file.managed:
    - source: salt://apache/files/httpd.conf
    - name: /etc/httpd/conf/httpd.conf
    - template: jinja
    - defaults:
        port: {{salt['pillar.get']('apache:port',80)}}
    - require:
      - pkg: apache_install
  service.running:
    - name: httpd
    - enable: True
    - reload: True
    - watch:
      - file: /etc/httpd/conf/httpd.conf
      - pkg: apache_install
apache_install 是id号可以自定义

pkg,file,service 是 salt的 state module

require: 依赖某个state,在运行此state前,先运行依赖的state,依赖可以有多个

watch: 在某个state变化时运行此模

port:{{salt['pillar.get']('apache:port',80)}} 获取pillar中定义的apache port的值

四、修改httpd.conf 的模板文件

blob.png

这样定义完成之后就可以发布了

五、发布到salt-minion节点上去

salt '*' state.highstate
blob.png

补充:

 

  1、 如果要安装多个软件包,启动多个服务。这里我们以安装http和php-fpm软件包为例

[root@localhost apache]# cat init.sls.bak
apache_install:
  pkg:
    - name: httpd
    - pkgs:
      - httpd
      - php-fpm
    - installed

  file.managed:
    - source: salt://apache/files/httpd.conf
    - name: /etc/httpd/conf/httpd.conf
    - template: jinja
    - defaults:
        port: 8029
    - require:
      - pkg: apache_install
  service.running:
    - name: httpd
    - enable: True
    - reload: True
    - watch:
      - file: /etc/httpd/conf/httpd.conf
      - pkg: apache_install
php-fpm:
  service.running:
    - enable: True
    - reload: True
    - watch:
      - pkg: apache_install
2、top.sls 文件不仅仅支持正则还支持分组和grains。

通过正则进行匹配的示例,

base:
  '*':   
  - webserver
通过分组名进行匹配的示例,必须要有 – match: nodegroup

base:
  group1:   
  - match: nodegroup      
  - webserver
通过grain模块匹配的示例,必须要有- match: grain

base:
  'os:Fedora':  
   - match: grai
   - webserver

打赏

打赏

取消

感谢您的支持,我会继续努力的!

扫码支持
扫码打赏,你说多少就多少

打开支付宝扫一扫,即可进行扫码打赏哦

百分百源码网 建议打赏1~10元,土豪随意,感谢您的阅读!

共有5人阅读,期待你的评论!发表评论
昵称: 网址: 验证码: 点击我更换图片
最新评论

本文标签

广告赞助

能出一分力是一分吧!

订阅获得更多模板

本文标签

广告赞助

订阅获得更多模板