利用自动化运维工具saltstack配置nginx服务(2)

本篇博客单独摘录出来作为续篇进行实验,实验环境:https://blog.csdn.net/aaaaaab_/article/details/81750299

server1:172.25.38.1 salt-masterserver3:172.25.38.3 salt-minionnginx端)
SLS(代表SaLt State文件)是Salt State系统的核心。SLS描述了系统的目标状态,由格式简单
的数据构成。这经常被称作配置管理
top.sls 是配置管理的入口文件,一切都是从这里开始,在master 主机上,默认存放在/srv/salt/目录.
top.sls 默认从 base 标签开始解析执行,下一级是操作的目标,可以通过正则,grain模块,或分组名,
来进行匹配,再下一级是要执行的state文件,不包换扩展名。

在server1进行部署nginx的准备工作:

[root@server1 salt]# pwd
/srv/salt
[root@server1 salt]# ls
httpd
[root@server1 salt]# mkdir nginx
[root@server1 salt]# cd nginx/
[root@server1 nginx]# ls
[root@server1 nginx]# pwd
/srv/salt/nginx
[root@server1 nginx]# mkdir files
[root@server1 nginx]# cd files/
[root@server1 files]# pwd
/srv/salt/nginx/files
[root@server1 files]# ls
nginx-1.14.0.tar.gz
[root@server1 files]# cd ..
[root@server1 nginx]# pwd
/srv/salt/nginx

利用自动化运维工具saltstack配置nginx服务(2)_第1张图片

[root@server1 nginx]# vim install.sls  安装依赖性
[root@server1 nginx]# cat install.sls 
nginx-install:
  pkg.installed:
    - pkgs:
      - pcre-devel
      - openssl-devel
      - gcc
  file.managed:
    - name: /mnt/nginx-1.14.0.tar.gz
    - source: salt://nginx/files/nginx-1.14.0.tar.gz
[root@server1 nginx]# salt server3 state.sls nginx.install  安装依赖性成功并把包导入了server3的/mnt下

利用自动化运维工具saltstack配置nginx服务(2)_第2张图片
在server3查看/mnt下已经有了tar包,代表:

[root@server3 salt]# cd /mnt/
[root@server3 mnt]# ls
nginx-1.14.0.tar.gz
[root@server3 mnt]#

利用自动化运维工具saltstack配置nginx服务(2)_第3张图片
在server3搭建环境确保命令可以执行写入脚本:

[root@server3 nginx-1.14.0]# vim src/core/nginx.h 
[root@server3 nginx-1.14.0]# sed -i.bak 's/#define NGINX_VER          "nginx\/" NGINX_VERSION/#define NGINX_VER          "nginx"/g' src/core/nginx.h  
[root@server3 nginx-1.14.0]# vim src/core/nginx.h   已经去掉版本号确保安全
[root@server3 nginx-1.14.0]# vim auto/cc/gcc 
[root@server3 nginx-1.14.0]# sed -i.bak 's/CFLAGS="$CFLAGS -g"/#CFLAGS="$CFLAGS -g"/g' auto/cc/gcc 
[root@server3 nginx-1.14.0]# vim auto/cc/gcc 

利用自动化运维工具saltstack配置nginx服务(2)_第4张图片
在server1部署安装nginx:

[root@server1 nginx]# vim install.sls
[root@server1 nginx]# cat install.sls 
nginx-install:
  pkg.installed:
    - pkgs:
      - pcre-devel
      - openssl-devel
      - gcc
  file.managed:
    - name: /mnt/nginx-1.14.0.tar.gz
    - source: salt://nginx/files/nginx-1.14.0.tar.gz
  cmd.run:
    - name: cd /mnt && tar zxf nginx-1.14.0.tar.gz && cd nginx-1.14.0 && sed -i.bak 's/#define NGINX_VER          "nginx\/" NGINX_VERSION/#define NGINX_VER          "nginx"/g' src/core/nginx.h && sed -i.bak 's/CFLAGS="$CFLAGS -g"/#CFLAGS="$CFLAGS -g"/g' auto/cc/gcc && ./configure --prefix=/usr/local/nginx --with-http_ssl_module --with-http_stub_status_module --with-threads --with-file-aio &> /dev/null && make &> /dev/null && make install &> /dev/null
    - creates: /usr/local/nginx    这行表示当系统已经有了nginx文件不进行重复编译安装
