Ansible’s yum-module and yum itself

Today I had a problem with some RPMs which are replacing each other. Assume you have a package A which is providing X and a package B which is providing X, too. Then assume you have an application package which requires until a certain version package A and higher versions then package B. On top of this there are also the dependencies not described very well in the specfile of the application package itself.

So what is ansible doing in such a case?

Well, when you have now a playbook which let you choose which version you want to install and you need to clean up package A or package B (depending which version you choose) the you do probably something like this:

- name: "install version 1.0"
  become: true
  yum:
    name="application-package-1.0"
    state="present"
  when: some-magic-variable-is-set

So now what is happening? Yum sees that the application-package needs package X and picks just the latest version of the package X (as A and B are providing X).

The same happens actually also with the state=”absent”.

- name: "Remove package A before installing application-package"
  become: true
  yum:
    name="A"
    state="absent"

Now when package B is installed and should not be removed yum is removing it in anyway. It sees that there is a package which provides X and as the package A also provides X, then the assumption is taken, that the user wants actually remove package A.

So my solution was to query the rpm-DB before calling the removal of the package:

  - name: "Check if package A is really installed"
    shell: rpm -qa | grep real-name-of-package-A
    changed_when: no
    ignore_errors: true
    register: A_IS_INSTALLED

- name: "Remove package A before installing application-package"
  become: true
  yum:
    name="A"
    state="absent"
  when: A_IS_INSTALLED.rc == 0 and some-magic-variable-is-set

Hopefully this was clear what I have written here, it’s already late and had to fight a lot today with ansible… :-D

Falling in love with Ansible

Ansible is Simple IT Automation

And this is true! The slogan is fitting exactly was I experienced! I don’t go into more details, feel free to read the documentation and how Ansible works in detail, but it is really cool!

I want to share here some experience in developing playbooks. Not right away but very soon in the future :-) The kids are requesting my attention ^^

 

Update:

First one problem I had recently is the installation of a package in a particular version. The first run of the playbook installed the right version, however the second run of the same playbook was calling a yum update which caused the package to be updated. So for this I’ve written a small task which can be easily included in any playbook.

 

# roles/commons/tasks/yum_install_exact_version.yml
# Author: Lukas Tischler
# Date: 2016-11-12
# Notes: We could also call yum for getting the version but actually 
#rpm -q does the same
---
- name: check if package {{package}} is already installed
  command: "rpm -q {{package}}-{{version}}"
  register: package_installed
  changed_when: no
  failed_when: package_installed.rc > 1
  ignore_errors: True
  
- name: uninstall package {{package}} if not fitting to the version submitted
  become: yes
  yum:
    name="{{package}}"
    state=absent
    disablerepo=*
    enablerepo="{{yum_repo}}"
    update_cache=yes
    disable_gpg_check=yes
  ignore_errors: True
  when: package_installed.rc == 1

- name: install the package {{package}}
  become: yes
  yum:
    name="{{package}}-{{version}}"
    state=latest
    disablerepo=*
    enablerepo="{{yum_repo}}"
    update_cache=yes
    disable_gpg_check=yes
  when: package_installed.rc == 1

And the call of this tasks looks as shown below:

- include: roles/common/tasks/yum_install_exact_version.yml
  vars:
    package: "<package>"
    version: "<version>"

I hope this is helpful for the one or the other – please leave a comment ;-)