From 72b5b5a7967b97ed50a6e9322f8318025697ffdd Mon Sep 17 00:00:00 2001 From: Daniel Miranda Date: Wed, 16 Nov 2016 02:48:18 -0200 Subject: [PATCH] Add option to detect master/slave status from runtime status --- defaults/main.yml | 2 ++ tasks/local_facts.yml | 6 ++++++ tasks/main.yml | 8 ++++++-- tasks/runtime_roles.yml | 13 +++++++++++++ 4 files changed, 27 insertions(+), 2 deletions(-) create mode 100644 tasks/runtime_roles.yml diff --git a/defaults/main.yml b/defaults/main.yml index 0d77e87d..efa6f0c4 100644 --- a/defaults/main.yml +++ b/defaults/main.yml @@ -41,6 +41,8 @@ redis_socket_perm: 755 ## Replication options # Set slaveof just as you would in redis.conf. (e.g. "redis01 6379") redis_slaveof: false +# Ignore manually assigned roles for instances if runtime status can be used instead +redis_runtime_replication_role: yes # Make slaves read-only. "yes" or "no" redis_slave_read_only: "yes" redis_slave_priority: 100 diff --git a/tasks/local_facts.yml b/tasks/local_facts.yml index 232d84c3..0d91ff79 100644 --- a/tasks/local_facts.yml +++ b/tasks/local_facts.yml @@ -17,6 +17,7 @@ vars: redis_facts_cli_port: "{{ redis_port }}" when: not redis_sentinel + register: redis_facts_install_result - name: create redis sentinel facts template: @@ -27,3 +28,8 @@ redis_facts_cli_port: "{{ redis_sentinel_port }}" redis_facts_sentinel: yes when: redis_sentinel + register: redis_sentinel_facts_install_result + +- name: refresh facts due to new fact scripts + setup: + when: redis_facts_install_result.changed or redis_sentinel_facts_install_result.changed diff --git a/tasks/main.yml b/tasks/main.yml index 0e6db240..476d7bb8 100644 --- a/tasks/main.yml +++ b/tasks/main.yml @@ -1,6 +1,12 @@ --- - include: check_vars.yml +- include: local_facts.yml + when: redis_local_facts|bool + +- include: runtime_roles.yml + when: not redis_sentinel and redis_runtime_replication_role + - include: install.yml - include: server.yml @@ -13,5 +19,3 @@ tags: - config -- include: local_facts.yml - when: redis_local_facts|bool diff --git a/tasks/runtime_roles.yml b/tasks/runtime_roles.yml new file mode 100644 index 00000000..f7d48dd7 --- /dev/null +++ b/tasks/runtime_roles.yml @@ -0,0 +1,13 @@ +- name: set redis to slave automatically + set_fact: + redis_slaveof: >- + {{ ansible_local[redis_service_name].master_host }} + {{ ansible_local[redis_service_name].master_port }} + when: > + ansible_local[redis_service_name].role|default() == 'slave' + +- name: set redis to master automatically + set_fact: + redis_slaveof: ~ + when: > + ansible_local[redis_service_name].role|default() == 'master'