[root@server1 nginx]# salt server3 state.sls nginx.install   调用执行远程安装
server3:
----------
          ID: nginx-install
    Function: pkg.installed
      Result: True
     Comment: All specified packages are already installed
     Started: 12:31:14.233676
    Duration: 386.608 ms
     Changes:   
----------
          ID: nginx-install
    Function: file.managed
        Name: /mnt/nginx-1.14.0.tar.gz
      Result: True
     Comment: File /mnt/nginx-1.14.0.tar.gz is in the correct state
     Started: 12:31:14.621982
    Duration: 69.751 ms
     Changes:   
----------
          ID: nginx-install
    Function: cmd.run
        Name: cd /mnt && tar zxf nginx-1.14.0.tar.gz && cd nginx-1.14.0 && sed -i.bak 's/#define NGINX_VER          "nginx\/" NGINX_VERSION/#define NGINX_VER          "nginx"/g' src/core/nginx.h && sed -i.bak 's/CFLAGS="$CFLAGS -g"/#CFLAGS="$CFLAGS -g"/g' auto/cc/gcc && ./configure --prefix=/usr/local/nginx --with-http_ssl_module --with-http_stub_status_module --with-threads --with-file-aio &> /dev/null && make &> /dev/null && make install &> /dev/null
      Result: True
     Comment: Command "cd /mnt && tar zxf nginx-1.14.0.tar.gz && cd nginx-1.14.0 && sed -i.bak 's/#define NGINX_VER          "nginx\/" NGINX_VERSION/#define NGINX_VER          "nginx"/g' src/core/nginx.h && sed -i.bak 's/CFLAGS="$CFLAGS -g"/#CFLAGS="$CFLAGS -g"/g' auto/cc/gcc && ./configure --prefix=/usr/local/nginx --with-http_ssl_module --with-http_stub_status_module --with-threads --with-file-aio &> /dev/null && make &> /dev/null && make install &> /dev/null" run
     Started: 12:31:14.692432
    Duration: 23762.636 ms
     Changes:   
              ----------
              pid:
                  4267
              retcode:
                  0
              stderr:
              stdout:

Summary for server3
------------
Succeeded: 3 (changed=1)
Failed:    0
------------
Total states run:     3
Total run time:  24.219 s

利用自动化运维工具saltstack配置nginx服务(2)_第5张图片
在server1开启服务:重新编写脚本保证一个脚本内容不要繁多不然不好管理:

[root@server1 nginx]# ls
files  install.sls  service.ssl
[root@server1 nginx]# mv service.ssl service.sls 
[root@server1 nginx]# cat service.sls 
include:
  - nginx.install   导入nginx.install脚本

nginx-service:
  cmd.run:
    - name: /usr/local/nginx/sbin/nginx   调用绝对路径打开nginx
[root@server1 nginx]# salt server3 state.sls nginx.service  远程打开服务
server3:
----------
          ID: nginx-install
    Function: pkg.installed
      Result: True
     Comment: All specified packages are already installed
     Started: 13:11:11.491297
    Duration: 385.82 ms
     Changes:   
----------
          ID: nginx-install
    Function: file.managed
        Name: /mnt/nginx-1.14.0.tar.gz
      Result: True
     Comment: File /mnt/nginx-1.14.0.tar.gz is in the correct state
     Started: 13:11:11.878798
    Duration: 68.836 ms
     Changes:   
