Is it possible to use a variable which will choose a module or another according to OS type in a playbook ?
My IT infrastructure is composed of machines CentOS, Redhat, Debian and other OS. I would like to reduce the size and complexity of my playbook, by adding a condition which allows Ansible to choose the package manager (yum or apt according to OS type).
name: Verify that phpmyadmin is installed for rpmlike OS
action: yum pkg= phpmyadmin state=installed
only_if: ‘$is_rpmlike’
name: Verify that phpmyadmin is installed for deblike OS
action: apt pkg= phpmyadmin state=installed
only_if: ‘$is_deblike’
I hope that you will understand my question without any reading troubles because I’m french.
Finaly, if I missed something in the documentation, please let me know.
Best regards.
...
vars_files:
# common variables on OS
- "vars/external_vars.yml"
# selection if OS is different. but instead of
$facter_operatingsystem use $ansible_distribution
- [ "vars/$facter_operatingsystem.yml", "vars/defaults.yml" ]
There's also a fact which returns the package manager.
That all being said, this is a sometimes a non-obviously complex thing
to do, because most likely configuration files and other things will
be different as well, service names, and so on.
It is often much easier to just have two task include files, for
instance, because they'll both be very short...
Apache is very different in layout between Ubuntu and Red Hat for example.
The other reason we do this is so we can expose all the "bells and
whistles" of the individual package manager, rather than resorting to
a least-common-denominator approach.
I took inspiration from conditionals_part1.yml creating three files called Debian.yml, CentOS.yml and RedHat.yml in which I declared a variable packager.
Now my playbook works well.
Thank you very much for your help!
Acyl.