5.批量安装nginx服务并定时更新配置

编写sls文件
nginx:
  pkg:
    - installed
  service:
    - running
    - enable: True
    - reload: True
    - watch:
      - pkg: nginx
      - file: /etc/nginx/nginx.conf
      - file: /etc/nginx/conf.d/default.conf
/etc/nginx/nginx.conf:
  file.managed:
    - source: salt://config_file/nginx/nginx.conf
    - user: root
    - group: root
    - mode: 644
/etc/nginx/conf.d/default.conf:
  file.managed:
    - source: salt://config_file/nginx/conf.d/default.conf
    - user: root
    - group: root
    - mode: 644

执行sls
[root@salt-master salt]# salt 'salt-minion' state.sls sls_file.nginx
salt-minion:
----------
          ID: nginx
    Function: pkg.installed
      Result: True
     Comment: The following packages were installed/updated: nginx
     Started: 16:39:49.979913
    Duration: 5106.412 ms
     Changes:   
              ----------
              nginx:
                  ----------
                  new:
                      1.0.15-12.el6
                  old:
----------
          ID: /etc/nginx/nginx.conf
    Function: file.managed
      Result: True
     Comment: File /etc/nginx/nginx.conf is in the correct state
     Started: 16:39:55.093844
    Duration: 8.029 ms
     Changes:   
