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

**URL:** <https://forum.ansible.com/t/how-to-create-a-variable-with-value-based-on-some-condition/17757>\
**Category:** Ansible Project\
**Created:** [March 16, 2015, 7:31pm UTC](https://forum.ansible.com/t/how-to-create-a-variable-with-value-based-on-some-condition/17757 "2015-03-16T19:31:09Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Long\_Vu](https://avatars.discourse-cdn.com/v4/letter/l/65b543/32.png) [@Long\_Vu](https://forum.ansible.com/u/Long_Vu)\
**Post date:** [March 16, 2015, 7:31pm UTC](https://forum.ansible.com/t/how-to-create-a-variable-with-value-based-on-some-condition/17757/1 "2015-03-16T19:31:09Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![vaida.dan](https://avatars.discourse-cdn.com/v4/letter/v/5daacb/32.png) [@vaida.dan](https://forum.ansible.com/u/vaida.dan)\
**Post date:** [March 25, 2015, 12:40pm UTC](https://forum.ansible.com/t/how-to-create-a-variable-with-value-based-on-some-condition/17757/2 "2015-03-25T12:40:46Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![Barry\_Flanagan](https://avatars.discourse-cdn.com/v4/letter/b/d2c977/32.png) [@Barry\_Flanagan](https://forum.ansible.com/u/Barry_Flanagan)\
**Post date:** [March 27, 2015, 2:01pm UTC](https://forum.ansible.com/t/how-to-create-a-variable-with-value-based-on-some-condition/17757/3 "2015-03-27T14:01:07Z")

</div>

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:
