ansible批量部署Redis
一、目录环境
[root@wd-code deploy_redis]# tree . ├── file │ ├── redis-4.0.13.tar.gz │ └── redis.conf.j2 ├── hosts └── redis.yml 1 directory, 4 files
二、file目录
file目录下存放redis包与redis.conf.j2配置文件
redis.conf.j2配置如下:
bind 127.0.0.1 {{ ansible_default_ipv4.address }} protected-mode yes port 6379 tcp-backlog 511 timeout 0 tcp-keepalive 300 daemonize yes supervised no pidfile {{ redis_data_dir }}/redis_6379.pid loglevel notice logfile "" databases 16 always-show-logo yes save 900 1 save 300 10 save 60 10000 stop-writes-on-bgsave-error yes rdbcompression yes rdbchecksum yes dbfilename dump.rdb dir {{ redis_data_dir }} slave-serve-stale-data yes slave-read-only yes repl-diskless-sync no repl-diskless-sync-delay 5 repl-disable-tcp-nodelay no slave-priority 100 lazyfree-lazy-eviction no lazyfree-lazy-expire no lazyfree-lazy-server-del no slave-lazy-flush no appendonly no appendfilename "appendonly.aof" appendfsync everysec no-appendfsync-on-rewrite no auto-aof-rewrite-percentage 100 auto-aof-rewrite-min-size 64mb aof-load-truncated yes aof-use-rdb-preamble no lua-time-limit 5000 slowlog-log-slower-than 10000 slowlog-max-len 128 latency-monitor-threshold 0 notify-keyspace-events "" hash-max-ziplist-entries 512 hash-max-ziplist-value 64 list-max-ziplist-size -2 list-compress-depth 0 set-max-intset-entries 512 zset-max-ziplist-entries 128 zset-max-ziplist-value 64 hll-sparse-max-bytes 3000 activerehashing yes client-output-buffer-limit normal 0 0 0 client-output-buffer-limit slave 256mb 64mb 60 client-output-buffer-limit pubsub 32mb 8mb 60 hz 10 aof-rewrite-incremental-fsync yes
三、hosts文件
[redis] 10.128.31.42 ansible_ssh_user=root ansible_ssh_pass=xxxx
四、redis.yml文件
--- - hosts: redis remote_user: work vars: redis_data_dir: /home/work/redis/data var_dir: /home/work/redis unarch: /home/work redis_dir: /home/work/redis-4.0.13 tasks: - name: 创建redis数据目录 file: path={{ redis_data_dir }} state=directory - name: 复制redis包到远程主机 copy: src=./file/redis-4.0.13.tar.gz dest=/tmp - name: 解压包 unarchive: src: /tmp/redis-4.0.13.tar.gz dest: "{{ unarch }}" copy: no - name: 安装 shell: cd {{ unarch }}/redis-4.0.13 && make - name: 创建二进制目录配置文件目录 shell: cd {{ var_dir }} && mkdir -pv bin conf - name: cp 编译完成的二进制文件到bin和conf下配置文件 shell: cp {{ redis_dir }}/src/redis-* {{ var_dir }}/bin && rm -rf {{ var_dir }}/bin/*.c && rm -rf {{ var_dir }}/bin/*.o - name: 拷贝编译完成的redis.conf文件到conf下 template: src=./file/redis.conf.j2 dest={{ var_dir }}/conf/redis.conf - name: 启动redis shell: "{{ var_dir }}/bin/redis-server {{ var_dir }}/conf/redis.conf" - name: 删除redis解压包 file: path={{ redis_dir }} state=absent
五、执行剧本
ansible-playbook -i hosts redis.yml