# Create a variable based on a variable

**URL:** <https://forum.ansible.com/t/create-a-variable-based-on-a-variable/38772>\
**Category:** Ansible Project\
**Tags:** vmware\
**Created:** [May 8, 2024, 4:34pm UTC](https://forum.ansible.com/t/create-a-variable-based-on-a-variable/38772 "2024-05-08T16:34:36Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![dyioulos](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/dyioulos/32/8363_2.png) [@dyioulos](https://forum.ansible.com/u/dyioulos)\
**Post date:** [May 8, 2024, 4:34pm UTC](https://forum.ansible.com/t/create-a-variable-based-on-a-variable/38772/1 "2024-05-08T16:34:36Z")

</div>

Hello, all.

The subject of this post is probably terribly named. That said, here’s what I’m trying to accomplish:

I have two VMware vCenter hosts - [vcenter1.mycompany.com](http://vcenter1.mycompany.com) and [vcenter2.mycompany.com](http://vcenter2.mycompany.com). Within each host is are two datacenters - datacenter1 and datacenter2. for each host, the datacenter alignments are vcenter1 – datacenter1 and vcenter2 – datacenter2.

I have a number of playbooks that do actions against vCenter. For example, I have one deletes all snapshots. It looks like this:

---

<div class="post-metadata">

**Author:** ![Walter\_Rowe](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/walter_rowe/32/4506_2.png) [@Walter\_Rowe](https://forum.ansible.com/u/Walter_Rowe)\
**Post date:** [May 8, 2024, 5:26pm UTC](https://forum.ansible.com/t/create-a-variable-based-on-a-variable/38772/2 "2024-05-08T17:26:54Z")

</div>

A dictionary would solve this using the guest name as the key.

vcenter:  
host1: { datacenter: datacenter1, vcenter: vcenter2 }

host2: { datacenter: datacenter1, vcenter: vcenter1 }  
host3: { datacenter: datacenter2, vcenter: vcenter2 }  
host4: { datacenter: datacenter2, vcenter: vcenter1 }

{{ vcenter[inventory\_hostname].datacenter }}  
{{ vcenter[inventory\_hostname].vcenter }}

Keeping that dictionary update to date will be tedious but could be automated too. Query each vcenter / datacenter pair for its list of VMs and generate a vars file with the above dictionary. Include that vars file in your playbooks. Alternatively (and more expensive) is to create an initial task that queries the each vcenter / datacenter pair and creates this dictionary in real time.

Walter

---

<div class="post-metadata">

**Author:** ![astrakid](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/astrakid/32/1789_2.png) [@astrakid](https://forum.ansible.com/u/astrakid)\
**Post date:** [May 8, 2024, 5:52pm UTC](https://forum.ansible.com/t/create-a-variable-based-on-a-variable/38772/3 "2024-05-08T17:52:26Z")

</div>

i am quite a newbee with ansible, but i would suggest to  
a) use host-variables with e.g. “datacenter” set to the matching datacenter value or  
b) use a regex on the host variable to create the datacenter on it

regards,  
andre

---

<div class="post-metadata">

**Author:** ![John1](https://avatars.discourse-cdn.com/v4/letter/j/e9bcb4/32.png) [@John1](https://forum.ansible.com/u/John1)\
**Post date:** [May 8, 2024, 10:49pm UTC](https://forum.ansible.com/t/create-a-variable-based-on-a-variable/38772/4 "2024-05-08T22:49:38Z")

</div>

Just off the top of my head you can use set\_fact.

- name: Set datacenter variable based on hostname  
set\_fact:  
datacenter: “datacenter{{ ansible\_hostname[-1] }}”  
when: ansible\_hostname | regex\_search(‘\[1\]\*[1-2]’)

You can then just use {{ datacenter }} and it should populate correctly. I haven’t tested the above, but if it doesn’t work it should at least put you in the ball park.

* * *

1. a-zA-Z0-9

---

<div class="post-metadata">

**Author:** ![dyioulos](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/dyioulos/32/8363_2.png) [@dyioulos](https://forum.ansible.com/u/dyioulos)\
**Post date:** [May 17, 2024, 7:03pm UTC](https://forum.ansible.com/t/create-a-variable-based-on-a-variable/38772/5 "2024-05-17T19:03:01Z")

</div>

Than you all. I’ll make note of your suggestions. I solved my issue like this (whether it’s "optimal, I don;t know):

**vars:**  
**vcenter\_datacenter: \>-**  
**{% if vcenter\_hostname == ‘vcenter1’ %}**  
**Datacenter1**  **Datacenter**  
**{% elif vcenter\_hostname == ‘vcenter2’ %}**  
**Datacenter2**  **Datacenter**  
**{% else %}**  
**Default Datacenter**  
**{% endif %}**

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/ansible/original/1X/1882d9a01da5d8996a862359b0913cc568a39b84.svg) [@system](https://forum.ansible.com/u/system)\
**Post date:** [May 17, 2024, 9:01pm UTC](https://forum.ansible.com/t/create-a-variable-based-on-a-variable/38772/6 "2024-05-17T21:01:45Z")

</div>

Also:

vars:  
&nbsp;&nbsp;vcenter\_datacenter: '{{ vcenter\_hostname ==  
"vcenter1"|ternary("First", vcenter\_hostname ==  
"vcenter2"|ternary("datacenter1", "datacenter2")) }}' Dataceenter

but i would do this:

vars:  
&nbsp;&nbsp;&nbsp;DCS:  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;vcenter1: First  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;vcenter2: datacenter1  
&nbsp;&nbsp;vcenter\_datacenter: '{{ DCS[vcenter\_hostname]|default("datacenter2")  
}}' Datacenter

---

<div class="post-metadata">

**Author:** ![abhijeetJanwalkar](https://avatars.discourse-cdn.com/v4/letter/a/9e8a1a/32.png) [@abhijeetJanwalkar](https://forum.ansible.com/u/abhijeetJanwalkar)\
**Post date:** [May 23, 2024, 11:19am UTC](https://forum.ansible.com/t/create-a-variable-based-on-a-variable/38772/7 "2024-05-23T11:19:33Z")

</div>

Not exactly based on the variables, but I use this to find the datacenter in the vCenter (in my case I have only one datacenter per vCenter so it works)

- name: Gather information about all datacenters in vCenter  
community.vmware.vmware\_datacenter\_info:  
hostname: “{{ hostname }}”  
username: “{{ username }}”  
password: “{{ password }}”  
validate\_certs: “{{ false}}”  
delegate\_to: localhost  
register: datacenterInfo

#- name: Display datacenter Info  
#ansible.builtin.debug:  
#msg: “{{ datacenterInfo.datacenter\_info[0].name }}”

- name: set Fact - datacenter  
set\_fact:  
datacenter: “{{ datacenterInfo.datacenter\_info[0].name }}”

Hope this helps.

Warm Regards,  
Abhi
