Skip to main content Logo (IEC resistor symbol)logo

Quis custodiet ipsos custodes?
Home | About | All pages | RSS Feed | Gopher

Ansible - Only do action if on specific distribution (Debian, Ubuntu, CentOS or RHEL) or distribution version (ubuntu precise, ubuntu trusty)

Published: 09-11-2014 | Last update: 16-12-2018 | Author: Remy van Elst | Text only version of this article

Table of Contents

This Ansible playbook example helps you execute actions only if you are on acertain distribution. You might have a mixed environment with CentOS and Debianand when using Ansible to execute actions on nodes you don't need to run Yum onDebian, or Apt on CentOS. Some package names are different and such, so thishelps you with an only if statement to select a specific distribution. As abonus, you also get an only_if for specific distribution versions, like Ubuntuprecise (12.04 LTS) or Ubuntu Trusty (14.04 LTS).

If you like this article, consider sponsoring me by trying out a Digital OceanVPS. With this link you'll get $100 credit for 60 days). (referral link)

Specific Distribution

On a specific action, add the following when statement:

when: ansible_distribution == 'CentOS' or ansible_distribution == 'Red HatEnterprise Linux'

This is for RHEL and Centos, the following is for Debian/Ubuntu:

when: ansible_distribution == 'Debian' or ansible_distribution == 'Ubuntu'

This example playbook installs Apache2 on both Debian/Ubuntu and CentOS. Thisexample used apache because the name package name is different on the twodistributions.

---- hosts: example  become: true  user: remy  connection: ssh   tasks:  - name: Install apache    apt:       name: {{ item }}       state: latest    with_items:     - apache2    when: ansible_distribution == 'Debian' or ansible_distribution == 'Ubuntu'  - name: Install httpd    yum:       name: {{ item }}       state: latest    with_items:     - httpd    when: ansible_distribution == 'CentOS' or ansible_distribution == 'Red Hat Enterprise Linux'  - name: restart apache    service:       name: apache2       state: started       enabled: yes    when: ansible_distribution == 'Debian' or ansible_distribution == 'Ubuntu'  - name: restart httpd    service:       name: httpd       state: started       enabled: yes    when: ansible_distribution == 'CentOS' or ansible_distribution == 'Red Hat Enterprise Linux'

Specific Distribution Version

You might also need to do different actions based on distribution version,because some things are available on CentOS 6 but not on 5, or on Ubuntu Lucidyou need to install some backported packages and not on Ubuntu Precise.

For those situations, you can use either the {{ ansible_distribution_version }or {{ ansible_distribution_release }} variable. See some example output fromansible all -m setup -a "filter=ansible_distribution*":

    "ansible_distribution": "CentOS",    "ansible_distribution_release": "Final",    "ansible_distribution_version": "5.9"    "ansible_distribution": "CentOS",    "ansible_distribution_release": "Final",    "ansible_distribution_version": "6.4"    "ansible_distribution": "Ubuntu",    "ansible_distribution_release": "lucid",    "ansible_distribution_version": "10.04"    "ansible_distribution": "Ubuntu",    "ansible_distribution_release": "precise",    "ansible_distribution_version": "12.04"    "ansible_distribution": "Debian",    "ansible_distribution_release": "wheezy",    "ansible_distribution_version": "7"

Using these, you can filter the output by changing the when statement in youransible playbook:

when: ansible_distribution == 'CentOS' and ansible_distribution_version == '6.4'when: ansible_distribution == 'Ubuntu' and ansible_distribution_release == 'precise'when: ansible_distribution == 'Debian' and ansible_distribution_version == '7'when: ansible_os_family == "RedHat" and ansible_lsb.major_release|int >= 5

Package module (2015 short update)

As my former colleague Stein pointed me to, Ansible 2.0 has been released and itfeatures the package module. This is a generic module that installs, upgradeand removes packages using the underlying OS package manager. This moduleactually calls the pertinent package modules for each system (apt, yum, etc).

This means that if you use this article because you want a package install onDebian and CentOS, you can now just do the following:

- name: install (or upgrade to) the latest version of htop  package:     name: htop     state: latest

If a package has different names on different distributions, like Apache(apache2 on ubuntu, httpd on CentOS) you still need to use a when statement.

Read more about the package module on the ansible Docs website.

Tags: ansible, apt, configuration-management, deb, deployment, devops, ntp, packages, python, tutorials, yum