format List comparison ansible

137 views
Skip to first unread message

Rakesh Parida

unread,
Jan 30, 2020, 7:49:02 AM1/30/20
to Ansible Project
Hi ,

I want to compare two lists . But while doing this the list1 item loops over each item of list2. I just want item of list 1 just to compare between matching item in list2 and not with others.

my playbook:
- hosts: localhost
  tasks:
    - name: Create a list1
      set_fact:
        list1:
          - dp-steps-common-3.0.12
          - iam-python-common-1.0.1
          - iam-service-default-3.1.37
        list2:
          - dp-steps-common-3.0.15-1911280809_d103a.noarch
          - iam-python-common-1.0.1-1910242116.noarch
          - iam-service-default-3.1.37-1911141021_63e48.noarch

    - name: If the List comparision is equal
      debug:
        msg: " {{ item[0] }} is equal to {{ item[1] }}"
      with_nested:
        - "{{ list1 }}"
        - "{{ list2 }}"
      when: (item[0] in item[1])

    - name: If the List comparision is not equal
      debug:
        msg: " {{ item[0] }} is not equal to {{ item[1] }}"
      with_nested:
        - "{{ list1 }}"
        - "{{ list2 }}"
#      when: (item[0] not in item[1])
      when:  list1 | difference(list2)

o/p:
TASK [If the List comparision is equal] *****************************************************************************************************************************************************
skipping: [localhost] => (item=[u'dp-steps-common-3.0.12', u'dp-steps-common-3.0.15-1911280809_d103a.noarch'])
skipping: [localhost] => (item=[u'dp-steps-common-3.0.12', u'iam-python-common-1.0.1-1910242116.noarch'])
skipping: [localhost] => (item=[u'dp-steps-common-3.0.12', u'iam-service-default-3.1.37-1911141021_63e48.noarch'])
skipping: [localhost] => (item=[u'iam-python-common-1.0.1', u'dp-steps-common-3.0.15-1911280809_d103a.noarch'])
ok: [localhost] => (item=[u'iam-python-common-1.0.1', u'iam-python-common-1.0.1-1910242116.noarch']) => {
    "msg": " iam-python-common-1.0.1 is equal to iam-python-common-1.0.1-1910242116.noarch"
}
skipping: [localhost] => (item=[u'iam-python-common-1.0.1', u'iam-service-default-3.1.37-1911141021_63e48.noarch'])
skipping: [localhost] => (item=[u'iam-service-default-3.1.37', u'dp-steps-common-3.0.15-1911280809_d103a.noarch'])
skipping: [localhost] => (item=[u'iam-service-default-3.1.37', u'iam-python-common-1.0.1-1910242116.noarch'])
ok: [localhost] => (item=[u'iam-service-default-3.1.37', u'iam-service-default-3.1.37-1911141021_63e48.noarch']) => {
    "msg": " iam-service-default-3.1.37 is equal to iam-service-default-3.1.37-1911141021_63e48.noarch"
}

TASK [If the List comparision is not equal] *************************************************************************************************************************************************
ok: [localhost] => (item=[u'dp-steps-common-3.0.12', u'dp-steps-common-3.0.15-1911280809_d103a.noarch']) => {
    "msg": " dp-steps-common-3.0.12 is not equal to dp-steps-common-3.0.15-1911280809_d103a.noarch"
}
ok: [localhost] => (item=[u'dp-steps-common-3.0.12', u'iam-python-common-1.0.1-1910242116.noarch']) => {
    "msg": " dp-steps-common-3.0.12 is not equal to iam-python-common-1.0.1-1910242116.noarch"
}
ok: [localhost] => (item=[u'dp-steps-common-3.0.12', u'iam-service-default-3.1.37-1911141021_63e48.noarch']) => {
    "msg": " dp-steps-common-3.0.12 is not equal to iam-service-default-3.1.37-1911141021_63e48.noarch"
}
ok: [localhost] => (item=[u'iam-python-common-1.0.1', u'dp-steps-common-3.0.15-1911280809_d103a.noarch']) => {
    "msg": " iam-python-common-1.0.1 is not equal to dp-steps-common-3.0.15-1911280809_d103a.noarch"
}
ok: [localhost] => (item=[u'iam-python-common-1.0.1', u'iam-python-common-1.0.1-1910242116.noarch']) => {
    "msg": " iam-python-common-1.0.1 is not equal to iam-python-common-1.0.1-1910242116.noarch"
}
ok: [localhost] => (item=[u'iam-python-common-1.0.1', u'iam-service-default-3.1.37-1911141021_63e48.noarch']) => {
    "msg": " iam-python-common-1.0.1 is not equal to iam-service-default-3.1.37-1911141021_63e48.noarch"
}
ok: [localhost] => (item=[u'iam-service-default-3.1.37', u'dp-steps-common-3.0.15-1911280809_d103a.noarch']) => {
    "msg": " iam-service-default-3.1.37 is not equal to dp-steps-common-3.0.15-1911280809_d103a.noarch"
}
ok: [localhost] => (item=[u'iam-service-default-3.1.37', u'iam-python-common-1.0.1-1910242116.noarch']) => {
    "msg": " iam-service-default-3.1.37 is not equal to iam-python-common-1.0.1-1910242116.noarch"
}
ok: [localhost] => (item=[u'iam-service-default-3.1.37', u'iam-service-default-3.1.37-1911141021_63e48.noarch']) => {
    "msg": " iam-service-default-3.1.37 is not equal to iam-service-default-3.1.37-1911141021_63e48.noarch"
}


Desired o/p of last task:
ok: [localhost] => (item=[u'dp-steps-common-3.0.12', u'dp-steps-common-3.0.15-1911280809_d103a.noarch']) => {
    "msg": " dp-steps-common-3.0.12 is not equal to dp-steps-common-3.0.15-1911280809_d103a.noarch"
 

Vladimir Botka

unread,
Jan 30, 2020, 8:51:06 AM1/30/20
to Rakesh Parida, ansible...@googlegroups.com
On Thu, 30 Jan 2020 04:49:02 -0800 (PST)
Rakesh Parida <rakeshp...@gmail.com> wrote:

> Hi ,
>
> I want to compare two lists.
> [...]
> my playbook:
> - hosts: localhost
> tasks:
> - name: Create a list1
> set_fact:
> list1:
> - dp-steps-common-3.0.12
> - iam-python-common-1.0.1
> - iam-service-default-3.1.37
> list2:
> - dp-steps-common-3.0.15-1911280809_d103a.noarch
> - iam-python-common-1.0.1-1910242116.noarch
> - iam-service-default-3.1.37-1911141021_63e48.noarch

Is this what you're looking for?

- debug:
msg: "{{ item }} is in the list2"
loop: "{{ list1 }}"
when: list2|select('match', item)|list

gives

"msg": "iam-python-common-1.0.1 is in the list2"
"msg": "iam-service-default-3.1.37 is in the list2

HTH,

-vlado

Rakesh Parida

unread,
Jan 30, 2020, 9:11:34 AM1/30/20
to Ansible Project
HI Vladimir,

Thanksfor your reply.

Actually this is not what i want , i just want my item1 to not to iterate over the loop and compare every item, it should only compare the relative item2.
PLs check my o/p.
I just only want the o/p:
"msg": " dp-steps-common-3.0.12 is not equal to dp-steps-common-3.0.15-1911280809_d103a.noarch"

Regards
Rakesh

Vladimir Botka

unread,
Jan 30, 2020, 10:17:37 AM1/30/20
to Rakesh Parida, ansible...@googlegroups.com
On Thu, 30 Jan 2020 06:11:34 -0800 (PST)
Rakesh Parida <rakeshp...@gmail.com> wrote:

> Actually this is not what i want , i just want my item1 to not to iterate
> over the loop and compare every item, it should only compare the relative
> item2.
> PLs check my o/p.
> I just only want the o/p:
> "msg": " dp-steps-common-3.0.12 is not equal to
> dp-steps-common-3.0.15-1911280809_d103a.noarch"

OK. Is this probably what you're looking for?

- debug:
msg: "{{ item.0 }} is not equal to {{ item.1 }}"
loop: "{{ list1|zip(list2)|list }}"
when: item.0.split('-')[-1] is not version(item.1.split('-')[-2])

gives

"msg": "dp-steps-common-3.0.12 is not equal to
dp-steps-common-3.0.15-1911280809_d103a.noarch"


The order and the format of the items is significant. HTH,

-vlado

Rakesh Parida

unread,
Jan 30, 2020, 1:08:03 PM1/30/20
to Ansible Project
Hi Vladimir,

I have another query . Now i altered the sequence of items in the  lists, the iam python of list1 compares with the dp-steps of list
Is there any way the list comparision would be successful even if the sequence is altered.


        list1:
          - iam-python-common-1.0.0
          - iam-service-default-3.1.42
          - dp-steps-common-3.0.12
        list2:
          - dp-steps-common-3.0.15-1911280809_d103a.noarch
          - iam-service-default-3.1.37-1911141021_63e48.noarch
          - iam-python-common-1.0.1-1910242116.noarch

Vladimir Botka

unread,
Jan 30, 2020, 1:29:37 PM1/30/20
to Rakesh Parida, ansible...@googlegroups.com
On Thu, 30 Jan 2020 10:08:02 -0800 (PST)
Rakesh Parida <rakeshp...@gmail.com> wrote:

> Is there any way the list comparision would be successful even if the
> sequence is altered.
>
> list1:
> - iam-python-common-1.0.0
> - iam-service-default-3.1.42
> - dp-steps-common-3.0.12
> list2:
> - dp-steps-common-3.0.15-1911280809_d103a.noarch
> - iam-service-default-3.1.37-1911141021_63e48.noarch
> - iam-python-common-1.0.1-1910242116.noarch

Sure. Sort the lists. For example

- debug:
msg: "{{ item.0 }} is not equal to {{ item.1 }}"
loop: "{{ list1|sort|zip(list2|sort)|list }}"
when: item.0.split('-')[-1] is not version(item.1.split('-')[-2])

gives

"msg": "dp-steps-common-3.0.12 is not equal to
dp-steps-common-3.0.15-1911280809_d103a.noarch"
"msg": "iam-python-common-1.0.0 is not equal to
iam-python-common-1.0.1-1910242116.noarch"
"msg": "iam-service-default-3.1.42 is not equal to
iam-service-default-3.1.37-1911141021_63e48.noarch"

HTH,

-vlado

Rakesh Parida

unread,
Jan 30, 2020, 1:40:57 PM1/30/20
to Ansible Project
Hi Vladimir,

Thanks for the reply. Awsome.
Its exactly what i want to achieve. Thanks for your time . Highly appreciate your help:) 

With Regards
Rakesh

Pawan Kumar

unread,
Jan 30, 2020, 11:20:53 PM1/30/20
to Ansible Project
Thanks Vladimir , 

Extraordinary solution  . 
Just for curiosity , would be please explain the steps in solution . 


warm regards
Pawan 

Vladimir Botka

unread,
Jan 31, 2020, 3:20:27 AM1/31/20
to Pawan Kumar, ansible...@googlegroups.com
On Thu, 30 Jan 2020 20:20:53 -0800 (PST)
Pawan Kumar <pawa...@gmail.com> wrote:

> > >
> > > list1:
> > > - iam-python-common-1.0.0
> > > - iam-service-default-3.1.42
> > > - dp-steps-common-3.0.12
> > > list2:
> > > - dp-steps-common-3.0.15-1911280809_d103a.noarch
> > > - iam-service-default-3.1.37-1911141021_63e48.noarch
> > > - iam-python-common-1.0.1-1910242116.noarch

> >
> > - debug:
> > msg: "{{ item.0 }} is not equal to {{ item.1 }}"
> > loop: "{{ list1|sort|zip(list2|sort)|list }}"
> > when: item.0.split('-')[-1] is not version(item.1.split('-')[-2])
> >
> > gives
> >
> > "msg": "dp-steps-common-3.0.12 is not equal to
> > dp-steps-common-3.0.15-1911280809_d103a.noarch"
> > "msg": "iam-python-common-1.0.0 is not equal to
> > iam-python-common-1.0.1-1910242116.noarch"
> > "msg": "iam-service-default-3.1.42 is not equal to
> > iam-service-default-3.1.37-1911141021_63e48.noarch"

> Just for curiosity , would be please explain the steps in solution.

Sure. The steps are self-explaining, I think.

