Let’s suppose the following data structure:
        - 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'
The goal is for example to *extract the port number when 
domain_definition.name == 'server21'.*

As simple as it sounds, I tried the following tasks with 'json_query' and 
'selectattr': they all failed:
- name: test with json_query 1/2
  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'
  debug:
        var: "{{ domain_definition | 
json_query('domain_definition[?name==`server21`].port') }}"
  ignore_errors: yes

- name: test with json_query 2/2
  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'
  debug:
        var: "{{ . | 
json_query('domain_definition[?name==`server21`].port') }}"
  ignore_errors: yes

- name: test with selectattr 'match'
  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'
  debug:
        var: "{{ domain_definition | selectattr('name', 'match', 
'^server21$')).port }}"
  ignore_errors: yes

- name: test with selectattr 'match'
  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'
  debug:
        var: "{{ domain_definition | selectattr('name', 'equalto', 
'server21')).port }}"
  ignore_errors: yes

leads to:
ASK [yang : test with json_query 1/2] 
***********************************************************************************************************************
fatal: [XEv_Spine_31]: FAILED! => {
    "msg": "template error while templating string: Expected an expression, 
got 'end of print statement'. String: {{}}"
}
...ignoring

TASK [yang : test with json_query 2/2] 
***********************************************************************************************************************
fatal: [XEv_Spine_31]: FAILED! => {
    "msg": "template error while templating string: unexpected '.'. String: 
{{ . | json_query('domain_definition[?name==`server21`].port') }}"
}
...ignoring

TASK [yang : test with selectattr 'match'] 
*******************************************************************************************************************
fatal: [XEv_Spine_31]: FAILED! => {
    "msg": "template error while templating string: unexpected ')'. String: 
{{ domain_definition | selectattr('name', 'match', '^server21$')).port }}"
}
...ignoring

TASK [yang : test with selectattr 'match'] 
*******************************************************************************************************************
fatal: [XEv_Spine_31]: FAILED! => {
    "msg": "template error while templating string: unexpected ')'. String: 
{{ domain_definition | selectattr('name', 'equalto', 'server21')).port }}"
}
...ignoring

What am I doing wrong?
Maybe there is an easier way to achieve that.

-- 
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/3faa234a-244c-497b-9e29-6b197bc092fe%40googlegroups.com.

Reply via email to