format List comparison ansible

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"

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

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

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

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

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

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

Thanks Vladimir ,

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

warm regards
Pawan

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

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.

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

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

I am working on Apache storm project using Ansible scripts ,I have stormconfproperties.yml which has variable values for supervisor_slots_ports

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

(attachments)


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.

(attachments)


Hi Vladimir,
I have tried a lot but unable to find solution for my issue. Pls help on this

Thanks for the response

(attachments)


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

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