----------
          ID: nginx-install
    Function: cmd.run
        Name: cd /mnt && tar zxf nginx-1.14.0.tar.gz && cd nginx-1.14.0 && sed -i.bak 's/#define NGINX_VER          "nginx\/" NGINX_VERSION/#define NGINX_VER          "nginx"/g' src/core/nginx.h && sed -i.bak 's/CFLAGS="$CFLAGS -g"/#CFLAGS="$CFLAGS -g"/g' auto/cc/gcc && ./configure --prefix=/usr/local/nginx --with-http_ssl_module --with-http_stub_status_module --with-threads --with-file-aio &> /dev/null && make &> /dev/null && make install &> /dev/null
      Result: True
     Comment: /usr/local/nginx exists
     Started: 13:11:11.948364
    Duration: 0.396 ms
     Changes:   
----------
          ID: nginx-service
    Function: cmd.run
        Name: /usr/local/nginx/sbin/nginx
      Result: True
     Comment: Command "/usr/local/nginx/sbin/nginx" run
     Started: 13:11:11.948852
    Duration: 8.64 ms
     Changes:   
              ----------
              pid:
                  6999
              retcode:
                  0
              stderr:
              stdout:

Summary for server3
------------
Succeeded: 4 (changed=1)
Failed:    0
------------
Total states run:     4
Total run time: 463.692 ms

利用自动化运维工具saltstack配置nginx服务(2)_第6张图片
在server3可以看到nginx的默认端口80开启:

[root@server3 nginx-1.14.0]# netstat -antlp
Active Internet connections (servers and established)
Proto Recv-Q Send-Q Local Address               Foreign Address             State       PID/Program name   
tcp        0      0 0.0.0.0:80                  0.0.0.0:*                   LISTEN      7000/nginx          
tcp        0      0 0.0.0.0:22                  0.0.0.0:*                   LISTEN      879/sshd            
tcp        0      0 127.0.0.1:25                0.0.0.0:*                   LISTEN      955/master          
tcp        0      0 172.25.38.3:50426           172.25.38.1:4505            ESTABLISHED 1699/python2.6      
tcp        0      0 172.25.38.3:22              172.25.38.250:36146         ESTABLISHED 1340/sshd           
tcp        0      0 :::22                       :::*                        LISTEN      879/sshd            
tcp        0      0 ::1:25                      :::*                        LISTEN      955/master          

利用自动化运维工具saltstack配置nginx服务(2)_第7张图片
分享接下来要使用的nginx脚本来直接通过脚本调用nginx服务:

[root@server1 files]# cat nginx   同样将脚本放到srv/salt/nginx/files文件夹下
#!/bin/sh
#
# nginx        Startup script for nginx
#
# chkconfig: - 85 15
# processname: nginx
# config: /usr/local/nginx/conf/nginx/nginx.conf
# pidfile: /usr/local/nginx/logs/nginx.pid
# description: nginx is an HTTP and reverse proxy server
#
### BEGIN INIT INFO
# Provides: nginx
# Required-Start: $local_fs $remote_fs $network
# Required-Stop: $local_fs $remote_fs $network
# Default-Start: 2 3 4 5
# Default-Stop: 0 1 6
# Short-Description: start and stop nginx
### END INIT INFO

# Source function library.
. /etc/rc.d/init.d/functions

if [ -L $0 ]; then
    initscript=`/bin/readlink -f $0`
else
    initscript=$0
fi

#sysconfig=`/bin/basename $initscript`

#if [ -f /etc/sysconfig/$sysconfig ]; then
#    . /etc/sysconfig/$sysconfig
#fi

nginx=${NGINX-/usr/local/nginx/sbin/nginx}
prog=`/bin/basename $nginx`
conffile=${CONFFILE-/usr/local/nginx/conf/nginx.conf}
lockfile=${LOCKFILE-/var/lock/subsys/nginx}
pidfile=${PIDFILE-/usr/local/nginx/logs/nginx.pid}
SLEEPMSEC=${SLEEPMSEC-200000}
UPGRADEWAITLOOPS=${UPGRADEWAITLOOPS-5}
RETVAL=0

