# Difficulty differencing list with integers vs list with strings.

**URL:** <https://forum.ansible.com/t/difficulty-differencing-list-with-integers-vs-list-with-strings/31053>\
**Category:** Ansible Project\
**Created:** [August 15, 2019, 10:09pm UTC](https://forum.ansible.com/t/difficulty-differencing-list-with-integers-vs-list-with-strings/31053 "2019-08-15T22:09:03Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Auralan](https://avatars.discourse-cdn.com/v4/letter/a/7cd45c/32.png) [@Auralan](https://forum.ansible.com/u/Auralan)\
**Post date:** [August 15, 2019, 10:09pm UTC](https://forum.ansible.com/t/difficulty-differencing-list-with-integers-vs-list-with-strings/31053/1 "2019-08-15T22:09:03Z")

</div>

Hi everyone,

I’ve been struggling to get the difference between 2 lists in Ansible.  
List 1 contains numbers as integers. List 2 contains numbers as strings.  
These obviously cannot be filtered with difference().

Does anyone know a workaround? Or a point where I can make a change in my parsing to remediate this difference?  
We are using Ansible 2.7.10. Upgrading is an option if needed. My setup is below. Thank you for your time!

List 1 comes from parsing a JSON document.

- name: “Create list from JSON source.”  
set\_fact:  
cmdb\_list: “{{ cmdb\_call.json.results | map(attribute=‘vid’) | list }}”

# Debug of cmdb\_list. Output shortened for readability.

ok: [localhost] =\> {  
“msg”: [  
[  
2141,  
2142,  
2143  
]  
]  
}

List 2 comes from parsing a XML document in 2 steps.

- name: “Create list from XML source. Step 1.”  
xml:  
xmlstring: “{{ device\_call.ansible\_facts.ansible\_net\_config }}”  
xpath: //vlan-id  
content: “text”  
register: device\_call\_filtered

# Debug of device\_call\_filtered. Output of matches and xmlstring keys shortened for readability.

ok: [some\_switch] =\> {  
“msg”: [  
{  
“actions”: {  
“namespaces”: {},  
“state”: “present”,  
“xpath”: “//vlan-id”  
},  
“changed”: false,  
“count”: 18,  
“failed”: false,  
“matches”: [  
{  
“vlan-id”: “2141”  
},  
{  
“vlan-id”: “2056”  
}  
],  
“msg”: 18,  
“xmlstring”: “\<?xml version='1.0' encoding='UTF-8'?\>\n \n \n \n some\_vlan\n 2141\n \n \n ”  
}  
]  
}

- name: “Create list of configured VLANs per Device.”  
set\_fact:  
device\_list: “{{ device\_call\_filtered.matches | map(attribute=‘vlan-id’) | list }}”  
delegate\_facts: true

# Debug of device\_list. Output shortened for readability.

ok: [some\_switch] =\> {  
“msg”: [  
[  
“2141”,  
“2056”  
]  
]  
}

- name: “Get difference.”  
set\_fact:  
difference\_list: “{{ cmdb\_list | difference(device\_list) }}”

^ So that’s not going to work in this state.

---

<div class="post-metadata">

**Author:** ![gaurav\_thosani](https://avatars.discourse-cdn.com/v4/letter/g/9dc877/32.png) [@gaurav\_thosani](https://forum.ansible.com/u/gaurav_thosani)\
**Post date:** [August 15, 2019, 11:24pm UTC](https://forum.ansible.com/t/difficulty-differencing-list-with-integers-vs-list-with-strings/31053/2 "2019-08-15T23:24:31Z")

</div>

Auralan,

---

<div class="post-metadata">

**Author:** ![Auralan](https://avatars.discourse-cdn.com/v4/letter/a/7cd45c/32.png) [@Auralan](https://forum.ansible.com/u/Auralan)\
**Post date:** [August 16, 2019, 12:30am UTC](https://forum.ansible.com/t/difficulty-differencing-list-with-integers-vs-list-with-strings/31053/3 "2019-08-16T00:30:51Z")

</div>

Hello Gaurav,

Thank you for your reply. I should have mentioned that I have previously tried making the int filter work in the “device\_list” task.  
Inserting it just before the list filter, however, doesn’t seem to work. It claims to not accept the resulting integer:

fatal: [some\_switch]: FAILED! =\> {“msg”: “Unexpected templating type error occurred on ({{ device\_call\_filtered.matches | map(attribute=‘vlan-id’) | int | list }}): ‘int’ object is not iterable”}

Hopefully someone can point me towards a syntax which can be used here. I would prefer to take care if this in as few steps as possible.

You did give me the idea for the workaround listed below. Although it is ugly, it does produce the list of integers:

- name: “Generate new list.”  
set\_fact:  
device\_list\_new: “{{ device\_list\_new | default() + [item | int] }}”  
with\_items: “{{ device\_list }}”

However, this ends up creating several thousand steps in the task, depending on the targeted environment. I tried the alternative below to shorten the task, but that just gives me a list with a single 0 in it:

- name: “Generate new list.”  
set\_fact:  
device\_list\_new: “{{ device\_list\_new | default() + [device\_list | int] }}”

Any further suggestions would be much appreciated. Thanks again to anyone taking time to read this.

---

<div class="post-metadata">

**Author:** ![Zolvaring](https://avatars.discourse-cdn.com/v4/letter/z/ce73a5/32.png) [@Zolvaring](https://forum.ansible.com/u/Zolvaring)\
**Post date:** [August 16, 2019, 2:03am UTC](https://forum.ansible.com/t/difficulty-differencing-list-with-integers-vs-list-with-strings/31053/4 "2019-08-16T02:03:50Z")

</div>

Would something like this syntax help?

set\_fact:  
difference\_list: \>-  
{{ numbers\_as\_integers |  
difference(numbers\_as\_strings |  
map(attribute=‘whatever\_attribute\_you\_may\_need’) |  
map(‘int’) |  
list }}

^ What that should do for the list of numbers as strings, is filter dicts into a pure list based on attribute (not clear if you need this or not, if you have a pure list of integers as strings you can remove that part obviously), the second map will apply the int filter to all members of your list, and then finally make it iterable with list. I was only able to test this briefly but it seemed to work for me if I understand what you’re trying to do.

---

<div class="post-metadata">

**Author:** ![Zolvaring](https://avatars.discourse-cdn.com/v4/letter/z/ce73a5/32.png) [@Zolvaring](https://forum.ansible.com/u/Zolvaring)\
**Post date:** [August 16, 2019, 2:07am UTC](https://forum.ansible.com/t/difficulty-differencing-list-with-integers-vs-list-with-strings/31053/5 "2019-08-16T02:07:52Z")

</div>

On closer examination I dropped a parenthesis entering this in, so for clarity:

difference\_list: \>-

{{ numbers\_as\_integers |  
difference(numbers\_as\_strings |  
map(attribute=‘whatever\_attribute\_you\_may\_need’) |  
map(‘int’) |  
list) }}
