摘要
自動化部署已經很成熟並且很多企業都在使用,ansible作爲自動化運維工具的主力軍,今天就推薦大家使用ansible的playbook自動化部署redis
準備工作
1、ansible服務端: 192.168.0.33
2、redis安裝服務器: 192.168.0.32
3、redis安裝文件: redis-5.0.7.tar.gz
目錄規劃
1、ansible服務端目錄:
ansible\
├── install
│ ├── redis-5.0.7.tar.gz
├── redis
│ ├── intall_redis.yml
│ ├── README.txt
│ └── redis
2、redis安裝客戶端目錄:
安裝目錄: /usr/local/redis
安裝包存放目錄:/opt/redis/redis-5.0.7.tar.gz
配置文件:/etc/redis/redis.conf
文件內容介紹
cat intall_redis.yml
---
- name: install redis
gather_facts: False
hosts: "{{ ip }}"
user: root
vars:
- redis_port: "{{ port|default(6379) }}"
tasks:
- name: Create the dir
file: path={{ item }} state=directory
with_items:
- /etc/redis/
- /opt/redis/
- name: get redis install package
copy: src=/opt/ansible/install/redis-5.0.7.tar.gz dest=/opt/redis/redis-5.0.7.tar.gz
- name: unarchive the redis-5.0.7.tar.gz
unarchive: src=/opt/redis/redis-5.0.7.tar.gz dest=/opt/redis/ copy=no
- name: install the redis-5.0.7.tar.gz
shell: /opt/redis/redis-5.0.7 && make PREFIX=/usr/local/redis install
- name: copy target redis.conf file
shell: cp /opt/redis/redis-5.0.7/redis.conf /etc/redis/
- name: copy redis command to system bin
shell: cd /usr/local/redis/bin && cp redis-benchmark redis-cli redis-server /usr/bin/
- name: init the redis maintain script
copy: src=/opt/ansible/redis/redis dest=/etc/init.d/redis mode=0755
- name: Start the redis server
shell: chkconfig --add redis
ignore_errors: yes
- name: add redis to chkconfig
shell: chkconfig --level 2345 redis on && /etc/init.d/redis start
ignore_errors: yes
cat redis
#!/bin/bash
#chkconfig: 2345 80 90
# Simple Redis init.d script conceived to work on Linux systems
# as it does use of the /proc filesystem.
PATH=/usr/local/bin:/sbin:/usr/bin:/bin
REDISPORT=6379
EXEC=/usr/local/redis/bin/redis-server
REDIS_CLI=/usr/local/redis/bin/redis-cli
PIDFILE=/var/run/redis.pid
CONF="/etc/redis/redis.conf"
case "$1" in
start)
if [ -f $PIDFILE ]
then
echo "$PIDFILE exists, process is already running or crashed"
else
echo "Starting Redis server..."
$EXEC $CONF
fi
if [ "$?"="0" ]
then
echo "Redis is running..."
fi
;;
stop)
if [ ! -f $PIDFILE ]
then
echo "$PIDFILE does not exist, process is not running"
else
PID=$(cat $PIDFILE)
echo "Stopping ..."
$REDIS_CLI -p $REDISPORT SHUTDOWN
while [ -x ${PIDFILE} ]
do
echo "Waiting for Redis to shutdown ..."
sleep 1
done
echo "Redis stopped"
fi
;;
restart|force-reload)
${0} stop
${0} start
;;
*)
echo "Usage: /etc/init.d/redis {start|stop|restart|force-reload}" >&2
exit 1
esac
任務流程
playbook的任務流程大致爲:
1、將redis-5.0.7.tar.gz安裝包及啓動文件redis複製到目標服務器
2、編譯安裝redis
3、將redis配置文件複製到指定目錄
4、將redis相關命令設置爲系統全局命令
5、將redis註冊爲系統服務且開機啓動
6、啓動redis服務