HiveBrain v1.2.0
Get Started
← Back to all entries
patternMinor

Testing for EL with Ansible facts; distribution or os_family?

Submitted by: @import:stackexchange-devops··
0
Viewed 0 times
withos_familytestingfordistributionansiblefacts

Problem

Sometimes in an Ansible playbook, you'll want to condition running a task on whether or not the distribution is CentOS, RHEL, etc, or if it's something else. A very common example is when a package is named one thing on CentOS and something else on Debian.

I see a lot of Ansible playbooks (and have written a few myself) that will handle this by adding a "when:" to the task like this:

when: ansible_distribution == 'CentOS' or ansible_distribution == 'RedHat'

Is there a reason that that's so common instead of checking for os_family? Why do I not see this more often?:

when: ansible_os_family == 'RedHat'

Besides being more direct and concise, that also allows for other EL Linuxes like Scientific Linux and Rocky Linux without having to continually add to a list of all the possible EL derivatives. Is there something I'm missing?

Solution

Maybe try using lsb_release to derive the information, setting it as a fact, then look up the family from an Ansible hash map populated with the associations. Here is a mention of ansible_facts['os_family'] at https://docs.ansible.com/ansible/latest/user_guide/playbooks_conditionals.html#ansible-facts-os-family

Context

StackExchange DevOps Q#14803, answer score: 2

Revisions (0)

No revisions yet.