List comparison and retireval of items on match

Hi All,

Good day to all.

I am currently working on a scenario where i need to compare two lists and print out the items which match on both lists .

List1 : [‘dba’,‘admin’,‘developer’]
List2 : [’ user1: dba admin’ , ‘user2 : dba developer’, ‘user3 : operator’]

So as per my scenario in comparing above two list sets , the output should be
user1
user2
as both users have dba/admin/developer item in there strings of list 1.

i tried below code but it doesnt seem to give me the output i need:

  • name: checking
    debug:
    msg: “item”
    with_nested:
  • “{{ VlclNewList }}”
  • “{{ VlclOldList }}”
    when: “(item[1] | difference(item[0]))”

Any help is highly appreciated.
Thanks.

Try

    - set_fact:
        List3: "{{ List2|
                   select('search', my_regex1)|
                   map('regex_replace', my_regex2, my_replace)|
                   map('trim')|
                   list }}"
      vars:
        my_regex1: '{{ List1|join("|") }}'
        my_regex2: '^(.*):(.*)$'
        my_replace: '\1'

Try

    - set_fact:
        List3: "{{ List3|default() + [key|trim] }}"
      loop: "{{ List2 }}"
      vars:
        key: "{{ item.split(':').0 }}"
        val: "{{ item.split(':').1.split() }}"
      when: val|intersect(List1)