1) See "zip"
https://docs.ansible.com/ansible/devel/user_guide/playbooks_filters.html#combining-items-from-multiple-lists-zip-and-zip-longest

- debug:
msg: "{{ item.0 }}
{{ item.1 }}"
loop: "{{ list1|sort|zip(list2|sort)|list }}"

gives

"msg": "dp-steps-common-3.0.12 dp-steps-common-3.0.15-1911280809_d103a.noarch"
"msg": "iam-python-common-1.0.0 iam-python-common-1.0.1-1910242116.noarch"
"msg": "iam-service-default-3.1.42 iam-service-default-3.1.37-1911141021_63e48.noarch"


2) See "split" and "Common Sequence Operations"
https://docs.python.org/3/library/stdtypes.html?highlight=split#str.split
https://docs.python.org/3/library/stdtypes.html?highlight=split#common-sequence-operations

- debug:
msg: "{{ item.0.split('-')[-1] }}
{{ item.1.split('-')[-2] }}"
loop: "{{ list1|sort|zip(list2|sort)|list }}"

gives

"msg": "3.0.12 3.0.15"
"msg": "1.0.0 1.0.1"
"msg": "3.1.42 3.1.37"


3) See "Version Comparison"
https://docs.ansible.com/ansible/latest/user_guide/playbooks_tests.html#version-comparison

HTH,

-vlado

Rakesh Parida

unread,
Feb 6, 2020, 3:42:29 AM2/6/20
to Ansible Project
Hi Vladimir,

If my items of lists are not fixed on both the list ,  how do i compare them:
Now i have one more issue on the LIST comparison:
If the numbers of items in list is not equal is there way to compare:

suppose my lists are:
      list1
            "dp-steps-common-3.0.12", 
            "iam-python-common-1.0.1", 
            "iam-service-default-3.1.37"
      list2:
            "dp-steps-common-3.0.15-1911280809_d103a.noarch", 
            "iam-service-default-3.1.37-1911141021_63e48.noarch"

    - name: If the List comparision is not equal
      debug:
        msg: "{{ item.0 }} is not equal to {{ item.1 }}"
      loop: "{{ list1|sort|zip(list2|sort)|list }}"
      when: item.0.split('-')[-1] is not version(item.1.split('-')[-2])
     
As you can see the items are not same in both list.
  

Vladimir Botka

unread,
Feb 6, 2020, 5:56:16 AM2/6/20
to Rakesh Parida, ansible...@googlegroups.com
On Thu, 6 Feb 2020 00:42:28 -0800 (PST)
Rakesh Parida <rakeshp...@gmail.com> wrote:

> If the numbers of items in list is not equal is there way to compare:
> suppose my lists are:
> list1
> "dp-steps-common-3.0.12",
> "iam-python-common-1.0.1",
> "iam-service-default-3.1.37"
> list2:
> "dp-steps-common-3.0.15-1911280809_d103a.noarch",
> "iam-service-default-3.1.37-1911141021_63e48.noarch"
>
> - name: If the List comparision is not equal
> debug:
> msg: "{{ item.0 }} is not equal to {{ item.1 }}"
> loop: "{{ list1|sort|zip(list2|sort)|list }}"
> when: item.0.split('-')[-1] is not version(item.1.split('-')[-2])

Do you know what you would like to get as a result?

Rakesh Parida

unread,
Feb 6, 2020, 6:08:25 AM2/6/20
to Ansible Project
Hi Vladimir,

I would like my o/p as below:
dp-steps-common-3.0.12 not equal to dp-steps-common-3.0.15-1911280809_d103a.noarch

iam-service-default-3.1.37 is equal to iam-service-default-3.1.37-1911141021_63e48.noarch

iam-python-common-1.0.1 not found in list2

vishwas govekar

unread,
Feb 6, 2020, 8:36:36 AM2/6/20
to ansible...@googlegroups.com
I am working on Apache storm project using Ansible scripts ,I have stormconfproperties.yml which has variable values for supervisor_slots_ports

image.png
In my template files I have to pass it as variables .Can you help me for supervisor.slots.ports  as I have  done for the remaining but unable to do that
I have tried several times but failed 


image.png