start() {
    echo -n $"Starting $prog: "

    daemon --pidfile=${pidfile} ${nginx} -c ${conffile}
    RETVAL=$?
    echo
    [ $RETVAL = 0 ] && touch ${lockfile}
    return $RETVAL
}

stop() {
    echo -n $"Stopping $prog: "
    killproc -p ${pidfile} ${prog}
    RETVAL=$?
    echo
    [ $RETVAL = 0 ] && rm -f ${lockfile} ${pidfile}
}

reload() {
    echo -n $"Reloading $prog: "
    killproc -p ${pidfile} ${prog} -HUP
    RETVAL=$?
    echo
}

upgrade() {
    oldbinpidfile=${pidfile}.oldbin

    configtest -q || return
    echo -n $"Starting new master $prog: "
    killproc -p ${pidfile} ${prog} -USR2
    echo

    for i in `/usr/bin/seq $UPGRADEWAITLOOPS`; do
        /bin/usleep $SLEEPMSEC
        if [ -f ${oldbinpidfile} -a -f ${pidfile} ]; then
            echo -n $"Graceful shutdown of old $prog: "
            killproc -p ${oldbinpidfile} ${prog} -QUIT
            RETVAL=$?
            echo
            return
        fi
    done

    echo $"Upgrade failed!"
    RETVAL=1
}

configtest() {
    if [ "$#" -ne 0 ] ; then
        case "$1" in
            -q)
                FLAG=$1
                ;;
            *)
                ;;
        esac
        shift
    fi
    ${nginx} -t -c ${conffile} $FLAG
    RETVAL=$?
    return $RETVAL
}

rh_status() {
    status -p ${pidfile} ${nginx}
}

# See how we were called.
case "$1" in
    start)
        rh_status >/dev/null 2>&1 && exit 0
        start
        ;;
    stop)
        stop
        ;;
    status)
        rh_status
        RETVAL=$?
        ;;
    restart)
        configtest -q || exit $RETVAL
        stop
        start
        ;;
    upgrade)
        rh_status >/dev/null 2>&1 || exit 0
        upgrade
        ;;
    condrestart|try-restart)
        if rh_status >/dev/null 2>&1; then
            stop
            start
        fi
        ;;
    force-reload|reload)
        reload
        ;;
    configtest)
        configtest
        ;;
    *)
        echo $"Usage: $prog {start|stop|restart|condrestart|try-restart|force-reload|upgrade|reload|status|help|configtest}"
        RETVAL=2
esac

exit $RETVAL

在server1直接调用脚本来调用nginx服务:

[root@server1 files]# pwd
/srv/salt/nginx/files
[root@server1 files]# ls
nginx-1.14.0.tar.gz  nginx.conf
[root@server1 files]# ls
nginx  nginx-1.14.0.tar.gz  nginx.conf
[root@server1 files]# ll
total 1004
-rwxr-xr-x 1 root root    3136 Aug 17 13:30 nginx   这个是脚本自己编写的调用nginx的脚本,脚本必须保证有可执行权限
-rw-r--r-- 1 root root 1016272 Aug 17 11:59 nginx-1.14.0.tar.gz
-rw-r--r-- 1 root root    2656 Aug 17 13:25 nginx.conf  这个是从server3传过来的配置文件
[root@server1 files]# vim /srv/salt/nginx/service.sls 
[root@server1 files]# cat /srv/salt/nginx/service.sls 
include:
  - nginx.install

/usr/local/nginx/conf/nginx.conf:
  file.managed:
    - source: salt://nginx/files/nginx.conf

