# Find version with json\_query

**URL:** <https://forum.ansible.com/t/find-version-with-json-query/29381>\
**Category:** Ansible Project\
**Created:** [January 6, 2019, 7:55am UTC](https://forum.ansible.com/t/find-version-with-json-query/29381 "2019-01-06T07:55:17Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![mcoussi](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/mcoussi/32/8056_2.png) [@mcoussi](https://forum.ansible.com/u/mcoussi)\
**Post date:** [January 6, 2019, 7:55am UTC](https://forum.ansible.com/t/find-version-with-json-query/29381/1 "2019-01-06T07:55:17Z")

</div>

Hi,

I have to deploy an application wich compose of many files to install.

I created a dictionnary whith version and file who need to be install.

Here is the dictionnary

`  
dict\_app:  
version: RC1

- sql
- bin  
version: RC2
- sql
- bin

`

Here is the YAML ansible code. I try to use json\_query to find file of a version.  
`

- name: Test variable  
debug:  
msg: “{{ item }}”  
with\_items: ‘{{ dict\_app | json\_query(“[?version==`RC1`]”) }}’  
`

But when i execute the playbook i have this result

`TASK [TEST_version : Test variable] ******************************************************************************************* ok: [test1] => (item=) => { "changed": false, "item": "", "msg": "" }`

Thanks for your help,

Matt

---

<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:** [January 6, 2019, 9:09am UTC](https://forum.ansible.com/t/find-version-with-json-query/29381/2 "2019-01-06T09:09:45Z")

</div>

> Hi,
> 
> I have to deploy an application wich compose of many files to install.
> 
> I created a dictionnary whith version and file who need to be install.
> 
> Here is the dictionnary
> 
> dict\_app:  
> &nbsp;&nbsp;&nbsp;version: RC1  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;- sql  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;- bin  
> &nbsp;&nbsp;&nbsp;version: RC2  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;- sql  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;- bin

Run an  
&nbsp;&nbsp;- debug: var=dict\_app  
and you'll see that you don't get what you think.

You have two version so the second one will overwrite the fist one.  
And the version is a dict and a list and that will not work as you think.

Just make the variable like this:

dict\_app:  
&nbsp;&nbsp;RC1:  
&nbsp;&nbsp;&nbsp;&nbsp;- sql  
&nbsp;&nbsp;&nbsp;&nbsp;- bin  
&nbsp;&nbsp;RC2:  
&nbsp;&nbsp;&nbsp;&nbsp;- sql  
&nbsp;&nbsp;&nbsp;&nbsp;- bin

> Here is the YAML ansible code. I try to use json\_query to find file of a  
> version.  
> - name: Test variable  
> &nbsp;&nbsp;debug:  
> &nbsp;&nbsp;&nbsp;msg: "{{ item }}"  
> &nbsp;&nbsp;with\_items: '{{ dict\_app | json\_query("[?version==`RC1`]") }}'

Then you can do this

- name: Test variable  
&nbsp;&nbsp;debug:  
&nbsp;&nbsp;&nbsp;&nbsp;msg: "{{ item }}"  
&nbsp;&nbsp;with\_items: '{{ dict\_app.RC1 }}'

---

<div class="post-metadata">

**Author:** ![mcoussi](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/mcoussi/32/8056_2.png) [@mcoussi](https://forum.ansible.com/u/mcoussi)\
**Post date:** [January 6, 2019, 9:40am UTC](https://forum.ansible.com/t/find-version-with-json-query/29381/3 "2019-01-06T09:40:46Z")

</div>

Hi,

Thanks for your answer.

I undeerstand what you say.

I change my variable strcture like this

And then i get this :

deploy\_app:  
version: RC2

dict\_app:

- {version: RC1, livrables: bin}

- {version: RC1, livrables: sql}

- {version: RC2, livrables: com}

- name: Valorisation de la variable pour les livrables  
set\_fact:  
install\_livrable: “{{dict\_app|json\_query(‘[?version==`' + deploy_app.version + '`].[livrables]’)}}”

- name: Affichage des livrables à installer pour la versiion en cours  
debug:  
msg: “{{ item }}”  
with\_items: ‘{{ install\_livrable}}’

`  
ok: [test1] =\> (item=bin) =\> {  
“changed”: false,  
“item”: “bin”,  
“msg”: “bin”  
}  
ok: [test1] =\> (item=sql) =\> {  
“changed”: false,  
“item”: “sql”,  
“msg”: “sql”  
}

`

Thanks for your help,

Matt
