# Collect hostnames in an array

**URL:** <https://forum.ansible.com/t/collect-hostnames-in-an-array/38135>\
**Category:** Ansible Project\
**Tags:** ansible-project\
**Created:** [June 21, 2023, 2:49am UTC](https://forum.ansible.com/t/collect-hostnames-in-an-array/38135 "2023-06-21T02:49:00Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Prady\_A](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/prady_a/32/4792_2.png) [@Prady\_A](https://forum.ansible.com/u/Prady_A)\
**Post date:** [June 21, 2023, 2:49am UTC](https://forum.ansible.com/t/collect-hostnames-in-an-array/38135/1 "2023-06-21T02:49:00Z")

</div>

Hi all

Pls help… need to collect all the hostnames in an array and pass that are art finally to an python script as an argument…so that I can process that python file with all hostname in a loop.

If any other way to doing it that would be certainly great…

Regards…  
Prady

---

<div class="post-metadata">

**Author:** ![dnmvisser](https://avatars.discourse-cdn.com/v4/letter/d/8e8cbc/32.png) [@dnmvisser](https://forum.ansible.com/u/dnmvisser)\
**Post date:** [June 21, 2023, 3:18am UTC](https://forum.ansible.com/t/collect-hostnames-in-an-array/38135/2 "2023-06-21T03:18:26Z")

</div>

[https://docs.ansible.com/ansible/latest/cli/ansible-inventory.html](https://docs.ansible.com/ansible/latest/cli/ansible-inventory.html)

---

<div class="post-metadata">

**Author:** ![Prady\_A](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/prady_a/32/4792_2.png) [@Prady\_A](https://forum.ansible.com/u/Prady_A)\
**Post date:** [June 21, 2023, 3:25am UTC](https://forum.ansible.com/t/collect-hostnames-in-an-array/38135/3 "2023-06-21T03:25:00Z")

</div>

Thank Dick…Thank you for your kindness…  
Sorry if I confused. My intention is to run the playbook and get all the hostnames in a variable .

Like :  
var=hostvars[ansible\_hostname][‘ansible\_hostname’]

With the above command I can fetch the hostname but how to append all the hostnames in an array so that finally I can pass that array to python script.

Regards

---

<div class="post-metadata">

**Author:** ![Akasurde](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/akasurde/32/230_2.png) [@Akasurde](https://forum.ansible.com/u/Akasurde)\
**Post date:** [June 21, 2023, 3:35am UTC](https://forum.ansible.com/t/collect-hostnames-in-an-array/38135/4 "2023-06-21T03:35:20Z")

</div>

I would write an Ansible module from the Python script and do the rest in Ansible. Anyways, check [https://docs.ansible.com/ansible/latest/reference\_appendices/special\_variables.html#magic-variables](https://docs.ansible.com/ansible/latest/reference_appendices/special_variables.html#magic-variables) for ansible\_play\_hosts and ansible\_play\_hosts\_all

---

<div class="post-metadata">

**Author:** ![Prady\_A](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/prady_a/32/4792_2.png) [@Prady\_A](https://forum.ansible.com/u/Prady_A)\
**Post date:** [June 21, 2023, 3:46am UTC](https://forum.ansible.com/t/collect-hostnames-in-an-array/38135/5 "2023-06-21T03:46:25Z")

</div>

Thank you Abhijeet…  
Actually I ve to add one role which can create an excel to automate with the hostnames in it…  
so what I thought to gather all the hostnames and process them using a python script to update the excel and send the excel in mail.

Regards