----------
          ID: /etc/nginx/conf.d/default.conf
    Function: file.managed
      Result: True
     Comment: File /etc/nginx/conf.d/default.conf updated
     Started: 16:39:55.101953
    Duration: 7.596 ms
     Changes:   
              ----------
              diff:
                  ---  
                  +++  
                  @@ -2,7 +2,7 @@
                   # The default server
                   #
                   server {
                  -    listen       80 default_server;
                  +    listen       8080 default_server;
                       server_name  _;
                   
                       #charset koi8-r;
----------
          ID: nginx
    Function: service.running
      Result: True
     Comment: Service nginx has been enabled, and is running
     Started: 16:39:55.109722
    Duration: 247.775 ms
     Changes:   
              ----------
              nginx:
                  True

Summary
------------
Succeeded: 4 (changed=3)
Failed:    0
------------
Total states run:     4
[root@salt-master salt]# salt 'salt-minion' cmd.run 'ps -ef|grep nginx'
salt-minion:
    root       6535      1  0 16:39 ?        00:00:00 nginx: master process /usr/sbin/nginx -c /etc/nginx/nginx.conf
    nginx      6538   6535  0 16:39 ?        00:00:00 nginx: worker process                   
    root       6563   6562  0 16:40 ?        00:00:00 /bin/bash -c ps -ef|grep nginx
    root       6565   6563  0 16:40 ?        00:00:00 grep nginx
[root@salt-master salt]# salt 'salt-minion' cmd.run 'netstat -anutlp|grep nginx'
salt-minion:
    tcp        0      0 0.0.0.0:8080                0.0.0.0:*                   LISTEN      6535/nginx          
    tcp        0      0 0.0.0.0:80                  0.0.0.0:*                   LISTEN      6535/nginx
这样可以看到nginx已经安装完毕并启动
修master上的配置文件并修改端口
[root@salt-master salt]# salt 'salt-minion' state.sls sls_file.nginx
salt-minion:
----------
          ID: nginx
    Function: pkg.installed
      Result: True
     Comment: Package nginx is already installed.
     Started: 16:42:15.191730
    Duration: 256.728 ms
     Changes:   
----------
          ID: /etc/nginx/nginx.conf
    Function: file.managed
      Result: True
     Comment: File /etc/nginx/nginx.conf is in the correct state
     Started: 16:42:15.450801
    Duration: 2.926 ms
     Changes:   
----------
          ID: /etc/nginx/conf.d/default.conf
    Function: file.managed
      Result: True
     Comment: File /etc/nginx/conf.d/default.conf updated
     Started: 16:42:15.453807
    Duration: 7.091 ms
     Changes:   
              ----------
              diff:
                  ---  
                  +++  
                  @@ -2,7 +2,7 @@
                   # The default server
                   #
                   server {
                  -    listen       8080 default_server;
                  +    listen       8089 default_server;
                       server_name  _;
                   
                       #charset koi8-r;
----------
          ID: nginx
    Function: service.running
      Result: True
     Comment: Service reloaded
     Started: 16:42:15.488612
    Duration: 52.676 ms
     Changes:   
              ----------
              nginx:
                  True

Summary
------------
Succeeded: 4 (changed=2)
Failed:    0
------------
Total states run:     4
[root@salt-master salt]# salt 'salt-minion' cmd.run 'netstat -anutlp|grep nginx'
salt-minion:
    tcp        0      0 0.0.0.0:80                  0.0.0.0:*                   LISTEN      6535/nginx          
    tcp        0      0 0.0.0.0:8089                0.0.0.0:*                   LISTEN      6535/nginx
可以看到端口修改生效

第二种执行方式,在minion端主动拉取
[root@salt-minion salt]# salt-call state.sls sls_file.nginx
[INFO    ] Loading fresh modules for state activity
[INFO    ] Fetching file from saltenv 'base', ** skipped ** latest already in cache 'salt://sls_file/nginx.sls'
[INFO    ] Running state [nginx] at time 16:44:14.631980
[INFO    ] Executing state pkg.installed for nginx
[INFO    ] Executing command ['rpm', '-qa', '--queryformat', '%{NAME}_|-%{EPOCH}_|-%{VERSION}_|-%{RELEASE}_|-%{ARCH}_|-(none)\n'] in directory '/root'
[INFO    ] Package nginx is already installed.
[INFO    ] Completed state [nginx] at time 16:44:14.906126
[INFO    ] Running state [/etc/nginx/nginx.conf] at time 16:44:14.909161
[INFO    ] Executing state file.managed for /etc/nginx/nginx.conf
[INFO    ] File /etc/nginx/nginx.conf is in the correct state
[INFO    ] Completed state [/etc/nginx/nginx.conf] at time 16:44:14.912211
[INFO    ] Running state [/etc/nginx/conf.d/default.conf] at time 16:44:14.912364
[INFO    ] Executing state file.managed for /etc/nginx/conf.d/default.conf
[INFO    ] Fetching file from saltenv 'base', ** done ** 'config_file/nginx/conf.d/default.conf'
[INFO    ] File changed:
---  
+++  
@@ -2,7 +2,7 @@
 # The default server
 #
 server {
-    listen       8089 default_server;
+    listen       8088 default_server;
     server_name  _;
 
     #charset koi8-r;

[INFO    ] Completed state [/etc/nginx/conf.d/default.conf] at time 16:44:14.922370
[INFO    ] Running state [nginx] at time 16:44:14.922602
[INFO    ] Executing state service.running for nginx
[INFO    ] Executing command '/sbin/service nginx status' in directory '/root'
[INFO    ] Executing command '/sbin/chkconfig --list nginx' in directory '/root'
[INFO    ] Executing command '/sbin/runlevel' in directory '/root'
[INFO    ] Service nginx is already enabled, and is in the desired state
[INFO    ] Completed state [nginx] at time 16:44:14.963950
[INFO    ] Running state [nginx] at time 16:44:14.964159
[INFO    ] Executing state service.mod_watch for nginx
[INFO    ] Executing command '/sbin/service nginx status' in directory '/root'
[INFO    ] Executing command '/sbin/service nginx reload' in directory '/root'
[INFO    ] {'nginx': True}
[INFO    ] Completed state [nginx] at time 16:44:15.024064
local:
----------
          ID: nginx
    Function: pkg.installed
      Result: True
     Comment: Package nginx is already installed.
     Started: 16:44:14.631980
    Duration: 274.146 ms
     Changes:   
----------
          ID: /etc/nginx/nginx.conf
    Function: file.managed
      Result: True
     Comment: File /etc/nginx/nginx.conf is in the correct state
     Started: 16:44:14.909161
    Duration: 3.05 ms
     Changes:   
----------
          ID: /etc/nginx/conf.d/default.conf
    Function: file.managed
      Result: True
     Comment: File /etc/nginx/conf.d/default.conf updated
     Started: 16:44:14.912364
    Duration: 10.006 ms
     Changes:   
              ----------
              diff:
                  ---  
                  +++  
                  @@ -2,7 +2,7 @@
                   # The default server
                   #
                   server {
                  -    listen       8089 default_server;
                  +    listen       8088 default_server;
                       server_name  _;
                   
                       #charset koi8-r;
----------
          ID: nginx
    Function: service.running
      Result: True
     Comment: Service reloaded
     Started: 16:44:14.964159
    Duration: 59.905 ms
     Changes:   
              ----------
              nginx:
                  True

Summary
------------
Succeeded: 4 (changed=2)
Failed:    0
------------
Total states run:     4


[root@salt-master salt]# salt 'salt-minion' cmd.run 'netstat -anutlp|grep nginx'
salt-minion:
    tcp        0      0 0.0.0.0:80                  0.0.0.0:*                   LISTEN      6535/nginx          
    tcp        0      0 0.0.0.0:8088                0.0.0.0:*                   LISTEN      6535/nginx
可以看到也是生效的

首先打开pillar
pillar_roots:
  base:
    - /srv/pillar
创建相应目录
mkdir /srv/pillar/nginx

编写top.sls
cd /srv/pillar
必须要有top.sls
[root@salt-master pillar]# vim top.sls 

base:
  'salt-minion':
    - nginx.nginx

编写nginx.sls
[root@salt-master pillar]# vim nginx/nginx.sls 

schedule:
  nginx:
    function: state.sls
    minutes: 1
    args:
      - 'nginx'

[root@salt-master pillar]# salt 'salt-minion' pillar.data
salt-minion:
    ----------
    schedule:
        ----------
        nginx:
            ----------
            args:
                - nginx
            function:
                state.sls
            minutes:
                1

修改配置文件
[root@salt-master pillar]# vim ../salt/config_file/nginx/conf.d/default.conf
[root@salt-master pillar]# salt 'salt-minion' saltutil.refresh_pillar
salt-minion:
    True 
[root@salt-master pillar]# salt 'salt-minion' cmd.run 'netstat -anutlp|grep nginx'
salt-minion:
    tcp        0      0 0.0.0.0:80                  0.0.0.0:*                   LISTEN      6535/nginx          
    tcp        0      0 0.0.0.0:8088                0.0.0.0:*                   LISTEN      6535/nginx    
等待一分钟
以上需要注意的是具体应用的sls需要和实际命令一致
例如:
salt 'salt-minion' state.sls sls_file.nginx
schedule:
  nginx:
    function: state.sls
    minutes: 1
    args:
      - 'nginx'
就应该为以下内容
schedule:
  nginx:
    function: state.sls
    minutes: 1#seconds: 1
    args:
      - 'sls_file.nginx'  
top.sls为一个路由文件
base:
  'salt-minion':
    - nginx.nginx
这样就会每分钟或者每秒钟执行
salt 'salt-minion' state.sls sls_file.nginx这条指令

转载于:https://my.oschina.net/eddylinux/blog/704970

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值