nginx-service:
  file.managed:
    - name: /etc/init.d/nginx
    - source: salt://nginx/files/nginx
    - mode: 755

  service.running:    
    - name: nginx
    - reload: True    重新加载服务
    - watch:    监控主配置文件,这个文件必须存在不然会报错
      - file: /usr/local/nginx/conf/nginx.conf
[root@server1 files]# salt server3 state.sls nginx.service  推送成功
server3:
----------
          ID: nginx-install
    Function: pkg.installed
      Result: True
     Comment: All specified packages are already installed
     Started: 13:35:29.602435
    Duration: 386.332 ms
     Changes:   
----------
          ID: nginx-install
    Function: file.managed
        Name: /mnt/nginx-1.14.0.tar.gz
      Result: True
     Comment: File /mnt/nginx-1.14.0.tar.gz is in the correct state
     Started: 13:35:29.990500
    Duration: 69.312 ms
     Changes:   
----------
          ID: nginx-install
    Function: cmd.run
        Name: cd /mnt && tar zxf nginx-1.14.0.tar.gz && cd nginx-1.14.0 && sed -i.bak 's/#define NGINX_VER          "nginx\/" NGINX_VERSION/#define NGINX_VER          "nginx"/g' src/core/nginx.h && sed -i.bak 's/CFLAGS="$CFLAGS -g"/#CFLAGS="$CFLAGS -g"/g' auto/cc/gcc && ./configure --prefix=/usr/local/nginx --with-http_ssl_module --with-http_stub_status_module --with-threads --with-file-aio &> /dev/null && make &> /dev/null && make install &> /dev/null
      Result: True
     Comment: /usr/local/nginx exists
     Started: 13:35:30.060523
    Duration: 0.382 ms
     Changes:   
----------
          ID: /usr/local/nginx/conf/nginx.conf
    Function: file.managed
      Result: True
     Comment: File /usr/local/nginx/conf/nginx.conf is in the correct state
     Started: 13:35:30.061002
    Duration: 49.653 ms
     Changes:   
----------
          ID: nginx-service
    Function: file.managed
        Name: /etc/init.d/nginx
      Result: True
     Comment: File /etc/init.d/nginx updated
     Started: 13:35:30.110781
    Duration: 50.872 ms
     Changes:   
              ----------
              diff:
                  New file
              mode:
                  0755
----------
          ID: nginx-service
    Function: service.running
        Name: nginx
      Result: True
     Comment: The service nginx is already running
     Started: 13:35:30.162505
    Duration: 59.073 ms
     Changes:   

Summary for server3
------------
Succeeded: 6 (changed=1)
Failed:    0
------------
Total states run:     6
Total run time: 615.624 ms

在server3可以看到相应的进程:

[root@server3 nginx-1.14.0]# ps ax   有nginx服务正在运行

利用自动化运维工具saltstack配置nginx服务(2)_第8张图片
在server1改变进程数再次推送:

[root@server1 nginx]# vim files/nginx.conf 更改配置文件
[root@server1 nginx]# cat files/nginx.conf | head -n 3

#user  nobody;
worker_processes  2;
[root@server1 nginx]# salt server3 state.sls nginx.service  再次推送
server3:
----------
          ID: nginx-install
    Function: pkg.installed
      Result: True
     Comment: All specified packages are already installed
     Started: 13:53:20.337838
    Duration: 386.904 ms
     Changes:   
----------
          ID: nginx-install
    Function: file.managed
        Name: /mnt/nginx-1.14.0.tar.gz
      Result: True
     Comment: File /mnt/nginx-1.14.0.tar.gz is in the correct state
     Started: 13:53:20.726428
    Duration: 69.658 ms
     Changes:   
