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

On Thursday, August 15, 2019 at 7:03:50 PM UTC-7, Zolvaring wrote:
>
> 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.
>
> On Thursday, August 15, 2019 at 3:09:03 PM UTC-7, Auralan wrote:
>>
>> 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 
>> <configuration>\\n <vlans>\\n <vlan>\\n <name>some_vlan</name>\\n 
>> <vlan-id>2141</vlan-id>\\n </vlan>\\n </vlans>\\n </configuration>"
>>         }
>>     ]
>> }
>>
>>   - 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.
>>
>>

-- 
You received this message because you are subscribed to the Google Groups 
"Ansible Project" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to ansible-project+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/ansible-project/3eb306e4-a3b8-404d-9835-e2b7b278e190%40googlegroups.com.

Reply via email to