Best way to check for installed yum package/rpm version in Ansible and use it

I just want to update this old discussion to point out that there is now a package module that makes this more straightforward

- name: get the rpm or apt package facts
  package_facts:
    manager: "auto"

- name: show apache2 version
  debug: var=ansible_facts.packages.apache2[0].version

I think more native ansible way would be:

- name: get package version
  yum:
    list: package_name
  register: package_name_version

- name: set package version
  set_fact:
    package_name_version: "{{ package_name_version.results|selectattr('yumstate','equalto','installed')|map(attribute='version')|list|first }}"

It's okay to suppress this warning in your case. Use args like:

---
- name: Get version of RPM
  shell: yum list installed custom-rpm | grep custom-rpm | awk '{print $2}' | cut -d'-' -f1
  register: version
  changed_when: False
  args:
    warn: no

Equivalent to warn=no on the shell: line but tidier.

Tags:

Ansible