# Variable depends on other variable

**URL:** <https://forum.ansible.com/t/variable-depends-on-other-variable/13555>\
**Category:** Ansible Project\
**Created:** [September 25, 2013, 10:23pm UTC](https://forum.ansible.com/t/variable-depends-on-other-variable/13555 "2013-09-25T22:23:36Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Edgars](https://avatars.discourse-cdn.com/v4/letter/e/f17d59/32.png) [@Edgars](https://forum.ansible.com/u/Edgars)\
**Post date:** [September 25, 2013, 10:23pm UTC](https://forum.ansible.com/t/variable-depends-on-other-variable/13555/1 "2013-09-25T22:23:36Z")

</div>

Hi

I am wondering is it possible to set variable’s value which depends on other variable’s value? Something like this:

dbname = prod:  
if dbid == 1

dbname = dev:  
if dbid == 2

Or maybe there is another way how to achieve this?

Thanks,  
Edgars

---

<div class="post-metadata">

**Author:** ![Michael\_DeHaan2](https://avatars.discourse-cdn.com/v4/letter/m/da6949/32.png) [@Michael\_DeHaan2](https://forum.ansible.com/u/Michael_DeHaan2)\
**Post date:** [September 25, 2013, 11:55pm UTC](https://forum.ansible.com/t/variable-depends-on-other-variable/13555/2 "2013-09-25T23:55:16Z")

</div>

Group variables are a great way to do this, and you can assign variables for both your prod and development groups that your machines are in.

See “group\_vars/” in the docs for a nice place to put this info.

Technically you can use “set\_fact” but needing it is something of a “playbook complexity smell” in most cases.

---

<div class="post-metadata">

**Author:** ![Edgars](https://avatars.discourse-cdn.com/v4/letter/e/f17d59/32.png) [@Edgars](https://forum.ansible.com/u/Edgars)\
**Post date:** [October 4, 2013, 12:30pm UTC](https://forum.ansible.com/t/variable-depends-on-other-variable/13555/3 "2013-10-04T12:30:37Z")

</div>

Could you give me example with set\_facts? I am trying to set fact, but I am not sure how can I use that fact later in playbook

Edgars

ceturtdiena, 2013. gada 26. septembris 00:23:36 UTC+2, Edgars rakstīja:

---

<div class="post-metadata">

**Author:** ![Michael\_DeHaan2](https://avatars.discourse-cdn.com/v4/letter/m/da6949/32.png) [@Michael\_DeHaan2](https://forum.ansible.com/u/Michael_DeHaan2)\
**Post date:** [October 4, 2013, 1:18pm UTC](https://forum.ansible.com/t/variable-depends-on-other-variable/13555/4 "2013-10-04T13:18:06Z")

</div>

You really should use group variables instead.

---

<div class="post-metadata">

**Author:** ![Edgars](https://avatars.discourse-cdn.com/v4/letter/e/f17d59/32.png) [@Edgars](https://forum.ansible.com/u/Edgars)\
**Post date:** [October 4, 2013, 2:38pm UTC](https://forum.ansible.com/t/variable-depends-on-other-variable/13555/5 "2013-10-04T14:38:41Z")

</div>

I have no groups here. I am running playbook and all tasks on localhost with connection=local. I have DB server with multiple DB instances. Depending on DB instance ID, I want to set config options.

Edgars

ceturtdiena, 2013. gada 26. septembris 00:23:36 UTC+2, Edgars rakstīja:

---

<div class="post-metadata">

**Author:** ![Michael\_DeHaan2](https://avatars.discourse-cdn.com/v4/letter/m/da6949/32.png) [@Michael\_DeHaan2](https://forum.ansible.com/u/Michael_DeHaan2)\
**Post date:** [October 4, 2013, 11:34pm UTC](https://forum.ansible.com/t/variable-depends-on-other-variable/13555/6 "2013-10-04T23:34:11Z")

</div>

You could create some groups 🙂
