1

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?

1
  • That's not really an answerable question. People wrote their code that way because it met their needs. There are other ways to write it that might also meet their needs, but once they have something that works for them there's no reason for them to go looking for alternative solutions. If ansible_facts.os_family meets your needs, use it. If it doesn't, don't. Nov 7 at 20:31
0

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.