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

Leave a Reply