# how to access hosts variable in ansible tasks

**URL:** <https://forum.ansible.com/t/how-to-access-hosts-variable-in-ansible-tasks/30111>\
**Category:** Ansible Project\
**Created:** [April 14, 2019, 4:04am UTC](https://forum.ansible.com/t/how-to-access-hosts-variable-in-ansible-tasks/30111 "2019-04-14T04:04:45Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Kunalsing\_Thakur](https://avatars.discourse-cdn.com/v4/letter/k/3d9bf3/32.png) [@Kunalsing\_Thakur](https://forum.ansible.com/u/Kunalsing_Thakur)\
**Post date:** [April 14, 2019, 4:04am UTC](https://forum.ansible.com/t/how-to-access-hosts-variable-in-ansible-tasks/30111/1 "2019-04-14T04:04:45Z")

</div>

- hosts: [test.example.com](http://test.example.com), [test1.example.com](http://test1.example.com)  
gather\_facts: false  
tasks:
- name: checking uptime for {{ ansible\_hostname }}  
command: uptime  
when: ( “slave” in uriresult.content )

---

<div class="post-metadata">

**Author:** ![Kunalsing\_Thakur](https://avatars.discourse-cdn.com/v4/letter/k/3d9bf3/32.png) [@Kunalsing\_Thakur](https://forum.ansible.com/u/Kunalsing_Thakur)\
**Post date:** [April 14, 2019, 4:53am UTC](https://forum.ansible.com/t/how-to-access-hosts-variable-in-ansible-tasks/30111/2 "2019-04-14T04:53:07Z")

</div>

One more thing i don’t want to use gather\_facts: true

---

<div class="post-metadata">

**Author:** ![Raghavendra\_Rao](https://avatars.discourse-cdn.com/v4/letter/r/c77e96/32.png) [@Raghavendra\_Rao](https://forum.ansible.com/u/Raghavendra_Rao)\
**Post date:** [April 14, 2019, 10:13am UTC](https://forum.ansible.com/t/how-to-access-hosts-variable-in-ansible-tasks/30111/3 "2019-04-14T10:13:24Z")

</div>

You can use inventory\_hostname.

This takes the name of the hosts as configured in the inventory file.

---

<div class="post-metadata">

**Author:** ![Kunalsing\_Thakur](https://avatars.discourse-cdn.com/v4/letter/k/3d9bf3/32.png) [@Kunalsing\_Thakur](https://forum.ansible.com/u/Kunalsing_Thakur)\
**Post date:** [April 14, 2019, 12:54pm UTC](https://forum.ansible.com/t/how-to-access-hosts-variable-in-ansible-tasks/30111/4 "2019-04-14T12:54:02Z")

</div>

can you point me to Ansible doc For this also

I need to use only test or test1

---

<div class="post-metadata">

**Author:** ![Raghavendra\_Rao](https://avatars.discourse-cdn.com/v4/letter/r/c77e96/32.png) [@Raghavendra\_Rao](https://forum.ansible.com/u/Raghavendra_Rao)\
**Post date:** [April 14, 2019, 6:54pm UTC](https://forum.ansible.com/t/how-to-access-hosts-variable-in-ansible-tasks/30111/5 "2019-04-14T18:54:53Z")

</div>

[https://docs.ansible.com/ansible/latest/user\_guide/playbooks\_variables.html#accessing-information-about-other-hosts-with-magic-variables](https://docs.ansible.com/ansible/latest/user_guide/playbooks_variables.html#accessing-information-about-other-hosts-with-magic-variables)
