Saturday, March 26, 2016

Ansible:Do task if host belongs to specific operating system and version

Some times we require to run specific commands on specific operating systems. This is because some commands will work in one distribution and other will not work. Ansible is good at dealing with this type of situations with three variables
 1)ansible_distribution

 2)ansible_distribution_release

 3)ansible_distribution_version
The ansible_distribution specifies which type of OS you want to run tasks or skip tasks. As of this blog writing, Ansible support below OS.
RedHat, Fedora, CentOS, Scientific, SLC, Ascendos, CloudLinux, PSBM, OracleLinux, OVS, OEL, Amazon, XenServer, Ubuntu, Debian, SLES, SLED, OpenSuSE, SuSE, Gentoo, ArchLinux, Mandriva, Mandrake, Solaris, Nexenta, OmniOS, OpenIndiana, SmartOS.
The ansible_distribution_releasespecifies which release is it, for example if you take Ubuntu the release can be precise or vivid.
The ansible_distribution_versionspecifies which version of the release is it. For example Ubuntu precise have four versions like 12.04.1, 12.04.2, 12.04.3, 12.04.4. And when we come to Centos the version can be 6.0, 6.2, 6.4 etc.
So how can we say which command to run in which distribution, release and version?
We can specifiy when keyword as below.
Example1: Remove monit software when the operating system is Ubuntu
  -name: Remove monit software in Ubuntu

   apt: pkg=monit state=absent

   when: ansible_distribution == 'Ubuntu'
Example2: Remove monit software when the operating system is Ubuntu and release is 'precise' which is default to 12.04 version.
  -name: Remove monit software in Ubuntu

   apt: pkg=monit state=absent

   when: ansible_distribution == 'Ubuntu' and ansible_distribution_release == 'precise'
Example3: Remove monit software when the operating system is Ubuntu, release is 'precise' and version is 12.04.3
  -name: Remove monit software in Ubuntu

   apt: pkg=monit state=absent

   when: ansible_distribution == 'Ubuntu' and ansible_distribution_release == 'precise' and ansible_distribution_version == 12.04.3
Hope this helps some one struggling to install same package with different name on different flavors, versions and releases. 

No comments:

Post a Comment