# How to assign inventory group alias to a variable?

**URL:** <https://forum.ansible.com/t/how-to-assign-inventory-group-alias-to-a-variable/29461>\
**Category:** Ansible Project\
**Created:** [January 17, 2019, 8:25pm UTC](https://forum.ansible.com/t/how-to-assign-inventory-group-alias-to-a-variable/29461 "2019-01-17T20:25:06Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Robert\_F](https://avatars.discourse-cdn.com/v4/letter/r/9d8465/32.png) [@Robert\_F](https://forum.ansible.com/u/Robert_F)\
**Post date:** [January 17, 2019, 8:25pm UTC](https://forum.ansible.com/t/how-to-assign-inventory-group-alias-to-a-variable/29461/1 "2019-01-17T20:25:06Z")

</div>

I have this Ansible inventory file:

[fileservers]  
[fs01.example.com](http://fs01.example.com) ansible\_ssh\_host=192.168.1.1 ip\_addr=192.168.1.1

[dbservers]  
[db01.example.com](http://db01.example.com) ansible\_ssh\_host=192.168.1.2 ip\_addr=192.168.1.2

[webservers]  
[web01.example.com](http://web01.example.com) ansible\_ssh\_host=192.168.1.3 ip\_addr=192.168.1.3

[cmsservers]  
[cms01.example.com](http://cms01.example.com) ansible\_ssh\_host=192.168.1.4 ip\_addr=192.168.1.4

What I want to do is be able to set some facts that contain the host aliases when I run this playbook on my [web01.example.com](http://web01.example.com) server:

- name: create alias facts  
set\_fact:  
file\_server\_alias: hostvars[{{ inventory\_hostname }}][‘groups’][‘fileservers’][0] # should get string ‘[fs01.example.com](http://fs01.example.com)’  
db\_server\_alias: hostvars[{{ inventory\_hostname }}][‘groups’][‘dbservers’][0] # should get string ‘[db01.example.com](http://db01.example.com)’  
cms\_server\_alias: hostvars[{{ inventory\_hostname }}][‘groups’][‘cmsservers’][0] # should get string ‘[cms01.example.com](http://cms01.example.com)’

However, when I do the above, file\_server\_alias just gets set to ‘hostvars[{{ inventory\_hostname }}][‘groups’][‘fileservers’][0]’, and so on. Is there any way to assign these inventory group aliases to variables?

Thanks.

---

<div class="post-metadata">

**Author:** ![Kai\_Stian\_Olstad](https://avatars.discourse-cdn.com/v4/letter/k/e19adc/32.png) [@Kai\_Stian\_Olstad](https://forum.ansible.com/u/Kai_Stian_Olstad)\
**Post date:** [January 17, 2019, 9:27pm UTC](https://forum.ansible.com/t/how-to-assign-inventory-group-alias-to-a-variable/29461/2 "2019-01-17T21:27:27Z")

</div>

The syntax is not correct, correct syntax should be  
&nbsp;&nbsp;&nbsp;file\_server\_alias: "{{ hostvars[inventory\_hostname]['groups']['fileservers'][0] }}"

But there is not point in using hostvars in this example since you can just use groups(one of the magic variables in Ansible)  
&nbsp;&nbsp;&nbsp;file\_server\_alias: "{{ groups['fileservers'][0] }}"

---

<div class="post-metadata">

**Author:** ![Robert\_F](https://avatars.discourse-cdn.com/v4/letter/r/9d8465/32.png) [@Robert\_F](https://forum.ansible.com/u/Robert_F)\
**Post date:** [January 17, 2019, 10:49pm UTC](https://forum.ansible.com/t/how-to-assign-inventory-group-alias-to-a-variable/29461/3 "2019-01-17T22:49:50Z")

</div>

Thank you! That is the solution.
