# Use hostvars as hosts names in playbooks

**URL:** <https://forum.ansible.com/t/use-hostvars-as-hosts-names-in-playbooks/19163>\
**Category:** Ansible Project\
**Created:** [September 2, 2015, 7:51am UTC](https://forum.ansible.com/t/use-hostvars-as-hosts-names-in-playbooks/19163 "2015-09-02T07:51:46Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Emmanuel\_Lacour](https://avatars.discourse-cdn.com/v4/letter/e/e19adc/32.png) [@Emmanuel\_Lacour](https://forum.ansible.com/u/Emmanuel_Lacour)\
**Post date:** [September 2, 2015, 7:51am UTC](https://forum.ansible.com/t/use-hostvars-as-hosts-names-in-playbooks/19163/1 "2015-09-02T07:51:46Z")

</div>

Dear ansible users,

I try to write a paybook where I first get a host name from a host, then would like to do thinks on this host name.

Here is a try:

---

<div class="post-metadata">

**Author:** ![Brian\_Coca](https://avatars.discourse-cdn.com/v4/letter/b/6de8d8/32.png) [@Brian\_Coca](https://forum.ansible.com/u/Brian_Coca)\
**Post date:** [September 2, 2015, 12:33pm UTC](https://forum.ansible.com/t/use-hostvars-as-hosts-names-in-playbooks/19163/2 "2015-09-02T12:33:57Z")

</div>

hostvars are not usable in 'hosts:' clause, use group\_by if you want  
to create adhoc groups for use in subsequent plays.

---

<div class="post-metadata">

**Author:** ![Emmanuel\_Lacour](https://avatars.discourse-cdn.com/v4/letter/e/e19adc/32.png) [@Emmanuel\_Lacour](https://forum.ansible.com/u/Emmanuel_Lacour)\
**Post date:** [September 2, 2015, 7:33pm UTC](https://forum.ansible.com/t/use-hostvars-as-hosts-names-in-playbooks/19163/3 "2015-09-02T19:33:17Z")

</div>

thanks, thought I discovered delegate\_to which seems to be exacly what I need, i.e. a playbook on the new vm with preliminary tasks executed on hypervisors to setup the vm, then tasks on the vm, then I may add tasks delegated\_to backup servers to register host for example. Nice!
