Re: [ansible-project] Difficulty differencing list with integers vs list with strings.

2019-08-15 Thread Auralan
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.



On Friday, 16 August 2019 01:24:57 UTC+2, gaurav thosani wrote:
>
> Auralan, 
>
> Wondering if jinja2 typecasting could be attempted for your use case 
> Reference: 
> https://stackoverflow.com/questions/39938323/jinja-convert-string-to-integer?rq=1
>
> Gaurav Thosani 
>
>
>
> On Thu, Aug 15, 2019 at 3:09 PM 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": "\\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.
>>
>> -- 
>> 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...@googlegroups.com .
>> To view this discussion on the web visit 
>> https://groups.google.com/d/msgid/ansible-project/da41df3e-93d3-4745-a98b-4acd7d182cc2%40googlegroups.com
>>  
>> 
>> .
>>
>

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

Re: [ansible-project] Difficulty differencing list with integers vs list with strings.

2019-08-15 Thread gaurav thosani
Auralan,

Wondering if jinja2 typecasting could be attempted for your use case
Reference:
https://stackoverflow.com/questions/39938323/jinja-convert-string-to-integer?rq=1

Gaurav Thosani



On Thu, Aug 15, 2019 at 3:09 PM 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": "\\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.
>
> --
> 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/da41df3e-93d3-4745-a98b-4acd7d182cc2%40googlegroups.com
> 
> .
>

-- 
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/CAGvJ0CVR5u4rs%3DgiNtBW4rGUyMbzLHcaBhgpFPAP8TLe_4Fiig%40mail.gmail.com.


[ansible-project] Difficulty differencing list with integers vs list with strings.

2019-08-15 Thread Auralan
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": "\\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.

-- 
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/da41df3e-93d3-4745-a98b-4acd7d182cc2%40googlegroups.com.