----------
          ID: nginx-install
    Function: cmd.run
        Name: cd /mnt && tar zxf nginx-1.14.0.tar.gz && cd nginx-1.14.0 && sed -i.bak 's/#define NGINX_VER          "nginx\/" NGINX_VERSION/#define NGINX_VER          "nginx"/g' src/core/nginx.h && sed -i.bak 's/CFLAGS="$CFLAGS -g"/#CFLAGS="$CFLAGS -g"/g' auto/cc/gcc && ./configure --prefix=/usr/local/nginx --with-http_ssl_module --with-http_stub_status_module --with-threads --with-file-aio &> /dev/null && make &> /dev/null && make install &> /dev/null
      Result: True
     Comment: /usr/local/nginx exists
     Started: 13:53:20.796804
    Duration: 0.366 ms
     Changes:   
----------
          ID: /usr/local/nginx/conf/nginx.conf
    Function: file.managed
      Result: True
     Comment: File /usr/local/nginx/conf/nginx.conf updated
     Started: 13:53:20.797261
    Duration: 51.103 ms
     Changes:   
              ----------
              diff:
                  ---  
                  +++  
                  @@ -1,6 +1,6 @@

                   #user  nobody;
                  -worker_processes  1;
                  +worker_processes  2;

                   #error_log  logs/error.log;
                   #error_log  logs/error.log  notice;
----------
          ID: nginx-service
    Function: file.managed
        Name: /etc/init.d/nginx
      Result: True
     Comment: File /etc/init.d/nginx is in the correct state
     Started: 13:53:20.848494
    Duration: 30.661 ms
     Changes:   
----------
          ID: nginx-service
    Function: service.running
        Name: nginx
      Result: True
     Comment: Service reloaded
     Started: 13:53:20.910413
    Duration: 33.171 ms
     Changes:   
              ----------
              nginx:
                  True

Summary for server3
------------
Succeeded: 6 (changed=2)
Failed:    0
------------
Total states run:     6
Total run time: 571.863 ms

利用自动化运维工具saltstack配置nginx服务(2)_第9张图片
在server3查看进程会多出来nginx进程,由于脚本里面加入了监控配置文件,当配置文件改变会再次调用脚本:

[root@server3 nginx-1.14.0]# ps ax

利用自动化运维工具saltstack配置nginx服务(2)_第10张图片
直接通过脚本推送nginx的相关用户组:

[root@server1 nginx]# vim files/nginx.conf 
[root@server1 nginx]# cat files/nginx.conf | head -n 3

user nginx nginx;    修改配置文件指定nginx的用户组
worker_processes  2;
[root@server1 nginx]# ls
files  install.sls  service.sls
[root@server1 nginx]# cd ..
[root@server1 salt]# mkdir users
[root@server1 salt]# cd users/
[root@server1 users]# ls
nginx.sls
[root@server1 users]# vim nginx.sls 
[root@server1 users]# cat nginx.sls 
nginx-group:
  group.present:
    - name: nginx
    - gid: 800

nginx-user:
  user.present:
    - name: nginx
    - uid: 800
    - gid: 800
    - shell: /sbin/nologin   指定shell
    - createhome: False
    - home: /usr/local/nginx  指定家目录
[root@server1 users]# cd ..
[root@server1 salt]# ls
httpd  nginx  users
[root@server1 salt]# cd nginx/
[root@server1 nginx]# ls
files  install.sls  service.sls
[root@server1 nginx]# cat service.sls 
include:
  - users.nginx   导入用户组脚本
  - nginx.install   导入nginx安装脚本

/usr/local/nginx/conf/nginx.conf:
  file.managed:
    - source: salt://nginx/files/nginx.conf

nginx-service:
  file.managed:
    - name: /etc/init.d/nginx
    - source: salt://nginx/files/nginx
    - mode: 755   指定权限

  service.running:
    - name: nginx
    - reload: True   重载nginx
    - watch:
      - file: /usr/local/nginx/conf/nginx.conf

[root@server1 nginx]# salt server3 state.sls nginx.service   远程推送
server3:
----------
          ID: nginx-group
    Function: group.present
        Name: nginx
      Result: True
     Comment: Group nginx is present and up to date
     Started: 14:03:00.516558
    Duration: 1.71 ms
     Changes:   
