how to compare lists whose elements are regular expressions in Ansible?

1,136 views
Skip to first unread message

rajthecomputerguy

unread,
Aug 12, 2020, 12:29:40 AM8/12/20
to Ansible Project
Hi Team 

I wish to compare two lists, one list has regex expression and the second has ordinary data and return a match. Please help



e.g.


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

list2:
   - linux
   - 6.0.1
   - mysql
Thanks

Vladimir Botka

unread,
Aug 12, 2020, 1:35:09 AM8/12/20
to rajthecomputerguy, ansible...@googlegroups.com
On Tue, 11 Aug 2020 21:29:40 -0700 (PDT)
rajthecomputerguy <rajtheco...@gmail.com> 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

rajthecomputerguy

unread,
Aug 12, 2020, 5:52:43 AM8/12/20
to Ansible Project
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
Reply all
Reply to author
Forward
0 new messages