We have designed an ansible playbook for nodes on which Ubuntu is installed.
Now we have to play the same Playbook on new machines (node) for a different customer who uses Centos.
The problem is that for Centos, Ansible uses Yum module (which use yum package manager to install module). In our actual playbook, we are obviously using apt module.
What is recommended by Ansible to better manage this scenario?
- Is it better to rewrite a version of the playbook for Centos?
- Or is it better to keep the same playbook and use dynamically either "apt" on Ubuntu? or Yum on centos?
- Is there another more suitable solution?
2
Answers
You’d probably be better off using the
package
module, which will select an appropriate package manager appropriate to the target machine. Then you just need to maintain per-distribution lists of package names, but the tasks themselves can be identical.That is, you could write something like:
And adjacent to your playbook have, have the files
os_Ubuntu.yml
andos_CentOS.yml
, and in those files set therequired_packages
variable.A common approach is writing one playbook for multiple distros with a number of conditions inside the playbook or just separate distro-specific tasks into different files and include these files into the main role like this
Thus in files
setup-Ubuntu.yml
,setup-CentOS.yml
etc you will keep all the actions specific to a particular Linux. Please also see Geerlingguy’s roles like this one as a suitable example.In the case you want just install or remove a package without using more sophisticated abilities from apt or yum, you may use package module which is rather simple but if thats enough for you then you will get the same code for different Linux flavours.
Also, you need to take into account that in Ubuntu and CentOS many files (including configuration files and program binaries) are located in different places. So if you decide to work with the package module, you may deal with distro-specific things by using external files in a manner like this:
After this task, you can use variables defined in the mentioned files initialised with values specific to your platform like this