----------
          ID: nginx-user
    Function: user.present
        Name: nginx
      Result: True
     Comment: New user nginx created
     Started: 14:03:00.518784
    Duration: 377.813 ms
     Changes:   
              ----------
              fullname:
              gid:
                  800
              groups:
                  - nginx
              home:
              homephone:
              name:
                  nginx
              passwd:
                  x
              roomnumber:
              shell:
                  /sbin/nologin
              uid:
                  800
              workphone:
----------
          ID: nginx-install
    Function: pkg.installed
      Result: True
     Comment: All specified packages are already installed
     Started: 14:03:01.344070
    Duration: 386.81 ms
     Changes:   
----------
          ID: nginx-install
    Function: file.managed
        Name: /mnt/nginx-1.14.0.tar.gz
      Result: True
     Comment: File /mnt/nginx-1.14.0.tar.gz is in the correct state
     Started: 14:03:01.732627
    Duration: 69.411 ms
     Changes:   
----------
          ID: nginx-install
    Function: cmd.run
        Name: cd /mnt && tar zxf nginx-1.14.0.tar.gz && cd nginx-1.14.0 && sed -i.bak 's/#define NGINX_VER          "nginx\/" NGINX_VERSION/#define NGINX_VER          "nginx"/g' src/core/nginx.h && sed -i.bak 's/CFLAGS="$CFLAGS -g"/#CFLAGS="$CFLAGS -g"/g' auto/cc/gcc && ./configure --prefix=/usr/local/nginx --with-http_ssl_module --with-http_stub_status_module --with-threads --with-file-aio &> /dev/null && make &> /dev/null && make install &> /dev/null
      Result: True
     Comment: /usr/local/nginx exists
     Started: 14:03:01.802758
    Duration: 0.365 ms
     Changes:   
----------
          ID: /usr/local/nginx/conf/nginx.conf
    Function: file.managed
      Result: True
     Comment: File /usr/local/nginx/conf/nginx.conf is in the correct state
     Started: 14:03:01.803216
    Duration: 30.086 ms
     Changes:   
----------
          ID: nginx-service
    Function: file.managed
        Name: /etc/init.d/nginx
      Result: True
     Comment: File /etc/init.d/nginx is in the correct state
     Started: 14:03:01.833434
    Duration: 30.509 ms
     Changes:   
----------
          ID: nginx-service
    Function: service.running
        Name: nginx
      Result: True
     Comment: The service nginx is already running
     Started: 14:03:01.864866
    Duration: 31.124 ms
     Changes:   

Summary for server3
------------
Succeeded: 8 (changed=1)
Failed:    0
------------
Total states run:     8
Total run time: 927.828 ms

利用自动化运维工具saltstack配置nginx服务(2)_第11张图片
在server3可以查看到指定uid和gid的用户:

[root@server3 nginx-1.14.0]# id nginx
uid=800(nginx) gid=800(nginx) groups=800(nginx)

这里写图片描述
为了方便源码编译其他tar包时需要安装依赖性,将依赖性工具打包:

[root@server1 nginx]# cd ..
[root@server1 salt]# pwd
/srv/salt
[root@server1 salt]# ls
httpd  nginx  users
[root@server1 salt]# mkdir pkgs
[root@server1 salt]# cd pkgs/
[root@server1 pkgs]# ls
[root@server1 pkgs]# vim make.sls  编写工具包脚本
[root@server1 pkgs]# cat make.sls 
make-gcc:
  pkg.installed:
    - pkgs:
      - pcre-devel
      - openssl-devel
      - gcc
[root@server1 pkgs]# ls
make.sls
[root@server1 pkgs]# cd ..
[root@server1 salt]# ls
httpd  nginx  pkgs  users
[root@server1 salt]# cd nginx/
[root@server1 nginx]# ls
files  install.sls  service.sls
[root@server1 nginx]# vim install.sls 
[root@server1 nginx]# cat install.sls 
include:
  - pkgs.make   导入工具包

