Find version with json_query

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

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

Run an
  - 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:
  RC1:
    - sql
    - bin
  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`]") }}'

Then you can do this

- name: Test variable
  debug:
    msg: "{{ item }}"
  with_items: '{{ dict_app.RC1 }}'

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