I want a variable on Ansible to be active or passive depending on the condition.
I am using the role "davidwittman.redis". I'm setting up a redis cluster. I need to run a single yml file by deploying it to 3 servers at the same time. I can't distribute "yml" files separately.
I have a simple setup configuration below. Here, I want to activate the "redis_slaveof" value on all other servers, except the master server, depending on the condition.
In other words, I want the "redis_slaveof" value I selected to not be active on the master server, but to have this value active on the other 2 servers.
How can I do this, do you have any suggestions ?
my main ansible configuration;
> - name: Redis Replication Slave Installations hosts: localhost
>
> pre_tasks:
> - name: check redis server1
> shell: "cat /etc/hosts |grep redis |awk '{print $2}' |sed -n 1p"
> register: server01
>
> - name: check redis server2
> shell: "cat /etc/hosts |grep redis |awk '{print $2}' |sed -n 2p"
> register: server02
>
> - name: check redis server3
> shell: "cat /etc/hosts |grep redis |awk '{print $2}' |sed -n 3p"
> register: server03
>
> roles:
> - role: davidwittman.redis
>
> vars:
> - redis_version: 6.2.6
> - redis_bind: 0.0.0.0
> - redis_port: 6379
> - redis_service_name: redis-service
> - redis_protected_mode: "no"
> - redis_config_file_name: "redis.conf"
>
> # - redis_slaveof: "{{ server01.stdout }} 6379"
I tried the following;
- I added extra task and "import_role". I put a when condition in it. If my server01 value and server local hostname value are not the equal, enable this role. Thus, I thought that it would activate this value on other servers except the master server.But it didn't work, added it on all servers. I saw the following error in the logs. "[WARNING]: flush_handlers task does not support when conditional"
tasks: - name: test import_role: name: davidwittman.redis vars: redis_slaveof: "{{ server01.stdout }} 6379" when: '"{{ (server01.stdout) }}" != "{{ ansible_hostname }}"'
- I added the when condition to the main role as above and made it to two different roles in a single yml. This method also failed.
- role: davidwittman.redis when: '"{{ (server01.stdout) }}" != "{{ ansible_hostname }}"'
- I defined the "if/else" condition as follows in the variable. But although it seems to have worked, it still gave the error "does not support when conditional". Added it to all servers as well.
- redis_slaveof: "{%if '{{ (server01.stdout) }} != {{ ansible_hostname }}' %} {{ '{{ (server01.stdout) }} 6379' }} {% else %} {{ '' }} {% endif %}"