Using json query filter

1,298 views
Skip to first unread message

trg.ac...@gmail.com

unread,
Jun 30, 2017, 6:47:43 AM6/30/17
to Ansible Project
Hello,

I'm trying to determined if package is installed and what version
I have the following tasks

vars:
    cmk_version: 1.4.0p7

    - name: Check if check-mk-raw package is installed
      yum:
        list: installed
      register: yum_packages

    - name: Display cmk_installed_version
      debug:
        var: item
      with_items: "{{yum_packages|json_query(jsonquery)}}"
      vars:
        jsonquery: "results[?name=='check-mk-raw-{{cmk_version}}'].name"


how I can use jsonquery var that will grab anything that starts with check-mk-raw so I can test if package name containing check-mk-raw is installed.
Also what would be the best way to just extract the version/release only. if I use var  jsonquery: "results[?name=='check-mk-raw-{{cmk_version}}'].name" the result would be check-mk-raw-1.4.0p7. I will need to extract "1.4.0p7" value only

J Hawkesworth

unread,
Jun 30, 2017, 7:24:11 AM6/30/17
to Ansible Project
I can recommend using the http://jmespath.org/tutorial.html to figure out what your jsonquery needs to be.  You can paste your json into any of the example boxes and then tinker with the jsonquery and see the results directly on the webpage.

Hope this helps a bit

Jon
Message has been deleted

Daniel JD

unread,
Jun 30, 2017, 4:19:58 PM6/30/17
to Ansible Project
Why not something like:

- name: Check if check_mk is installed
  shell: 'yum list installed check-mk-raw-*'
  register: cmk_status

- name: Check result
  debug:
    msg: "{{ cmk_status| ternary( cmk_status.stdout.....[extract version here], "Not installed") }}"
Reply all
Reply to author
Forward
0 new messages