# Get a list of ansible hosts

**URL:** <https://forum.ansible.com/t/get-a-list-of-ansible-hosts/36727>\
**Category:** Ansible Project\
**Created:** [June 13, 2022, 12:16pm UTC](https://forum.ansible.com/t/get-a-list-of-ansible-hosts/36727 "2022-06-13T12:16:54Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Tobi](https://avatars.discourse-cdn.com/v4/letter/t/b9e5f3/32.png) [@Tobi](https://forum.ansible.com/u/Tobi)\
**Post date:** [June 13, 2022, 12:16pm UTC](https://forum.ansible.com/t/get-a-list-of-ansible-hosts/36727/1 "2022-06-13T12:16:54Z")

</div>

Hello everyone!

I’m new here and also quiet new to ansible as well 🙂

I will try to summarize:

The goal:  
I wanna get a list of ansible hosts (could be made out of the ansible inv e.g.) and each element should be seperated by a space. The reason for that is that I want to run a bash script which needs a list attached to it like “script.sh host\_1 host\_2 host\_3 …”

The problem:  
I need to specify one host, which needs to be in front of every other host e.g. “script.sh host\_ **2** host\_1 host\_3 …”

My ansible inventory.ini example looks like:  
[manager]  
host\_2.example.de

[utility\_nodes]  
host\_1.example.de host\_template=Utility1  
host\_2.example.de host\_template=Utility2

[master\_nodes]  
host\_3.example.de host\_template=Master1  
host\_4.example.de host\_template=Master2

Many thanks in advance and  
Best regards  
Tobi

---

<div class="post-metadata">

**Author:** ![Walter\_Rowe](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/walter_rowe/32/4506_2.png) [@Walter\_Rowe](https://forum.ansible.com/u/Walter_Rowe)\
**Post date:** [June 13, 2022, 12:53pm UTC](https://forum.ansible.com/t/get-a-list-of-ansible-hosts/36727/2 "2022-06-13T12:53:18Z")

</div>

[https://docs.ansible.com/ansible/latest/reference\_appendices/special\_variables.html](https://docs.ansible.com/ansible/latest/reference_appendices/special_variables.html)

groups

A dictionary/map with all the groups in inventory and each group has the list of hosts that belong to it

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/ansible/original/1X/1882d9a01da5d8996a862359b0913cc568a39b84.svg) [@system](https://forum.ansible.com/u/system)\
**Post date:** [June 13, 2022, 6:55pm UTC](https://forum.ansible.com/t/get-a-list-of-ansible-hosts/36727/3 "2022-06-13T18:55:41Z")

</div>

{{' '.join(listofhosts) }}

as to what provides the 'list of hosts' it could be just  
groups['groupname'] or if you need host patterns  
lookup('inventory\_hostnames', 'all:!manager')
