摘要
自动化部署已经很成熟并且很多企业都在使用,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服务