# bind loopback and default IP

**URL:** <https://forum.ansible.com/t/bind-loopback-and-default-ip/33918>\
**Category:** Ansible Project\
**Created:** [September 29, 2020, 1:21am UTC](https://forum.ansible.com/t/bind-loopback-and-default-ip/33918 "2020-09-29T01:21:43Z")\
**Posts on this page:** 12\
**Page:** 1

<div class="post-metadata">

**Author:** ![Andrew\_Meyer1](https://avatars.discourse-cdn.com/v4/letter/a/df705f/32.png) [@Andrew\_Meyer1](https://forum.ansible.com/u/Andrew_Meyer1)\
**Post date:** [September 29, 2020, 1:21am UTC](https://forum.ansible.com/t/bind-loopback-and-default-ip/33918/1 "2020-09-29T01:21:43Z")

</div>

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!

---

<div class="post-metadata">

**Author:** ![Dick\_Visser](https://avatars.discourse-cdn.com/v4/letter/d/d9b06d/32.png) [@Dick\_Visser](https://forum.ansible.com/u/Dick_Visser)\
**Post date:** [September 29, 2020, 5:22am UTC](https://forum.ansible.com/t/bind-loopback-and-default-ip/33918/2 "2020-09-29T05:22:56Z")

</div>

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 }}

---

<div class="post-metadata">

**Author:** ![Andrew\_Meyer1](https://avatars.discourse-cdn.com/v4/letter/a/df705f/32.png) [@Andrew\_Meyer1](https://forum.ansible.com/u/Andrew_Meyer1)\
**Post date:** [September 29, 2020, 2:24pm UTC](https://forum.ansible.com/t/bind-loopback-and-default-ip/33918/3 "2020-09-29T14:24:37Z")

</div>

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 }}

---

<div class="post-metadata">

**Author:** ![racke](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/racke/32/4767_2.png) [@racke](https://forum.ansible.com/u/racke)\
**Post date:** [September 29, 2020, 2:28pm UTC](https://forum.ansible.com/t/bind-loopback-and-default-ip/33918/4 "2020-09-29T14:28:20Z")

</div>

> 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  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Racke

---

<div class="post-metadata">

**Author:** ![Dick\_Visser](https://avatars.discourse-cdn.com/v4/letter/d/d9b06d/32.png) [@Dick\_Visser](https://forum.ansible.com/u/Dick_Visser)\
**Post date:** [September 29, 2020, 2:59pm UTC](https://forum.ansible.com/t/bind-loopback-and-default-ip/33918/5 "2020-09-29T14:59:52Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![Andrew\_Meyer1](https://avatars.discourse-cdn.com/v4/letter/a/df705f/32.png) [@Andrew\_Meyer1](https://forum.ansible.com/u/Andrew_Meyer1)\
**Post date:** [September 29, 2020, 3:32pm UTC](https://forum.ansible.com/t/bind-loopback-and-default-ip/33918/6 "2020-09-29T15:32:16Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![racke](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/racke/32/4767_2.png) [@racke](https://forum.ansible.com/u/racke)\
**Post date:** [September 29, 2020, 3:37pm UTC](https://forum.ansible.com/t/bind-loopback-and-default-ip/33918/7 "2020-09-29T15:37:10Z")

</div>

> 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  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Racke

---

<div class="post-metadata">

**Author:** ![Andrew\_Meyer1](https://avatars.discourse-cdn.com/v4/letter/a/df705f/32.png) [@Andrew\_Meyer1](https://forum.ansible.com/u/Andrew_Meyer1)\
**Post date:** [September 30, 2020, 3:54pm UTC](https://forum.ansible.com/t/bind-loopback-and-default-ip/33918/8 "2020-09-30T15:54:05Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![racke](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/racke/32/4767_2.png) [@racke](https://forum.ansible.com/u/racke)\
**Post date:** [September 30, 2020, 3:59pm UTC](https://forum.ansible.com/t/bind-loopback-and-default-ip/33918/9 "2020-09-30T15:59:56Z")

</div>

> 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  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Racke

---

<div class="post-metadata">

**Author:** ![Dick\_Visser](https://avatars.discourse-cdn.com/v4/letter/d/d9b06d/32.png) [@Dick\_Visser](https://forum.ansible.com/u/Dick_Visser)\
**Post date:** [September 30, 2020, 4:34pm UTC](https://forum.ansible.com/t/bind-loopback-and-default-ip/33918/10 "2020-09-30T16:34:33Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![racke](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/racke/32/4767_2.png) [@racke](https://forum.ansible.com/u/racke)\
**Post date:** [September 30, 2020, 4:36pm UTC](https://forum.ansible.com/t/bind-loopback-and-default-ip/33918/11 "2020-09-30T16:36:23Z")

</div>

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

Or looking at the wrong server?

Regards  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Racke

---

<div class="post-metadata">

**Author:** ![Andrew\_Meyer1](https://avatars.discourse-cdn.com/v4/letter/a/df705f/32.png) [@Andrew\_Meyer1](https://forum.ansible.com/u/Andrew_Meyer1)\
**Post date:** [September 30, 2020, 4:52pm UTC](https://forum.ansible.com/t/bind-loopback-and-default-ip/33918/12 "2020-09-30T16:52:39Z")

</div>

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.
