How to create a variable with value based on some condition?

Hi,

In Centos 7, mysql is now “mariadb”, in Centos 6 or below it is “mysq”.

Looking at “ansible_distribution_major_version” fact I will be able to find if my target host is Centos 6 or 7.

How would I create a variable {{db_name}} such that I can use it like this:

yum: pkg={{db_name}} state=installed
yum: pkg={{db_name}}-server state=installed

That variable should also be sharable with other roles as well.

Thanks,
Long

This email or any attachments may contain confidential or legally privileged information intended for the sole use of the addressees. Any use, redistribution, disclosure, or reproduction of this information, except as intended, is prohibited. If you received this email in error, please notify the sender and remove all copies of the message, including any attachments.

There are some ways to go around this.

One would be to have two tasks which would use the when condition to find the distro version and install the relevant package.

If this is too much overhead, depending on your roles, you could use two tasks with set_fact and work your way up from there in the other roles.

I do this by having the below at the top of my tasks/main.yml

Loading vars

  • name: Add the OS specific varibles
    include_vars: “{{ ansible_os_family }}.yml”

Then I have a RedHat.yml: