[CONTACT]

[ABOUT]

[POLICY]

This is a only version

Found at: raymii.org:70/Ansible_-_Only_if_on_specific_distribution_or_distribution_version.txt

This is a text-only version of the following page on https://raymii.org:
---
Title       : 	Ansible - Only do action if on specific distribution (Debian, Ubuntu, CentOS or RHEL) or distribution version (ubuntu precise, ubuntu trusty)
Author      : 	Remy van Elst
Date        : 	09-11-2014
Last update : 	16-12-2018
URL         : 	https://raymii.org/s/tutorials/Ansible_-_Only_if_on_specific_distribution_or_distribution_version.html
Format      : 	Markdown/HTML
---



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

I'm developing an open source monitoring app called Leaf Node Monitoring, for windows, linux & android. Go check it out!

Consider sponsoring me on Github. It means the world to me if you show your appreciation and you'll help pay the server costs.

You can also sponsor me by getting a Digital Ocean VPS. With this referral link you'll get $100 credit for 60 days.



  * 2018-12-16: update ansible syntax to version 2.5, use become
  * 2015-09-24: Added `package` module, changed only_if to when
  * 2014-09-11: Initial release

#### Specific Distribution

On a specific action, add the following `when` statement:

    
    
    when: ansible_distribution == 'CentOS' or ansible_distribution == 'Red Hat
    Enterprise 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. This
example used apache because the name package name is different on the two
distributions.

    
    
    ---
    - 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 Lucid
you 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 from
`ansible 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 your
ansible 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 it
features the `package` module. This is a generic module that installs, upgrade
and removes packages using the underlying OS package manager. This module
actually 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 on
Debian 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][2].

   [1]: https://www.digitalocean.com/?refcode=7435ae6b8212
   [2]: https://docs.ansible.com/ansible/package_module.html

---

License:
All the text on this website is free as in freedom unless stated otherwise. 
This means you can use it in any way you want, you can copy it, change it 
the way you like and republish it, as long as you release the (modified) 
content under the same license to give others the same freedoms you've got 
and place my name and a link to this site with the article as source.

This site uses Google Analytics for statistics and Google Adwords for 
advertisements. You are tracked and Google knows everything about you. 
Use an adblocker like ublock-origin if you don't want it.

All the code on this website is licensed under the GNU GPL v3 license 
unless already licensed under a license which does not allows this form 
of licensing or if another license is stated on that page / in that software:

    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see .

Just to be clear, the information on this website is for meant for educational 
purposes and you use it at your own risk. I do not take responsibility if you 
screw something up. Use common sense, do not 'rm -rf /' as root for example. 
If you have any questions then do not hesitate to contact me.

See https://raymii.org/s/static/About.html for details.


AD: