Ansible 角色(roles)一键部署redis集群(三主三从)

一、目录结构

.              roles主目录
├── redis      
│   ├── files
│   │   └── redis-6.2.5.tar.gz
│   ├── tasks
│   │   └── main.yml 
│   └── vars
│       └── main.yml
└── role_redis.yml 启动文件

二、task清单

---
 - name: Create Base Path
   file:
     path: '{{install_path}}/redis/{{item.port}}'
     state: 'directory'
   become: true
   with_items:
     - { port: '{{master_port}}' }
     - { port: '{{slave_port}}' }

 - name: Create Base Path
   file:
     path: '{{data_path}}/{{item.port}}'
     state: 'directory'
   become: true
   with_items:
     - { port: '{{master_port}}' }
     - { port: '{{slave_port}}' }

 - name: Check if redis exists
   shell: 'ps -ef | grep redis-server | grep -v "grep" | wc -l'
   register: check_redis

 - name: Stop redis server
   shell: 'redis-cli -h {{ ansible_facts.ens192.ipv4.address }} -p {{item.port}}  shutdown'
   with_items:
     - { port: '{{master_port}}' }
     - { port: '{{slave_port}}' }
   when: check_redis.stdout|int > 0

 - name: Delete aof
   shell: 'rm -rf {{data_path}}/{{item.port}}/appendonly.aof'
   become: true
   with_items:
     - { port: '{{master_port}}' }
     - { port: '{{slave_port}}' }

 - name: Delete nodes.conf
   shell: 'rm -rf {{data_path}}/{{item.port}}/nodes.conf'
   become: true
   with_items:
     - { port: '{{master_port}}' }
     - { port: '{{slave_port}}' }

 - name: Copy redis tar
   copy:
     src: '{{file_name}}.tar.gz'
     dest: '/tmp'
   become: true

 - name: Unpack redis tar
   unarchive:
     src: '/tmp/{{file_name}}.tar.gz'
     dest: '{{install_path}}'
     remote_src: yes
     owner: root
     group: root
   become: true
 - name: Installing Redis
   shell: make && make install
   args:
     chdir: '{{install_path}}/{{file_name}}'
   become: true

 - name: Replace common config
   replace:
     path: '{{install_path}}/{{file_name}}/redis.conf'
     regexp: '{{item.regexp}}'
     replace: '{{ item.line }}'
   with_items:
     - { regexp: 'bind 127.0.0.1 -::1', line: 'bind 0.0.0.0'}
     - { regexp: '# cluster-enabled yes', line: 'cluster-enabled yes'}
     - { regexp: '# cluster-config-file nodes-6379.conf', line: 'cluster-config-file {{install_path}}/redis/{{master_port}}/nodes.conf'}
     - { regexp: '# cluster-node-timeout 15000', line: 'cluster-node-timeout 5000'}
     - { regexp: 'appendonly no', line: 'appendonly yes'}
     - { regexp: 'daemonize no', line: 'daemonize yes'}
     - { regexp: '# masterauth <master-password>', line: 'masterauth "om-redis-uat-2021"'}
     - { regexp: 'pidfile /var/run/redis_6379.pid', line: 'pidfile  {{data_path}}/{{master_port}}/redis.pid'}
     - { regexp: 'dir ./', line: 'dir {{data_path}}/{{master_port}}'}
   become: true

 - name: Copy config
   copy:
     src: '{{install_path}}/{{file_name}}/redis.conf'
     dest: '{{install_path}}/redis/{{item.port}}/'
   with_items:
     - { port: '{{master_port}}' }
     - { port: '{{slave_port}}' }
   become: true

 - name: Replace slave config
   replace:
     path: '{{install_path}}/redis/{{slave_port}}/redis.conf'
     regexp: '{{item.regexp}}'
     replace: '{{ item.line }}'
   with_items:
     - { regexp: 'cluster-config-file {{install_path}}/redis/{{master_port}}/nodes.conf', line: 'cluster-config-file {{install_path}}/redis/{{slave_port}}/nodes.conf'}
     - { regexp: 'pidfile  {{data_path}}/{{master_port}}/redis.pid', line: 'pidfile {{data_path}}/{{slave_port}}/redis.pid'}
     - { regexp: 'dir {{data_path}}/{{master_port}}', line: 'dir {{data_path}}/{{slave_port}}'}
   become: true


 - name: Modify port
   replace:
     path: '{{item.path}}'
     regexp: '{{item.regexp}}'
     replace: '{{ item.line }}'
   become: true
   with_items:
     - {path: '{{install_path}}/redis/{{master_port}}/redis.conf', regexp: 'port 6379', line: 'port {{master_port}}'}
     - {path: '{{install_path}}/redis/{{slave_port}}/redis.conf', regexp: 'port 6379', line: 'port {{slave_port}}'}
 - name: Delete nodes conf
   file:
     path: '{{data_path}}/{{item.port}}/nodes-{{item.port}}.conf'
     state: 'absent'
   with_items:
     - { port: '{{master_port}}' }
     - { port: '{{slave_port}}' }
   become: true

 - name: Start redis
   shell: '{{install_path}}/{{file_name}}/src/redis-server {{install_path}}/redis/{{item.port}}/redis.conf'
   become:  true
   with_items:
     - { port: '{{master_port}}' }
     - { port: '{{slave_port}}' }
   become: true

 - name: Create Cluster
   shell: 'echo yes | redis-cli --cluster create 10.86.130.145:7000 10.86.130.144:7000 10.86.130.146:7000 10.86.130.145:7001 10.86.130.144:7001 10.86.130.146:7001 --cluster-replicas 1'
   run_once: true

三、vars变量

master_port: 7000
slave_port: 7001
file_name: 'redis-6.2.5'
install_path: '/usr/local'
data_path: '/data/redis'

四、role_redis.yml

---
 - hosts: test
   remote_user: zhuhg
   roles:
     - redis
posted @ 2021-08-20 16:18  啦啦la  阅读(383)  评论(0编辑  收藏  举报