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?
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 }}"
How about you use RPM to retrieve the version directly in stead of going trough various pipes:
rpm -q --qf "%{VERSION}" custom-rpm
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With