# hostname as a variable in a playbook based on inventory vars

**URL:** <https://forum.ansible.com/t/hostname-as-a-variable-in-a-playbook-based-on-inventory-vars/35626>\
**Category:** Ansible Project\
**Created:** [August 16, 2021, 4:59am UTC](https://forum.ansible.com/t/hostname-as-a-variable-in-a-playbook-based-on-inventory-vars/35626 "2021-08-16T04:59:18Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Saranya\_N](https://avatars.discourse-cdn.com/v4/letter/s/898d66/32.png) [@Saranya\_N](https://forum.ansible.com/u/Saranya_N)\
**Post date:** [August 16, 2021, 4:59am UTC](https://forum.ansible.com/t/hostname-as-a-variable-in-a-playbook-based-on-inventory-vars/35626/1 "2021-08-16T04:59:18Z")

</div>

I need to get the hostname as a variable in my playbook

I have the inventory defined the inventory like below

[group1]  
[host1.vs.com](http://host1.vs.com) type=secondary servertype=vm  
[host2.vs.com](http://host2.vs.com) type=primary servertype=vm

[group2]  
[host4.vs.com](http://host4.vs.com) type=primary servertype=physical

[host3.vs.com](http://host3.vs.com) type=secondary servertype=physical

I run my playbook with --limit option always so using ansible\_limit I’m able to get the variable name of the group.

now depending on the group i need to get the hostname which is secondary as a variable.  
id if --limit is group1, I need [host1.vs.com](http://host1.vs.com) as a var…how can i do that?

any help is appreciated, thank you

---

<div class="post-metadata">

**Author:** ![Parth\_Patel](https://avatars.discourse-cdn.com/v4/letter/p/e9a140/32.png) [@Parth\_Patel](https://forum.ansible.com/u/Parth_Patel)\
**Post date:** [August 16, 2021, 5:06am UTC](https://forum.ansible.com/t/hostname-as-a-variable-in-a-playbook-based-on-inventory-vars/35626/2 "2021-08-16T05:06:19Z")

</div>

HI Saranya,

If i didn’t misunderstood, would you like to make use of inbuilt special variables to get hostname as - inventory\_hostname, inventory\_hostname\_short which directly gives you hostname value and it can be stored in variable or printed using debug module.

PS :- You can also make use of facts to get hostname → parse it → store value in variable.

Cheers,  
Parth

---

<div class="post-metadata">

**Author:** ![Saranya\_N](https://avatars.discourse-cdn.com/v4/letter/s/898d66/32.png) [@Saranya\_N](https://forum.ansible.com/u/Saranya_N)\
**Post date:** [August 16, 2021, 5:30am UTC](https://forum.ansible.com/t/hostname-as-a-variable-in-a-playbook-based-on-inventory-vars/35626/3 "2021-08-16T05:30:01Z")

</div>

H ello,

I did look at magic variables and i was not able to get what i wanted…  
i basically want to get hostname which is of type secondary from group1 … so that i can store it in a var and use elsewhere…

---

<div class="post-metadata">

**Author:** ![Saranya\_N](https://avatars.discourse-cdn.com/v4/letter/s/898d66/32.png) [@Saranya\_N](https://forum.ansible.com/u/Saranya_N)\
**Post date:** [August 16, 2021, 5:31am UTC](https://forum.ansible.com/t/hostname-as-a-variable-in-a-playbook-based-on-inventory-vars/35626/4 "2021-08-16T05:31:21Z")

</div>

I know i can use inventory\_hostname but based on condition I want to get that i.,e type secondary from group1

---

<div class="post-metadata">

**Author:** ![Parth\_Patel](https://avatars.discourse-cdn.com/v4/letter/p/e9a140/32.png) [@Parth\_Patel](https://forum.ansible.com/u/Parth_Patel)\
**Post date:** [August 16, 2021, 6:27am UTC](https://forum.ansible.com/t/hostname-as-a-variable-in-a-playbook-based-on-inventory-vars/35626/5 "2021-08-16T06:27:35Z")

</div>

iterating over hostvars data looks like a bug reported by few of them (found articles). tried but not able to iterate over hostvars using loop or with\_items.

 ![image.png](https://us1.discourse-cdn.com/ansible/original/2X/5/58e18fb3279e167c50d94deb1c44f02ddd7b05d0.png)

 ![image.png](https://us1.discourse-cdn.com/ansible/original/2X/f/fa5cb82d306578ef0c502e8858df1f2620c6c014.png)

 ![image.png](https://us1.discourse-cdn.com/ansible/original/2X/c/cac2053ab1316b02e043d7e3064b3c5bdbc7fe62.png)

---

<div class="post-metadata">

**Author:** ![Parth\_Patel](https://avatars.discourse-cdn.com/v4/letter/p/e9a140/32.png) [@Parth\_Patel](https://forum.ansible.com/u/Parth_Patel)\
**Post date:** [August 16, 2021, 6:30am UTC](https://forum.ansible.com/t/hostname-as-a-variable-in-a-playbook-based-on-inventory-vars/35626/6 "2021-08-16T06:30:17Z")

</div>

[https://gitmemory.com/issue/ansible/ansible/68364/683214528](https://gitmemory.com/issue/ansible/ansible/68364/683214528)

[https://github.com/ansible/ansible/issues/68364](https://github.com/ansible/ansible/issues/68364)

> **(attachments)**
>
> ![image.png](https://us1.discourse-cdn.com/ansible/original/2X/5/58e18fb3279e167c50d94deb1c44f02ddd7b05d0.png)  
> ![image.png](https://us1.discourse-cdn.com/ansible/original/2X/f/fa5cb82d306578ef0c502e8858df1f2620c6c014.png)  
> ![image.png](https://us1.discourse-cdn.com/ansible/original/2X/c/cac2053ab1316b02e043d7e3064b3c5bdbc7fe62.png)
