Search code examples
ansiblejson-query

Filtering through unicode lists in Ansible


What I'm trying to do is determine if a specific version of a specific package is installed to use in a when clause later in my playbook. I've gotten far enough to get what effectively is a true/false for the package being installed, but haven't been able to put the pieces together for the version check.

Here is what I've got so far:

- name: Yum list of installed rpms
  yum:
    list: installed
  register: yum_installed

- name: Get tar version info
  set_fact:
    yum_test:  "{{yum_installed|json_query(jsonquery) }}"
  vars:
    jsonquery: "results[?name=='tar']"

-  debug:
    msg: "Success"
  when: yum_test|json_query([?version=='1.62'])

This the error that I end up getting:

The conditional check 'yum_test|json_query([?version=='1.62'])' failed.
The error was: template error while templating string: unexpected char u'?' at 26. 
String: {% if yum_test|json_query([?version=='1.62']) %} True {% else %} False {% endif %}

If I take a look at the yum_test fact I created, it looks like this:

[{
  u'envra': u'2:tar-1.26-31.el7.x86_64', 
  u'name': u'tar', 
  u'repo': u'installed', 
  u'epoch': u'2', 
  u'version': u'1.26', 
  u'release': u'31.el7', 
  u'yumstate': u'installed', 
  u'arch': u'x86_64'
}]

It seems to me that my issue is the list is being saved as a unicode object... but I'm not sure how to clean it up. I've tried using to_yaml and to_json with no luck. Can anyone point me in the right direction, or has a better way to do what I'm attempting?


Solution

  • Following additions fix my issue:

    "{{ yum_test |json_query(\"[?version=='\" + tar_version + \"']\") }}"