--
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-proje...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/ansible-project/b8e2d62c-ae99-4c0e-8cda-4ac4056bf766%40googlegroups.com.

vishwas govekar

unread,
Feb 6, 2020, 9:30:24 AM2/6/20
to ansible...@googlegroups.com

Dick Visser

unread,
Feb 6, 2020, 9:57:04 AM2/6/20
to ansible...@googlegroups.com
Hi 

You already are engaged in a conversation on this list. Branching that into another by verbatim forwarding a single message without context isn’t going to help you get better responses.

Please post proper questions, or just wait until the volunteers here respond to your original question. 

FYI this is a best effort list, no SLA etc.



--
Sent from a mobile device - please excuse the brevity, spelling and punctuation.

Rakesh Parida

unread,
Feb 6, 2020, 10:08:12 AM2/6/20
to Ansible Project
Hi Vladimir, 
I have tried  a lot but unable to find solution  for my issue. Pls help on this

--
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-proje...@googlegroups.com.

vishwas govekar

unread,
Feb 6, 2020, 10:42:21 AM2/6/20
to ansible...@googlegroups.com

Vladimir Botka

unread,
Feb 6, 2020, 10:57:26 AM2/6/20
to Rakesh Parida, ansible...@googlegroups.com
On Thu, 6 Feb 2020 20:37:37 +0530
Rakesh Parida <rakeshp...@gmail.com> wrote:

> > I would like my o/p as below:
> > dp-steps-common-3.0.12 not equal to
> > dp-steps-common-3.0.15-1911280809_d103a.noarch
> > iam-service-default-3.1.37 is equal to
> > iam-service-default-3.1.37-1911141021_63e48.noarch
> > iam-python-common-1.0.1 not found in list2
> >
> >> > If the numbers of items in list is not equal is there way to compare:
> >> > suppose my lists are:
> >> > list1
> >> > "dp-steps-common-3.0.12",
> >> > "iam-python-common-1.0.1",
> >> > "iam-service-default-3.1.37"
> >> > list2:
> >> > "dp-steps-common-3.0.15-1911280809_d103a.noarch",
> >> > "iam-service-default-3.1.37-1911141021_63e48.noarch"

Sure. Transform the data first. For example

- set_fact:
my_pkgs: "{{ my_pkgs|default({})|
combine({pkg_name: {'list1': item, 'list2': pkg2}}) }}"
vars:
pkg_name: "{{ item.split('-')[:-1]|join('-') }}"
pkg2: "{{ list2|select('search', pkg_name)|list|first|default('') }}"
loop: "{{ list1 }}"
- debug:
var: my_pkgs

give

"my_pkgs": {
"dp-steps-common": {
"list1": "dp-steps-common-3.0.12",
"list2": "dp-steps-common-3.0.15-1911280809_d103a.noarch"
},
"iam-python-common": {
"list1": "iam-python-common-1.0.1",
"list2": ""
},
"iam-service-default": {
"list1": "iam-service-default-3.1.37",
"list2": "iam-service-default-3.1.37-1911141021_63e48.noarch"
}
}

Then the manipulation with the data is trivial. For example

- debug:
msg: |
{% if item.value.list2 %}
{% if item.value.list1 in item.value.list2 %}
{{ item.value.list1 }} is equal to {{ item.value.list2 }}
{% else %}
{{ item.value.list1 }} not equal to {{ item.value.list2 }}
{% endif %}
{% else %}
{{ item.value.list1 }} not found in list2
{% endif %}
loop: "{{ my_pkgs|dict2items }}"

gives you what you want

"msg": "iam-python-common-1.0.1 not found in list2\n"
"msg": "iam-service-default-3.1.37 is equal to
iam-service-default-3.1.37-1911141021_63e48.noarch\n"
"msg": "dp-steps-common-3.0.12 not equal to
dp-steps-common-3.0.15-1911280809_d103a.noarch\n"

HTH,

-vlado

Rakesh Parida

unread,
Feb 6, 2020, 5:43:09 PM2/6/20
to Vladimir Botka, Ansible Project
Hi Vladimir,
This Solutioñ works.
Thanks for your extended  help on this.
I just tweaked something to match my requirements, overall your solution works well for me.
Regards 
Rakesh
Reply all
Reply to author
Forward
0 new messages