fix templating with blockinline and include within redis.conf

This commit is contained in:
ace
2021-11-16 13:42:55 +03:00
parent b05fb8a3f7
commit c020c98f27
12 changed files with 141 additions and 57 deletions

View File

@ -12,7 +12,7 @@
- name: Add redis.list to sources
template:
src: "redis.list.j2"
src: "etc/apt/sources.list.d/redis.list.j2"
dest: /etc/apt/sources.list.d/redis.list
#- name: Add redis repository from PPA
@ -34,10 +34,16 @@
- name: Install redis {{ redis_debian_version }}
apt:
name: "{{ redis_debian_package }}={{ redis_debian_version }}"
name:
- "{{ redis_debian_package }}={{ redis_debian_version }}"
- "redis-tools={{ redis_debian_version }}"
update_cache: yes
when: is_redis.rc != 0
- name: Move default config file to /etc/redis/redis.conf.orig
command: mv /etc/redis/redis.conf /etc/redis/redis.conf.orig
when: is_redis.rc != 0
- name: Check if redis-sentinel is installed
ansible.builtin.shell: dpkg-query -W {{ redis_sentinel_debian_package }} 2>&1 | grep {{ redis_sentinel_debian_version }}
ignore_errors: True
@ -56,3 +62,7 @@
name: "{{ redis_sentinel_debian_package }}={{ redis_sentinel_debian_version }}"
update_cache: yes
when: is_redis_sentinel != 0
- name: Move default config file to /etc/redis/sentinel.conf.orig
command: mv /etc/redis/sentinel.conf /etc/redis/sentinel.conf.orig
when: is_redis_sentinel.rc != 0

View File

@ -1,5 +1,16 @@
---
- name: Add redis-sentinel config
template:
src: sentinel.conf.j2
- name: Ensure sentinel config exists
copy:
content: ""
dest: "/etc/redis/sentinel.conf"
force: no
owner: redis
group: redis
mode: 0640
- name: Template redis-sentinel config
blockinfile:
block: "{{ lookup('template', 'etc/redis/sentinel.conf.j2') }}"
path: "/etc/redis/sentinel.conf"
notify:
- Restart redis-sentinel

View File

@ -1,5 +1,42 @@
---
- name: Add redis config
template:
src: redis.conf.j2
dest: "/etc/redis/redis.conf"
- name: Check if redis is running
command: systemctl status redis-server
ignore_errors: yes
changed_when: False
register: service_redis_status
failed_when: False
- block:
- name: Ensure redis config exists
copy:
content: ""
dest: "/etc/redis/redis.conf"
force: no
owner: redis
group: redis
mode: 0640
- name: Template redis config
blockinfile:
block: "{{ lookup('template', 'etc/redis/redis.conf.j2') }}"
path: "/etc/redis/redis.conf"
notify:
- Restart redis
when: service_redis_status.rc != 0
- name: Ensure redis auth config exists
copy:
content: ""
dest: "/etc/redis/redis-auth.conf"
force: no
owner: redis
group: redis
mode: 0640
- name: Template redis auth config
blockinfile:
block: "{{ lookup('template', 'etc/redis/redis-auth.conf.j2') }}"
path: "/etc/redis/redis-auth.conf"
notify:
- Restart redis

View File

@ -0,0 +1,23 @@
---
- name: Discover redis with master role
shell: redis-cli --pass {{ redis_requirepass }} info replication | grep 'role:master'
register: redis_master
changed_when: False
failed_when: False
- name: Set fact about redis master
set_fact:
redis_master_ip: "{{ hostvars[inventory_hostname].ansible_default_ipv4.address | default(hostvars[play_hosts[0]].ansible_default_ipv4.address) }}"
delegate_to: "{{ item }}"
delegate_facts: true
loop: "{{ play_hosts }}"
when: redis_master.rc == 0
- name: Set fact about redis master
set_fact:
redis_master_ip: "{{ hostvars[play_hosts[0]].ansible_default_ipv4.address }}"
when: redis_master_ip is not defined
- debug:
msg: "Master redis IP is {{ redis_master_ip }}"
verbosity: 2

View File

@ -1,28 +1,15 @@
---
- name: Set fact about redis master
set_fact:
redis_master_ip: "{{ hostvars[inventory_hostname].ansible_default_ipv4.address }}"
delegate_to: "{{ item }}"
loop: "{{ play_hosts }}"
run_once: yes
when: hostvars[inventory_hostname].master is defined
- debug:
msg: "{{ redis_master_ip }}"
- name: Include redis master discover
include: discover_master.yaml
#- debug:
# msg: "{{ redis_master_ip }}"
- name: Include redis installation tasks
include: "{{ ansible_os_family }}/install.yaml"
- name: Check if redis is running
command: systemctl status redis-server
ignore_errors: yes
changed_when: False
register: service_redis_status
failed_when: False
- name: Configure redis
include: configure.yaml
when: service_redis_status.rc != 0
- name: Start redis server
service:
@ -30,17 +17,10 @@
enabled: True
state: started
masked: no
- name: Check if redis-sentinel is running
command: systemctl status redis-sentinel
ignore_errors: yes
changed_when: False
register: service_redis_sentinel_status
failed_when: False
register: start_redis
- name: Configure redis-sentinel
include: configure-sentinel.yaml
when: service_redis_sentinel_status.rc != 0
- name: Start redis-sentinel
service:
@ -48,3 +28,8 @@
enabled: True
state: started
masked: no
register: start_redis_sentinel
- name: Fix redis-sentinel config
include: configure-sentinel.yaml