# jinja template difference filter

**URL:** <https://forum.ansible.com/t/jinja-template-difference-filter/19247>\
**Category:** Ansible Project\
**Created:** [September 12, 2015, 6:33pm UTC](https://forum.ansible.com/t/jinja-template-difference-filter/19247 "2015-09-12T18:33:37Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![William\_Muriithi](https://avatars.discourse-cdn.com/v4/letter/w/6de8d8/32.png) [@William\_Muriithi](https://forum.ansible.com/u/William_Muriithi)\
**Post date:** [September 12, 2015, 6:33pm UTC](https://forum.ansible.com/t/jinja-template-difference-filter/19247/1 "2015-09-12T18:33:37Z")

</div>

Afternoon,

I have a template where I have a group of 4 IPs. I want to make a  
list of the 3 IPs - These 3 IPs are for the other database systems,  
so need to except the IP of the current database where the template is  
being deployed. The line that does this is below, but over the can¹t  
seem to get it working despite looking at it for a couple of years

/etc/ansible/roles/mysql/templates/template/galera.cnf.j2

Š.

wsrep\_cluster\_address=gcomm://{{databases | join(',') | difference  
(ansible\_ipv4.bond0)}};  
Š..

When I run the playbook, it fails with the following error.

fatal: [192.168.149.241] =\> {'msg': "AnsibleUndefinedVariable: One or  
more undefined variables: 'ansible\_ipv4' is undefined", 'failed':  
True}  
fatal: [192.168.149.241] =\> {'msg': 'One or more items failed.',  
'failed': True, 'changed': False, 'results': [{'msg':  
"AnsibleUndefinedVariable: One or more undefined variables:  
'ansible\_ipv4' is undefined", 'failed':  
True}]} FATAL: all hosts have already failed ‹ aborting

What would be the appropriate way to use difference filter to achieve  
my goal here?

Regards,

William

---

<div class="post-metadata">

**Author:** ![jean-yves](https://avatars.discourse-cdn.com/v4/letter/j/a587f6/32.png) [@jean-yves](https://forum.ansible.com/u/jean-yves)\
**Post date:** [September 12, 2015, 7:07pm UTC](https://forum.ansible.com/t/jinja-template-difference-filter/19247/2 "2015-09-12T19:07:31Z")

</div>

Perhaps, you should have a look at run\_once or/and delegated\_to

Regards,

---

<div class="post-metadata">

**Author:** ![Gerard\_Lynch](https://avatars.discourse-cdn.com/v4/letter/g/958977/32.png) [@Gerard\_Lynch](https://forum.ansible.com/u/Gerard_Lynch)\
**Post date:** [September 13, 2015, 4:48pm UTC](https://forum.ansible.com/t/jinja-template-difference-filter/19247/3 "2015-09-13T16:48:48Z")

</div>

difference takes two lists, so

wsrep\_cluster\_address=gcomm://{{databases | difference([ansible\_ipv4.bond0])}};

join(‘,’) creates a string, you want to use split (or if databases is a list already, nothing)  
note the square brackets around ansible\_ipv4.bond0, basically turning it into a list item.
