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.
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.
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:
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:
^ 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.