Q:

json_query uses contains to search item within another list failed

I have list of samples, and i want to select the item whose type is in the wanted_list.
- name: Get item based on anther list
  vars: 
    samples:
      - { item: 10, type: sample-1 },
      - { item: 20, type: sample-11 },
      - { item: 30, type: sample-2 }
    wanted_list: [ sample-11, sample-2 ]
    query: "[?contains('{{wanted_list|to_json}}', type)]"
  debug:
    var: samples | json_query(query)

Result:
TASK: Get item based on anther list
ok: [localhost] => {
    "samples|json_query(query)": [
        {
            "item": 10,
            "type": "sample-1"
        },
        {
            "item": 20,
            "type": "sample-11"
        },
        {
            "item": 30,
            "type": "sample-2"
        }
    ]
}

What i expected is it will only return item 20 and 30. But it actually return all 3. It looks like sample-1 match part of the list item sample-11.
But based on JMESPath doc contains:

boolean contains(array|string $subject, any $search)
If $subject is an array, this function returns true if one of the elements in the array
is equal to the provided $search value.

So sample-1 is not equal to sample-11 and then it should not be selected.

A:

You will want to use the backtick in order to quote literal JSON like that, since the single quote is just for strings
  - name: Get item based on anther list
    vars: 
      samples:
        - { item: 10, type: sample-1 }
        - { item: 20, type: sample-11 }
        - { item: 30, type: sample-2 }
      wanted_list: [ sample-11, sample-2 ]
      query: "[?contains(`{{ wanted_list | to_json }}`, type)]"
    debug:
      msg: '{{ samples | json_query(query) }}'

