一聚教程网:一个值得你收藏的教程网站

最新下载

热门教程

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

时间:2016-09-16 编辑:简简单单 来源:一聚教程网

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

热门栏目