Adapting the roles
So, how do we build the logic into our roles to execute only certain parts of them on different operating systems? As we know, the package names will be different. How do we define different sets of variables per operating system?
Operating system family
We have looked at the ansible.builtin.setup
module in Chapter 1, Installing and Running Ansible; this module gathers facts about our target hosts.
One of these facts is ansible_os_family
; this tells us the type of operating system we are running.
To demonstrate this, I have launched two hosts, one running Ubuntu 22.04
, and the second running Rocky Linux 9
as its operating system. I have created an inventory file which looks like the following:
RedHat ansible_host=178.79.178.78.nip.io Debian ansible_host=176.58.114.60.nip.io [ansible_hosts] RedHat Debian [ansible_hosts:vars] ansible_connection=ssh ansible_user=root ansible_private_key_file=~/.ssh/id_rsa host_key_checking=False
Important note
The...