bind loopback and default IP

I am writing a redis playbook and I want to have any new redis replication node built listen on the loopback address AND default IP address. I am having trouble trying to figure out how to achieve this. I have it set up in my jijna template like this:

bind {{ hostvars[inventory_hostname][‘ansible_default_ipv4’][‘address’] }}

This is what I have in my defaults/main.yml
redis_bind_interface: 127.0.0.1 “{{ ansible_all_ipv4_addresses }}”

Thanks in advance!

The jinja template doesn't use this 'redis_bind_interface' variable?

But if you want to use it, you should define it like this in your
playbook (or role, judging by the file name):

redis_bind_interface: "127.0.0.1 {{ ansible_all_ipv4_addresses | join (' ') }}"

Note that ansible_all_ipv4_addresses is a list, so you have to join it
using a space.

Then refer to it like this in your template:

bind {{ redis_bind_interface }}

Getting this after running the playbook

fatal: [10.150.1.128]: FAILED! => {
“changed”: false,
“msg”: “AnsibleError: An unhandled exception occurred while templating ‘127.0.0.1 {{ ansible_all_ipv4_address|join[‘0’] }}’. Error was a <class ‘ansible.errors.AnsibleError’>, original message: template error while templating string: expected token ‘end of print statement’, got ‘[’. String: 127.0.0.1 {{ ansible_all_ipv4_address|join[‘0’] }}”
}

Thisi s from my defaults/main.yml
redis_bind_interface: “127.0.0.1 {{ ansible_all_ipv4_address|join[‘0’] }}”

This is from the templated jinja
bind {{ redis_bind_interface }}

Getting this after running the playbook

fatal: [10.150.1.128]: FAILED! => {
"changed": false,
"msg": "AnsibleError: An unhandled exception occurred while templating '127.0.0.1 {{
ansible_all_ipv4_address|join['0'] }}'. Error was a <class 'ansible.errors.AnsibleError'>, original message: template
error while templating string: expected token 'end of print statement', got '['. String: 127.0.0.1 {{
ansible_all_ipv4_address|join['0'] }}"
}

Thisi s from my defaults/main.yml
redis_bind_interface: "127.0.0.1 {{ ansible_all_ipv4_address|join['0'] }}"

This is from the templated jinja
bind {{ redis_bind_interface }}

Why do you use join['0'] ?

Regards
         Racke

That is because you did not use the instruction that I gave, but
instead mangled it so that it now gives errors.
Just copy paste what I wrote for redis_bind_interface.

I ended up doing this:
redis_bind_interface: “127.0.0.1 {{ ansible_default_ipv4.address }}”

However now I’m having issues copying the config file to the newly built server. It shows that it is making the change to the /etc/redis.conf but the change doesn’t make it. I have tried renaming the current config file and it still won’t do it. I have full sudo rights ( my own lab box) and I have a become directive in my template block.

  • name: Ensure Redis is configured.
    template:
    src: redis.conf.j2
    dest: “{{ redis_conf_path }}”
    mode: 0640
    owner: redis
    group: root
    backup: yes
    force: yes
    become: true
    notify: restart redis

I ended up doing this:
redis_bind_interface: "127.0.0.1 {{ ansible_default_ipv4.address }}"

However now I'm having issues copying the config file to the newly built server. It shows that it is making the change
to the /etc/redis.conf but the change doesn't make it. I have tried renaming the current config file and it still won't
do it. I have full sudo rights ( my own lab box) and I have a become directive in my template block.

- name: Ensure Redis is configured.
template:
src: redis.conf.j2
dest: "{{ redis_conf_path }}"
mode: 0640
owner: redis
group: root
backup: yes
force: yes
become: true
notify: restart redis

Can you show the output of this task with using --diff when executing your play book?

Regards
        Racke

TASK [redis : Ensure Redis is configured.] ************************************************************************************************************************************************************************
task path: /opt/syseng/automation/ansible/playbooks/roles/redis/tasks/main.yml:24
NOTIFIED HANDLER redis : restart redis for 10.150.1.128
— before: /etc/redis.conf
+++ after: /home/andrew.meyer/.ansible/tmp/ansible-local-2556111vlumpr9k/tmpvxxyrw0q/redis.conf.j2
@@ -0,0 +1,44 @@
+# Ansible managed

TASK [redis : Ensure Redis is configured.]
************************************************************************************************************************************************************************
task path: /opt/syseng/automation/ansible/playbooks/roles/redis/tasks/main.yml:24
NOTIFIED HANDLER redis : restart redis for 10.150.1.128
--- before: /etc/redis.conf
+++ after: /home/andrew.meyer/.ansible/tmp/ansible-local-2556111vlumpr9k/tmpvxxyrw0q/redis.conf.j2
@@ -0,0 +1,44 @@
+# Ansible managed
+
+daemonize no
+pidfile /var/run/redis/redis.pid
+port 6379
+bind 127.0.0.1 10.150.1.128
+
+
+supervisd systemd
+
+timeout 300
+
+protected mode False
+
+loglevel notice
+logfile /var/log/redis/redis-server.log
+
+# To enable logging to the system logger, just set 'syslog-enabled' to yes,
+# and optionally update the other syslog parameters to suit your needs.
+# syslog-enabled no
+# syslog-ident redis
+# syslog-facility local0
+
+databases 16
+
+save 900 1
+save 300 10
+save 60 10000
+
+rdbcompression yes
+dbfilename dump.rdb
+dir /var/lib/redis
+
+# maxclients 128
+
+
+appendonly yes
+appendfsync everysec
+no-appendfsync-on-rewrite no
+appendfilename appendonly.aof
+
+
+
+

changed: [10.150.1.128] => {"changed": true}

So you are saying that content doesn't end up in /etc/redis.conf on your server (IP 10.150.1.128) ?

Regards
       Racke

Are you sure you're not running in check mode?

Are you sure you're not running in check mode?

Or looking at the wrong server?

Regards
        Racke

Stupid me I was doing it in check mode…but I tried it without it yesterday and it still wouldn’t make the changes.

Thanks for your help! On to the next hurdle w/ this playbook.