Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

ansible

I've been getting my feet wet with Ansible (2.0.0.2) on CentOS 7. I'm trying to obtain a version from an installed rpm/yum package, but ran into a warning message when running the script.

Ansible script:

---
- 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

- name: Update some file with version
  lineinfile:
    dest: /opt/version.xml
    regexp: "<version>"
    line: "  <version>{{ version.stdout }}</version>"

Running this works fine and does what it's supposed to, but it's returning a warning after it executes:

ok: [default] => {"changed": false, "cmd": "yum list installed custom-rpm | grep custom-rpm | awk '{print $2}' | cut -d'-' -f1", "delta": "0:00:00.255406", "end": "2016-05-17 23:11:54.998838", "rc": 0, "start": "2016-05-17 23:11:54.743432", "stderr": "", "stdout": "3.10.2", "stdout_lines": ["3.10.2"], "warnings": ["Consider using yum module rather than running yum"]}

[WARNING]: Consider using yum module rather than running yum

I looked up information for the yum module on the Ansible site, but I don't really want to install/update/delete anything.

I could simply ignore it or suppress it, but I was curious if there was a better way?

like image 701
Derek Elder Avatar asked May 17 '16 23:05

Derek Elder


3 Answers

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
like image 96
fred Avatar answered Sep 22 '22 00:09

fred


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 }}"
like image 28
Sergey GRIZZLY Avatar answered Sep 20 '22 00:09

Sergey GRIZZLY


How about you use RPM to retrieve the version directly in stead of going trough various pipes:

rpm -q --qf "%{VERSION}" custom-rpm
like image 28
Johan Godfried Avatar answered Sep 22 '22 00:09

Johan Godfried