SALT自动化运维工具(没有附图)

安装salt环境
[root@foundation10 html]# ls  ##真机apach默认发布目录,安放yum源
mitaka  pacemaker  rhel6  rhel6.5  rhel7.0  rhel7.2  rhel7.3  test.qcow2

[root@server1 ~]# vim /etc/yum.repos.d/rhel-source.repo 
[root@server1 ~]# yum clean all
[rhel-source]
name=Red Hat Enterprise Linux $releasever - $basearch - Source
baseurl=http://172.25.10.250/rhel6.5
enabled=1
gpgcheck=1
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-redhat-release

[salt]
name=salt
baseurl=http://172.25.10.250/rhel6
enabled=1
gpgcheck=0
[root@server1 ~] # yum install -y salt-master

[root@server2 ~]# vim /etc/yum.repos.d/rhel-source.repo
[root@server2 ~]# yum clean all
[root@server2 ~]# yum install -y salt-minion
[root@server2 ~]# cd /etc/salt
[root@server2 ~]# vim minion
#master: salt
master : server1

[root@server2 salt]# /etc/init.d/salt-minion start

[root@server1 ~]# salt-key -A ##授权
[root@server1 ~]# salt-key -L ##

salt实现自动化安装http服务
[root@server1 ~]# cd /etc/salt
[root@server1 salt]# vim master
#
file_roots:
  base:
    - /srv/salt
#

[root@server1 salt]# /etc/init.d/salt-master restart
[root@server1 salt]# mkdir /srv/salt
[root@server1 salt]# cd /srv/salt/
[root@server1 salt]# mkdir httpd
[root@server1 salt]# cd httpd/
[root@server1 httpd]# ls
[root@server1 httpd]# vim install.sls  
apache-install:
  pkg.installed:                 ##安装加运行
    - pkgs:
      - httpd

  service.running:
    - name: httpd
    - enable: True
    - reload: True

[root@server1 httpd]# salt server2 state.sls httpd ##推送


更改server2的http的配置文件
[root@server1 httpd]# mkdir files/
[root@server1 httpd]# cd files/
[root@server1 files]# scp server2:/etc/httpd/conf/httpd.conf .
[root@server1 files]# vim httpd.conf
#Listen 12.34.56.78:80
Listen 8080

#
[root@server1 files]# cd ..
[root@server1 httpd]# vim install.sls 
apache-install:
  pkg.installed:
    - pkgs:
      - httpd

  file.managed:
    - name: /etc/httpd/conf/httpd.conf
    - source: salt://httpd/files/httpd.conf

  service.running:
    - name: httpd
    - enable: True
    - reload: True
[root@server1 httpd]# salt server2 state.sls httpd.install

自动化部署nginx
[root@foundation10 5.27]# ls
file  nginx-1.12.0.tar.gz  rhel6
[root@foundation10 5.27]# scp nginx-1.12.0.tar.gz @server1:/srv/salt/nginx/files

[root@server3 ~]# vim /etc/yum.repos.d/rhel-source.repo
[root@server3 ~]# yum clean all
[root@server3 ~]# yum install -y salt-minion
[root@server3 ~]# cd /etc/salt
[root@server3 ~]# vim minion
#master: salt
master : server1

[root@server3 salt]# /etc/init.d/salt-minion start

[root@server1 ~]# salt-key -A ##授权
[root@server1 ~]# salt-key -L ##
[root@server1 ~]# cd /srv/salt/
[root@server1 salt]# mkdir nginx
[root@server1 salt]# cd nginx
[root@server1 nginx]# mkdir files
[root@server1 nginx]# vim install.sls
include:
  - pkgs.install

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

  cmd.run:
    - name: cd /mnt && tar zxf nginx-1.12.0.tar.gz && cd nginx-1.12.0 && 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    ##这里没有隐藏nginx版本号
    - create: /usr/local/nginx
[root@server1 nginx]# salt server3 state.sls nginx.install ##server3上nginx已经安装好了

将安装脚本和文件配置脚本整合到一起
[root@foundation10 5.27]# ls
5.27  file  nginx  nginx-1.12.0.tar.gz  rhel6
nginx 启动脚本内容
#!/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
[root@foundation10 5.27]# scp nginx @server1:/srv/salt/nginx/files

[root@server1 nginx]# vim service.sls
include:
  - nginx.install

/etc/init.d/nginx:   ##server3中文件放的位置
  file.managed:
    - source: salt://nginx/files/nginx ##本机中文件的位置
    - mode: 755

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

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


[root@server1 nginx]# salt server3 state.sls nginx.service

[root@server3 init.d]# /etc/init.d/nginx stop
Stopping nginx:                                            [  OK  ]
[root@server3 init.d]# /etc/init.d/nginx start
Starting nginx:                                            [  OK  ]
[root@server3 init.d]# /etc/init.d/nginx reload
Reloading nginx:                                           [  OK  ]
[root@server3 init.d]# /etc/init.d/nginx restart
Stopping nginx:                                            [  OK  ]
Starting nginx:                                            [  OK  ]

检测成功

猜你喜欢

转载自blog.csdn.net/guaiderzhu1314/article/details/80470662
今日推荐