Thanks for your response, 

I have still issue when there are two elements matched in regex pattern. 
please help

Playbook:
---
- name: list regex compare
  hosts: localhost
  gather_facts: no
  connection: local

  vars:
    list1:
      - linux.*
      - 6.*
      - mysql|python|gvim

    list2:
      - linux
      - linux linux
      - 6.0.2
      - msql
  tasks:

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

     - set_fact:
         list3: "{{ list3|default([]) + [item.0 is match(item.1)] }}"
       loop: "{{ list2|zip(list1)|list }}"

     - debug:
         msg: "{{ list3 is all  }}"

output:
TASK [debug] 
****************************************************************************************************************************************************************
Wednesday 12 August 2020  05:50:20 -0400 (0:00:00.060)       0:00:00.060 
******
ok: [localhost] => (item=[u'linux', u'linux.*']) => {
    "msg": true
}
ok: [localhost] => (item=[u'linux 1', u'6.*']) => {
    "msg": false
}
ok: [localhost] => (item=[u'6.0.2', u'mysql|python|gvim']) => {
    "msg": false
}

TASK [set_fact] 
*************************************************************************************************************************************************************
Wednesday 12 August 2020  05:50:20 -0400 (0:00:00.049)       0:00:00.110 
******
ok: [localhost] => (item=[u'linux', u'linux.*'])
ok: [localhost] => (item=[u'linux 1', u'6.*'])
ok: [localhost] => (item=[u'6.0.2', u'mysql|python|gvim'])

TASK [debug] 
****************************************************************************************************************************************************************
Wednesday 12 August 2020  05:50:20 -0400 (0:00:00.054)       0:00:00.164 
******
ok: [localhost] => {
    "msg": false
}

PLAY RECAP 
******************************************************************************************************************************************************************
localhost                  : ok=3    changed=0    unreachable=0    failed=0



On Wednesday, August 12, 2020 at 11:05:09 AM UTC+5:30, Vladimir Botka wrote:
>
> On Tue, 11 Aug 2020 21:29:40 -0700 (PDT) 
> rajthecomputerguy <[email protected] <javascript:>> wrote: 
>
> > ... compare two lists 
> > 
> > list1: 
> >    - linux.* 
> >    - 6.* 
> >    - mysql|python|gvim 
> > 
> > list2: 
> >    - linux 
> >    - 6.0.1 
> >    - mysql 
>
> See "Testing strings" and chose from “match”, “search” or “regex”. 
>
> https://docs.ansible.com/ansible/latest/user_guide/playbooks_tests.html#testing-strings
>  
>
> For example, the task below "match" items of the lists 
>
>     - debug: 
>         msg: "{{ item.0 is match(item.1) }}" 
>       loop: "{{ list2|zip(list1)|list }}" 
>
> If "compare two lists" means you want to know whether all items match 
> or not, create new list and test all items in it at once. For example 
>
> https://docs.ansible.com/ansible/latest/user_guide/playbooks_tests.html#test-if-a-list-contains-a-value
>  
>
>     - set_fact: 
>         list3: "{{ list3|default([]) + 
>                    [item.0 is match(item.1)] }}" 
>       loop: "{{ list2|zip(list1)|list }}" 
>     - debug: 
>         msg: "{{ list3 is all }}" 
>
>
> -- 
> Vladimir Botka 
>

-- 
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 [email protected].
To view this discussion on the web visit 
https://groups.google.com/d/msgid/ansible-project/5f7600ca-d96d-47e2-b639-ed581c754440o%40googlegroups.com.

Reply via email to