nginx-install:
  file.managed:
    - name: /mnt/nginx-1.14.0.tar.gz
    - source: salt://nginx/files/nginx-1.14.0.tar.gz

  cmd.run:
    - name: cd /mnt && tar zxf nginx-1.14.0.tar.gz && cd nginx-1.14.0 && sed -i.bak 's/#define NGINX_VER          "nginx\/" NGINX_VERSION/#define NGINX_VER          "nginx"/g' src/core/nginx.h && sed -i.bak 's/CFLAGS="$CFLAGS -g"/#CFLAGS="$CFLAGS -g"/g' auto/cc/gcc && ./configure --prefix=/usr/local/nginx --with-http_ssl_module --with-http_stub_status_module --with-threads --with-file-aio &> /dev/null && make &> /dev/null && make install &> /dev/null
    - creates: /usr/local/nginx
[root@server1 nginx]# salt server3 state.sls nginx.service  远程推送测试工具包,显示推送成功完成了nginx的相关配置
server3:
----------
          ID: nginx-group
    Function: group.present
        Name: nginx
      Result: True
     Comment: Group nginx is present and up to date
     Started: 14:11:29.260727
    Duration: 1.699 ms
     Changes:   
----------
          ID: nginx-user
    Function: user.present
        Name: nginx
      Result: True
     Comment: User nginx is present and up to date
     Started: 14:11:29.262931
    Duration: 20.721 ms
     Changes:   
----------
          ID: make-gcc
    Function: pkg.installed
      Result: True
     Comment: All specified packages are already installed
     Started: 14:11:29.730909
    Duration: 387.122 ms
     Changes:   
----------
          ID: nginx-install
    Function: file.managed
        Name: /mnt/nginx-1.14.0.tar.gz
      Result: True
     Comment: File /mnt/nginx-1.14.0.tar.gz is in the correct state
     Started: 14:11:30.119751
    Duration: 70.578 ms
     Changes:   
----------
          ID: nginx-install
    Function: cmd.run
        Name: cd /mnt && tar zxf nginx-1.14.0.tar.gz && cd nginx-1.14.0 && sed -i.bak 's/#define NGINX_VER          "nginx\/" NGINX_VERSION/#define NGINX_VER          "nginx"/g' src/core/nginx.h && sed -i.bak 's/CFLAGS="$CFLAGS -g"/#CFLAGS="$CFLAGS -g"/g' auto/cc/gcc && ./configure --prefix=/usr/local/nginx --with-http_ssl_module --with-http_stub_status_module --with-threads --with-file-aio &> /dev/null && make &> /dev/null && make install &> /dev/null
      Result: True
     Comment: /usr/local/nginx exists
     Started: 14:11:30.191093
    Duration: 0.413 ms
     Changes:   
----------
          ID: /usr/local/nginx/conf/nginx.conf
    Function: file.managed
      Result: True
     Comment: File /usr/local/nginx/conf/nginx.conf is in the correct state
     Started: 14:11:30.191624
    Duration: 32.744 ms
     Changes:   
----------
          ID: nginx-service
    Function: file.managed
        Name: /etc/init.d/nginx
      Result: True
     Comment: File /etc/init.d/nginx is in the correct state
     Started: 14:11:30.224499
    Duration: 30.972 ms
     Changes:   
----------
          ID: nginx-service
    Function: service.running
        Name: nginx
      Result: True
     Comment: The service nginx is already running
     Started: 14:11:30.256378
    Duration: 30.231 ms
     Changes:   

Summary for server3
------------
Succeeded: 8
Failed:    0
------------
Total states run:     8
Total run time: 574.480 ms

利用自动化运维工具saltstack配置nginx服务(2)_第12张图片

你可能感兴趣的:(利用自动化运维工具saltstack配置nginx服务(2))