# Get Field with equal condition from json generated ansible 2.5.2 uri module

**URL:** <https://forum.ansible.com/t/get-field-with-equal-condition-from-json-generated-ansible-2-5-2-uri-module/28844>\
**Category:** Ansible Project\
**Tags:** awx\
**Created:** [October 22, 2018, 2:58pm UTC](https://forum.ansible.com/t/get-field-with-equal-condition-from-json-generated-ansible-2-5-2-uri-module/28844 "2018-10-22T14:58:06Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![Alberto\_Jimenez\_Loza](https://avatars.discourse-cdn.com/v4/letter/a/97f17d/32.png) [@Alberto\_Jimenez\_Loza](https://forum.ansible.com/u/Alberto_Jimenez_Loza)\
**Post date:** [October 22, 2018, 2:58pm UTC](https://forum.ansible.com/t/get-field-with-equal-condition-from-json-generated-ansible-2-5-2-uri-module/28844/1 "2018-10-22T14:58:06Z")

</div>

I need get a field into a json what has been generated wit uri module.

This is my json that contains two hosts colections hc1 y hc2.  
I want to get the field id:693 of the host\_collection hc1.

```auto
            "created_at": "2018-10-18 13:29:01 UTC", 
            "description": null, 
            "id": 693, 
            "max_hosts": null, 
            "name": "hc1", 
            "organization_id": 1, 
            "permissions": {
                "deletable": true, 
                "editable": true
            }, 
            "total_hosts": 0, 
            "unlimited_hosts": true, 
            "updated_at": "2018-10-18 13:29:01 UTC"
        }, 
        {
            "created_at": "2018-10-18 13:29:04 UTC", 
            "description": null, 
            "id": 696, 
            "max_hosts": null, 
            "name": "hc2", 
            "organization_id": 1, 
            "permissions": {
                "deletable": true, 
                "editable": true
            }, 
            "total_hosts": 0, 
            "unlimited_hosts": true, 
            "updated_at": "2018-10-18 13:29:04 UTC"
        }, 

My playbook is this, but it fails.

 - name: GET consulta api satellite
    uri:
       url: "https://xxxxxx/katello/api/host_collections/"
       user: "{{user}}"
       password: "{{password}}"
       method: GET
       return_content: yes
       force_basic_auth: yes
       validate_certs: no
       headers:
               Content-Type: "application/json"
       status_code: [200,201,202,204,301,401]
    register: hc

  - name: GET ID d HC

    debug: 
       var="hc.json.results.[?name=='hc1'].id"

When I execute de playbook, it appears the next error:

TASK [GET ID del HC] *******************************************************************************************************************************************************************************
fatal: []: FAILED! => {}

MSG:

template error while templating string: unexpected char u'?' at 19. String: {{hc.json.results.[?name=='hc1'].id}}
        to retry, use: --limit @/awx/scripts/sat_uri/mb_get_host_collection.retry

If I put this option, go OK.

- name: GET ID del HC
    debug:
       var=hc.json.results[0].id
 

**TASK [GET ID del HC]*******************************************************************************************************************************************************************************
ok: [] => {
    "hc.json.results[0].id": "693"
}**

```

---

<div class="post-metadata">

**Author:** ![vbotka](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/vbotka/32/359_2.png) [@vbotka](https://forum.ansible.com/u/vbotka)\
**Post date:** [October 22, 2018, 4:02pm UTC](https://forum.ansible.com/t/get-field-with-equal-condition-from-json-generated-ansible-2-5-2-uri-module/28844/2 "2018-10-22T16:02:13Z")

</div>

> `Try`

- debug: var=hc.json.results|json\_query(‘[?name==`hc1`].id’)

---

<div class="post-metadata">

**Author:** ![Alberto\_Jimenez\_Loza](https://avatars.discourse-cdn.com/v4/letter/a/97f17d/32.png) [@Alberto\_Jimenez\_Loza](https://forum.ansible.com/u/Alberto_Jimenez_Loza)\
**Post date:** [October 23, 2018, 6:48am UTC](https://forum.ansible.com/t/get-field-with-equal-condition-from-json-generated-ansible-2-5-2-uri-module/28844/3 "2018-10-23T06:48:24Z")

</div>

It works OK, thanks.

- name: LIST id HC  
debug:  
var=hc.json.results|json\_query(‘[?name==`{{host_collection}}`].id’)

TASK [LIST id HC] \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*  
ok: =\> {  
“hc.json.results|json\_query(‘[?name==`hc1`].id’)”: [  
693  
]  
}

But I have another problem.  
I want to use that variable for another uri execution, but i don´t know how to pass the value to a var.

I tried with this, but it fails:

- set\_fact:  
**hc\_id: “{{hc.json.results|json\_query(‘[?name==`{{host_collection}}`].id’)}}”**

- name: LIST id HC  
debug:  
var=hc\_id

- name: GET INFO NODES HOST COLLECTION  
uri:  
url: “[https://xxxxxxx/katello/api/host\_collections/{{hc\_id}](https://xxxxxxx/katello/api/host_collections/%7B%7Bhc_id%7D)}/”  
user: “{{user}}”  
password: “{{password}}”  
method: GET  
return\_content: yes  
force\_basic\_auth: yes  
validate\_certs: no  
headers:  
Content-Type: “application/json”  
status\_code: [200,201,202,204,301,401]  
register: hc\_info

- name: LIST id HC  
debug:

hc\_id variable is empty:

TASK [set\_fact] \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*  
ok: [ssh.sva.itbatera.ejgv.eus]

TASK [LIST id HC] \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*  
ok: [ssh.sva.itbatera.ejgv.eus] =\> {  
“hc\_id”:   
}

---

<div class="post-metadata">

**Author:** ![Darkhan](https://avatars.discourse-cdn.com/v4/letter/d/34f0e0/32.png) [@Darkhan](https://forum.ansible.com/u/Darkhan)\
**Post date:** [October 23, 2018, 8:03am UTC](https://forum.ansible.com/t/get-field-with-equal-condition-from-json-generated-ansible-2-5-2-uri-module/28844/4 "2018-10-23T08:03:35Z")

</div>

> `Define it under a vars section.`

[https://docs.ansible.com/ansible/devel/user\_guide/playbooks\_variables.html#variables-defined-in-a-playbook](https://docs.ansible.com/ansible/devel/user_guide/playbooks_variables.html#variables-defined-in-a-playbook)

---

<div class="post-metadata">

**Author:** ![Alberto\_Jimenez\_Loza](https://avatars.discourse-cdn.com/v4/letter/a/97f17d/32.png) [@Alberto\_Jimenez\_Loza](https://forum.ansible.com/u/Alberto_Jimenez_Loza)\
**Post date:** [October 23, 2018, 8:33am UTC](https://forum.ansible.com/t/get-field-with-equal-condition-from-json-generated-ansible-2-5-2-uri-module/28844/5 "2018-10-23T08:33:55Z")

</div>

I tried with

vars:  
hc\_id: “{{hc.json.results|json\_query(‘[?name==`{{host_collection}}`].id’)}}”

And with

vars:  
hc\_id: “{{hc.json.results|json\_query(‘[?name==`\"{{host_collection}}\"`].id’)}}”

But the variable is empty always.

TASK [LIST id HC] \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*  
ok: =\> {  
“hc\_id”:   
}

The problem is that I have a variable {{host\_collection}} into another variable definition and I have to scaped the {} symbols, no?

---

<div class="post-metadata">

**Author:** ![Kai\_Stian\_Olstad](https://avatars.discourse-cdn.com/v4/letter/k/e19adc/32.png) [@Kai\_Stian\_Olstad](https://forum.ansible.com/u/Kai_Stian_Olstad)\
**Post date:** [October 23, 2018, 2:02pm UTC](https://forum.ansible.com/t/get-field-with-equal-condition-from-json-generated-ansible-2-5-2-uri-module/28844/6 "2018-10-23T14:02:32Z")

</div>

You can't use {{ }} inside {{ }}, you'll need to use Jinja concatenation  
I'm not 100% sure if this is correct but you can try:

"{{ hc.json.results | json\_query('[?name==`' ~ host_collection ~ '`].id') }}"

---

<div class="post-metadata">

**Author:** ![Alberto\_Jimenez\_Loza](https://avatars.discourse-cdn.com/v4/letter/a/97f17d/32.png) [@Alberto\_Jimenez\_Loza](https://forum.ansible.com/u/Alberto_Jimenez_Loza)\
**Post date:** [October 23, 2018, 2:30pm UTC](https://forum.ansible.com/t/get-field-with-equal-condition-from-json-generated-ansible-2-5-2-uri-module/28844/7 "2018-10-23T14:30:08Z")

</div>

It works ok.

Thank You.

hc\_id: “{{ hc.json.results | json\_query(‘[?name==`' ~ host_collection ~ '`].id’) }}”

TASK [GET id HC hc2] \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*  
ok: [xxxx] =\> {  
“hc\_id”: [  
586  
]  
}

TASK [LIST INFO hc2 with UID /586] \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*  
ok: [ssh.sva.itbatera.ejgv.eus] =\> {  
“hc\_info.json”: {  
“created\_at”: “2018-10-18 10:47:52 UTC”,  
“description”: null,  
“host\_ids”: [  
1280  
],  
“id”: 586,  
“max\_hosts”: null,  
“name”: “hc2”,  
“organization\_id”: 1,  
“permissions”: {  
“deletable”: true,  
“editable”: true  
},  
“total\_hosts”: 1,  
“unlimited\_hosts”: true,  
“updated\_at”: “2018-10-18 10:47:52 UTC”  
}  
}
