Ansible-playbook 部署redis主从一键部署
ansible-playbook redis目录树结构
[root@192 roles]# tree redis/redis/├── defaults├── files│ └── redis-3.2.5.tar.gz├── handlers│ └── main.yaml├── meta├── tasks│ └── main.yaml├── templates│ └── redis.conf.j2└── vars └── main.yaml
#首先这个tasks任务写的很乱,源码编译的这个启动脚本只有start和stop 两个参数,这个两个参数不是func函数类的,这块没办法做restart。当然也可以做,自己再重新搞个启动脚本,这块为了省事就用源码的脚本了。handlers是文件发生改变时触发重启。task里面注释的重启task,直接开启也可以的,两种方法都试过,完全ok。使用handlers就不需要注释那几行了。不使用handlers,直接把notify注释掉,start和stop开启就行。
[root@192 tasks]# cat main.yaml - name: Installredis base packages yum: name={ { item }} state=installed with_items: - gcc - make- name: copy redis.tar.gz to remote server copy: src=redis-{ { redis_version }}.tar.gz dest=/usr/local/- name: 解压redis shell: chdir=/usr/local/ tar zxf redis-{ { redis_version }}.tar.gz- name: make install redis shell: cd /usr/local/redis-{ { redis_version }} && make PREFIX=/usr/local/redis install- name: create redis init script copy: src=/usr/local/redis-{ { redis_version }}/utils/redis_init_script dest=/etc/init.d/redis mode=655- name: 修改redis-server启动脚本参数路径 shell: sed -i 's/^EXEC.*/EXEC=\/usr\/local\/redis\/bin\/redis-server/g' /etc/init.d/redis- name: 替换redis启动脚本CLIEXEC参数 shell: sed -i 's/^CLIEXEC.*/CLIEXEC=\/usr\/local\/redis\/bin\/redis-cli/g' /etc/init.d/redis- name: 添加环境变量文件 shell: /bin/echo { { item }} >> /etc/profile && source /etc/profile with_items: - export PATH=\$PATH:/usr/local/redis/bin- name: 创建redis配置文件路径 file: path=/etc/redis state=directory mode=755 owner=root group=root- name: copy redis配置文件到远程主机 template: src=redis.conf.j2 dest=/etc/redis/{ { port }}.conf notify: - stop redis - start redis#- name: start redis service# shell: service redis start #when: ansible_default_ipv4.address == "{ { slave_ip }}"- name: 替换master_ip shell: sed -i '/^# slaveof.*/a\slaveof { { master_ip }} { { port }}' /etc/redis/{ { port }}.conf when: ansible_default_ipv4.address == "{ { slave_ip }}" notify: - stop redis - start redis#- name: stop redis service# shell: service redis stop#- name: reload redis service# shell: service redis start
#handlers 文件内容:
[root@192 tasks]# cat ../handlers/main.yaml - name: stop redis shell: service redis stop- name: start redis shell: service redis start
#vars目录变量文件:
[root@192 vars]# cat main.yaml port: 6379redis_version: 3.2.5slave_ip: 192.168.121.129master_ip: 192.168.121.128
#模板文件就是一个redis.conf文件,这块只改了三个参数。
第一个:后台启动
[root@192 templates]# grep daemonize redis.conf.j2 # Note that Redis will write a pid file in /var/run/redis.pid when daemonized.daemonize yes# When the server runs non daemonized, no pid file is created if none is# specified in the configuration. When the server is daemonized, the pid file# output for logging but daemonize, logs will be sent to /dev/null
第二个:主从的ip
#这个ip必须要改,不然的话主从会异步的。master_link_status:down 会是这个状态,下午我怕错是发现主从一直异步,telnet发现端口问题。
[root@192 templates]# grep ansible redis.conf.j2 bind { { ansible_default_ipv4.address }}
第三个参数:
#日志必须的,之前我没打日志,主从异步也不知道怎么回事,开了日志才发现端口被拒。
[root@192 templates]# grep logfile redis.conf.j2 logfile "/usr/local/redis/redis.log"
其他参数可以优化,根据自己的需要做一个模板,我这只是练习玩。
#运行ansible-playbook 建议加个-v参数,可以看到具体的运行操作,可以看看运行是否符合自己的期望。
ansible-playbook 运行过程:
[root@192 ansible]# ansible-playbook redis.yml PLAY [test] ********************************************************************TASK [setup] *******************************************************************ok: [192.168.121.129]ok: [192.168.121.128]TASK [redis : redis base packages] *********************************************ok: [192.168.121.129] => (item=[u'gcc', u'make'])ok: [192.168.121.128] => (item=[u'gcc', u'make'])TASK [redis : copy redis.tar.gz to remote pc] **********************************changed: [192.168.121.128]changed: [192.168.121.129]TASK [redis : 解压redis] *********************************************************changed: [192.168.121.129] [WARNING]: Consider using unarchive module rather than running tarchanged: [192.168.121.128]TASK [redis : make install redis] *********************************************changed: [192.168.121.129]changed: [192.168.121.128]TASK [redis : create redis init script] ****************************************changed: [192.168.121.128]changed: [192.168.121.129]TASK [redis : redis init script cover] *****************************************changed: [192.168.121.129] [WARNING]: Consider using template or lineinfile module rather than running sedchanged: [192.168.121.128]TASK [redis : 替换redis启动脚本CLIEXEC参数] ********************************************changed: [192.168.121.128]changed: [192.168.121.129]TASK [redis : /etc/profile] ****************************************************changed: [192.168.121.129] => (item=export PATH=\$PATH:/usr/local/redis/bin)changed: [192.168.121.128] => (item=export PATH=\$PATH:/usr/local/redis/bin)TASK [redis : create redis config directory] ***********************************changed: [192.168.121.129]changed: [192.168.121.128]TASK [redis : redis template] **************************************************changed: [192.168.121.129]changed: [192.168.121.128]TASK [redis : start redis service] *********************************************skipping: [192.168.121.128]changed: [192.168.121.129] [WARNING]: Consider using service module rather than running serviceTASK [redis : 替换master_ip] *****************************************************changed: [192.168.121.129]changed: [192.168.121.128]TASK [redis : stop redis service] **********************************************changed: [192.168.121.128]changed: [192.168.121.129]TASK [redis : reload redis service] ********************************************changed: [192.168.121.129]changed: [192.168.121.128]PLAY RECAP *********************************************************************192.168.121.128 : ok=14 changed=12 unreachable=0 failed=0 192.168.121.129 : ok=15 changed=13 unreachable=0 failed=0
#192.168.121.129从库的状态,主库set 一个值,从库也是可以get到的。
127.0.0.1:6379> info Replication# Replicationrole:slavemaster_host:192.168.121.128master_port:6379master_link_status:upmaster_last_io_seconds_ago:1master_sync_in_progress:0slave_repl_offset:323slave_priority:100slave_read_only:1connected_slaves:0master_repl_offset:0repl_backlog_active:0repl_backlog_size:1048576repl_backlog_first_byte_offset:0repl_backlog_histlen:0127.0.0.1:6379> GET name"meng"