Run Ansible Tasks for a Specific OS Release Version

Red Hat Ansible Red Hat Ansible

I already know and have explained how easy it is to make an Ansible task run only on a specific Linux distro family (Debian/Ubuntu or RedHat/Centos), but recently needed to go even further: limit certain Ansible tasks to be run on specific RHEL releases.

How To Run Ansible Tasks for RedHat or Debian

First, let me remind you how to specify that a task should be run for a certain distro family and not the others:

 - name: Disable IPv6
 template:
 src: templates/disable-ipv6.conf.j2
 dest: /etc/sysctl.d/disable-ipv6.conf
 mode: 0660
 backup: yes
 notify:
 disable ipv6
 tags:
 fix
 sysctl
 noipv6
 when: ansible_os_family == 'RedHat'

 name: Debian | blacklist ipv6 in modprobe
 lineinfile: "dest=/etc/modprobe.d/blacklist.conf line='blacklist ipv6' create=yes"
 tags:
 fix
 sysctl
 noipv6
 when: ansible_os_family == 'Debian' 

In this example, the first task (Disable IPv6) will only deploy the sysctl config element on RedHat (CentOS, Fedora) systems.

The second task will only run on Debian/Ubuntu family of Operating Systems.

How To Run Ansible for Specific Release of RedHat

Now to the issue at hand: while IPv6 was possible to disable in RHEL 6 and RHEL 7, it seems the IPv6 module is built-in with RHEL 8/CentOS 8 distros – and so there’s no point in trying to disable it.

Here’s how I created a task that would only run on RedHat/CentOS releases older than RHEL 8/CentOS 8:

 - name: Disable IPv6
 template:
 src: templates/disable-ipv6.conf.j2
 dest: /etc/sysctl.d/disable-ipv6.conf
 mode: 0660
 backup: yes
 notify:
 disable ipv6
 tags:
 fix
 sysctl
 noipv6
 when: ansible_os_family == 'RedHat' and ansible_distribution_major_version|int <= 7

That’s it for today!

See Also




Keep Learning

Follow me on Facebook, Twitter or Telegram:
Recommended
I learn with Educative: Educative
IT Consultancy
I'm a principal consultant with Tech Stack Solutions. I help with cloud architectrure, AWS deployments and automated management of Unix/Linux infrastructure. Get in touch!

Recent Tweets