How to extract a value of a field within a list of dictionaries when a key matches another value

The playbook below

- name: test with json_query 1/2
  hosts: localhost
  vars:
    domain_definition:
      - name: server11
        cluster: cluster1
        port: '8080'
      - name: server12
        cluster: cluster2
        port: '8090'
      - name: server21
        cluster: cluster3
        port: '9080'
      - name: server22
        cluster: cluster4
        port: '9090'
  tasks:
    - debug:
        msg: "{{ domain_definition|
                 json_query('[?name==`server21`].port') }}"

gives

    "msg": [
        "9080"
    ]

HTH,

  -vlado

"selectattr" below gives the same result

    - debug:
        msg: "{{ domain_definition|
                 selectattr('name', 'regex', '^server21$')|
                 map(attribute= 'port')|
                 list }}"

HTH,

  -vlado

What Vladimir said (twice):

You are using:

debug:
  var:

while you should be doing:

debug:
  msg:

Dick