# How to get all hosts from inventory that are in a specified group into a variable??

**URL:** <https://forum.ansible.com/t/how-to-get-all-hosts-from-inventory-that-are-in-a-specified-group-into-a-variable/36375>\
**Category:** Ansible Project\
**Created:** [February 27, 2022, 9:46pm UTC](https://forum.ansible.com/t/how-to-get-all-hosts-from-inventory-that-are-in-a-specified-group-into-a-variable/36375 "2022-02-27T21:46:05Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![o\_haya1](https://avatars.discourse-cdn.com/v4/letter/o/a9adbd/32.png) [@o\_haya1](https://forum.ansible.com/u/o_haya1)\
**Post date:** [February 27, 2022, 9:46pm UTC](https://forum.ansible.com/t/how-to-get-all-hosts-from-inventory-that-are-in-a-specified-group-into-a-variable/36375/1 "2022-02-27T21:46:05Z")

</div>

Hi,

I am (still) new to working with Ansible…

In the inventory in our tower environment, we have groups configured.

I am trying to modify an existing playbook (and template file) to set a new property, “cache.memberlist”, with a value of all the hostnames in a specific group named, “SVC”.

I tested a small playbook:

- hosts: all  
gather\_facts: true  
tasks:
- set\_fact:  
host\_list: “{{ groups[‘SVC’] }}”
- debug:  
msg: “{{host\_list}}”

That does look like it is finding the hostnames, but (a) it is doing that for every host in the inventory and (b) it looks like, for every host, it is trying to connect to that host.

Is there some way that the playbook can just find (and return) the hostnames that are in the SVC group and return that?

Thanks,  
Jim

---

<div class="post-metadata">

**Author:** ![o\_haya1](https://avatars.discourse-cdn.com/v4/letter/o/a9adbd/32.png) [@o\_haya1](https://forum.ansible.com/u/o_haya1)\
**Post date:** [February 27, 2022, 11:03pm UTC](https://forum.ansible.com/t/how-to-get-all-hosts-from-inventory-that-are-in-a-specified-group-into-a-variable/36375/2 "2022-02-27T23:03:44Z")

</div>

Hi,

I think that I figured my question out… it was a “momentary fog” on my part :(… I have been using mostly ansible-playbook, and hardly ever used tower, but I just realized that I needed to put the specific hosts in the “limit” on the template page in Tower. Then, it only runs the playbook against those specific hosts.

Sorry about that!

Jim
