1. 程式人生 > >saltstack安裝nginx客戶端

saltstack安裝nginx客戶端

saltstack

#################################################
###

saltstack安裝nginx客戶端

#################################################

#看下目錄結構
[root@M01 nginx]# tree
.
├── files
│?? ├── extra
│?? │?? └── status.conf
│?? ├── nginx-1.14.0.tar.gz
│?? ├── nginx.conf
│?? └── nginx.init
└── nginx-install.sls

[root@M01 salt]# mkdir -p /srv/salt/nginx/files/extra

#主配置文件
[root@M01 nginx]# cat /srv/salt/nginx/nginx-install.sls
nginx-pkg:
pkg.installed:

  • pkgs:
    • gcc
    • gcc-c++
    • glibc
    • make
    • autoconf
    • openssl
    • openssl-devel
    • pcre
    • pcre-devel
    • zlib
    • zlib-devel

nginx-install:
file.managed:

  • name: /usr/local/src/nginx-1.14.0.tar.gz
  • source: salt://nginx/files/nginx-1.14.0.tar.gz
  • user: root
  • group: root
  • mode: 755
    cmd.run:
  • name: cd /usr/local/src && tar xf nginx-1.14.0.tar.gz && cd nginx-1.14.0 && ./configure --user=www --group=www --with-http_ssl_module --with-http_stub_status_module --prefix=/usr/local/www/ && make && make install
  • unless: test -d /usr/local/nginx
  • require:
    • pkg: nginx-pkg
    • file: nginx-install

nginx_user:
user.present:

  • name: www
  • createhome: False
  • gid_from_name: True
  • shell: /sbin/nologin

nginx_ln:
cmd.run:

  • names:
    • ln -s /usr/local/www/ /usr/local/nginx
  • unless: test -d /usr/local/nginx
  • require:
    • file: nginx-install

nginx_chkconfig:
file.managed:

  • name: /etc/init.d/nginx
  • source: salt://nginx/files/nginx.init
  • user: root
  • group: root
  • mode: 755
    cmd.run:
  • names:
    • chkconfig --add nginx
    • chkconfig nginx on
    • service nginx start
  • unless: chkconfig --list | grep nginx
  • require:
    • file: nginx_chkconfig

nginx_conf_extra:
file.recurse:

  • name: /usr/local/nginx/conf/extra
  • source: salt://nginx/files/extra

nginx_conf:
file.managed:

  • name: /usr/local/nginx/conf/nginx.conf
  • source: salt://nginx/files/nginx.conf
  • user: root
  • group: root
  • mode: 644

nginx-service:
file.directory:

  • name: /usr/local/nginx/conf/vhost
  • require:
    • cmd: nginx-install
      service.running:
  • name: nginx
  • enable: True
  • reload: True
  • require:
    • cmd: nginx-install
    • cmd: nginx_chkconfig
  • watch:
    • file: nginx_conf

#主頁的配置文件,只做了一個狀態
[root@M01 nginx]# cat /srv/salt/nginx/files/nginx.conf
worker_processes 1;
events {
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
sendfile on;
keepalive_timeout 65;
include extra/*.conf;
}

#狀態內容
[root@M01 nginx]# cat /srv/salt/nginx/files/extra/status.conf
#status
server {
listen 8081;
server_name status.yehaixiao.com;
location / {
stub_status on;
access_log off;
allow 192.168.44.0/24;
deny all;
}
}

#nginx.init由官方提供,根據安裝位置配置好路徑
nginx="/usr/local/www/sbin/nginx"
NGINX_CONF_FILE="/usr/local/www/conf/nginx.conf"

[root@M01 nginx]# cat /srv/salt/nginx/files/nginx.init
#!/bin/sh
#

nginx - this script starts and stops the nginx daemon

#

chkconfig: - 85 15

description: NGINX is an HTTP(S) server, HTTP(S) reverse \

proxy and IMAP/POP3 proxy server

processname: nginx

config: /etc/nginx/nginx.conf

config: /etc/sysconfig/nginx

pidfile: /var/run/nginx.pid

Source function library.

. /etc/rc.d/init.d/functions

Source networking configuration.

. /etc/sysconfig/network

Check that networking is up.

[ "$NETWORKING" = "no" ] && exit 0
nginx="/usr/local/www/sbin/nginx"
prog=$(basename $nginx)
NGINX_CONF_FILE="/usr/local/www/conf/nginx.conf"
[ -f /etc/sysconfig/nginx ] && . /etc/sysconfig/nginx
lockfile=/var/lock/subsys/nginx
make_dirs() {

make required directories

user=$nginx -V 2>&1 | grep "configure arguments:" | sed ‘s/[^*]*--user=\([^ ]*\).*/\1/g‘ -
if [ -z "grep $user /etc/passwd" ]; then
useradd -M -s /bin/nologin $user
fi
options=$nginx -V 2>&1 | grep ‘configure arguments:‘
for opt in $options; do
if [ echo $opt | grep ‘.*-temp-path‘ ]; then
value=echo $opt | cut -d "=" -f 2
if [ ! -d "$value" ]; then

echo "creating" $value

           mkdir -p $value && chown -R $user $value
       fi
   fi

done
}
start() {
[ -x $nginx ] || exit 5
[ -f $NGINX_CONF_FILE ] || exit 6
make_dirs
echo -n $"Starting $prog: "
daemon $nginx -c $NGINX_CONF_FILE
retval=$?
echo
[ $retval -eq 0 ] && touch $lockfile
return $retval
}
stop() {
echo -n $"Stopping $prog: "
killproc $prog -QUIT
retval=$?
echo
[ $retval -eq 0 ] && rm -f $lockfile
return $retval
}
restart() {
configtest || return $?
stop
sleep 1
start
}
reload() {
configtest || return $?
echo -n $"Reloading $prog: "
killproc $nginx -HUP
RETVAL=$?
echo
}
force_reload() {
restart
}
configtest() {
$nginx -t -c $NGINX_CONF_FILE
}
rh_status() {
status $prog
}
rh_status_q() {
rh_status >/dev/null 2>&1
}
case "$1" in
start)
rh_status_q && exit 0
$1
;;
stop)
rh_status_q || exit 0
$1
;;
restart|configtest)
$1
;;
reload)
rh_status_q || exit 7
$1
;;
force-reload)
force_reload
;;
status)
rh_status
;;
condrestart|try-restart)
rh_status_q || exit 0
;;
*)
echo $"Usage: $0 {start|stop|status|restart|condrestart|try-restart|reload|force-reload|configtest}"
exit 2
esac

#客戶端安裝nginx
[root@M01 zabbix_agent]# salt ‘WEB0?‘ state.sls nginx.nginx-install

#測試nginx布置成功
[root@M01 files]# curl status.yehaixiao.com:8081
Active connections: 1
server accepts handled requests
2 2 4
Reading: 0 Writing: 1 Waiting: 0

[root@M01 files]# cat /srv/salt/top.sls
base:
‘*‘:

  • apache.apache-install
  • dns.dns-install
  • hosts.hosts-install
  • history.history-install
  • sysctl.sysctl-install
  • haproxy.haproxy-install
  • cluster.haproxy-outside
  • keepalived.keepalived-install
  • cluster.haproxy-outside-keepalived
  • zabbix_agent.zabbix-agent-install
  • nginx.nginx-install

#測試執行,沒問題才更新到客戶端
[root@M01 base]# salt ‘WEB0?‘ state.highstate test=True

saltstack安裝nginx客戶端