尝试启动redis服务,启动redis服务失败开始安装redis并创建redis集群
---
- name: check redis service
shell: "ps -ef|grep redis|grep -v grep|awk '{print $2}'"
register: redis_process_result
- name: try to start redis service
shell: "/var/redis/{{redis.package_name}}/{{redis.cluster.port1}}/bin/redis-server /var/redis/{{redis.package_name}}/{{redis.cluster.port1}}/config/redis.conf &&
/var/redis/{{redis.package_name}}/{{redis.cluster.port2}}/bin/redis-server /var/redis/{{redis.package_name}}/{{redis.cluster.port2}}/config/redis.conf"
ignore_errors: true
register: redis_start_process
when: redis_process_result.stdout_lines==[]
- fail:
msg: "try to start redis fail, start setup redis later"
register: redis_setup
ignore_errors: true
when: redis_start_process is not skip and redis_start_process is failure
- name: setup redis
block:
- name: copy packages
copy:
src: "{{copy_path}}/{{redis.package_name}}.tar"
dest: "/var/redis/"
owner: root
group: root
mode: 0755
ignore_errors: yes
- name: unzip redis package
shell: cd /var/redis && tar -xvf {{redis.package_name}}.tar
- name: copy redis.conf
template:
src: "{{ item.src }}"
dest: "{{ item.dest }}"
backup: yes
with_items:
- { src: 'redis_1.conf.j2', dest: '/var/redis/{{redis.package_name}}/{{redis.cluster.port1}}/config/redis.conf' }
- { src: 'redis_2.conf.j2', dest: '/var/redis/{{redis.package_name}}/{{redis.cluster.port2}}/config/redis.conf' }
- name: copy redis service
template: src=redis.service.j2 dest=/usr/lib/systemd/system/redis.service
- name: enable redis service
shell: systemctl enable redis
- name: start redis service
shell: service redis start
- name: check cluster info
shell: /var/redis/{{redis.package_name}}/{{redis.cluster.port1}}/bin/redis-cli -h {{redis.redis_cli_addr}} -p {{redis.cluster.port1}} -a {{redis.password}} cluster info 2>/dev/null |grep "cluster_state"
register: redis_cluster_info
- name: create redis cluster
shell: yes yes | /var/redis/{{redis.package_name}}/{{redis.cluster.port1}}/bin/redis-cli --cluster create {{redis.cluster.addr1}}:{{redis.cluster.port1}} {{redis.cluster.addr2}}:{{redis.cluster.port1}}
{{redis.cluster.addr3}}:{{redis.cluster.port1}} {{redis.cluster.addr3}}:{{redis.cluster.port2}} {{redis.cluster.addr1}}:{{redis.cluster.port2}} {{redis.cluster.addr2}}:{{redis.cluster.port2}} --cluster-replicas 1 -a {{redis.password}}
ignore_errors: yes
when: "'fail' in redis_cluster_info.stdout"
- name: clean install package
shell: rm -f /var/redis/{{redis.package_name}}.tar
rescue:
- name:
shell: "echo setup end"
when: redis_setup is not skip
可以通过以下命令进行redis集群部署
ansible-playbook -i hosts playbook/redis.yml --tag install