# Hosts File - Get group name , only one item from group based on lookup

**URL:** <https://forum.ansible.com/t/hosts-file-get-group-name-only-one-item-from-group-based-on-lookup/32659>\
**Category:** Ansible Project\
**Created:** [April 12, 2020, 6:04am UTC](https://forum.ansible.com/t/hosts-file-get-group-name-only-one-item-from-group-based-on-lookup/32659 "2020-04-12T06:04:42Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Adam\_McGill](https://avatars.discourse-cdn.com/v4/letter/a/a9adbd/32.png) [@Adam\_McGill](https://forum.ansible.com/u/Adam_McGill)\
**Post date:** [April 12, 2020, 6:04am UTC](https://forum.ansible.com/t/hosts-file-get-group-name-only-one-item-from-group-based-on-lookup/32659/1 "2020-04-12T06:04:42Z")

</div>

Is it possible to specify in a playbook hosts value to be a group item from hosts file?

**example:**

Hosts file contains:

[groupone]  
comname1  
comname2  
comname3

in the playbook would like it to grab the group (groupone) but only apply the playbook routine to a name/value in that group and not all in that group.

groupone | comname2

---

<div class="post-metadata">

**Author:** ![racke](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/racke/32/4767_2.png) [@racke](https://forum.ansible.com/u/racke)\
**Post date:** [April 12, 2020, 8:27am UTC](https://forum.ansible.com/t/hosts-file-get-group-name-only-one-item-from-group-based-on-lookup/32659/2 "2020-04-12T08:27:15Z")

</div>

> Is it possible to specify in a playbook hosts value to be a group item from hosts file?
> 
> \*\_example:\_\*
> 
> Hosts file contains:
> 
> [groupone]  
> comname1  
> comname2  
> comname3
> 
> in the playbook would like it to grab the group (groupone) but only apply the playbook routine to a name/value in that  
> group and not all in that group.
> 
> groupone | comname2

You get the members of this group with the variable groups['groupone'].

Regards  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Racke

---

<div class="post-metadata">

**Author:** ![Adam\_McGill](https://avatars.discourse-cdn.com/v4/letter/a/a9adbd/32.png) [@Adam\_McGill](https://forum.ansible.com/u/Adam_McGill)\
**Post date:** [April 12, 2020, 5:50pm UTC](https://forum.ansible.com/t/hosts-file-get-group-name-only-one-item-from-group-based-on-lookup/32659/3 "2020-04-12T17:50:36Z")

</div>

Can i specify it on the hosts: line in the playbook is what i am trying to understand and point it to a value in the group.

hosts:  
[‘groupone’][‘comname2’]  
groupone.comname2

gives me warning , could not match supplied host pattern.

tried a few things like above, running into a syntax issue it seems or there isn’t a way to do it. Online documentation referencing a lot with referencing groups to combine or exclude. Which isn’t what i want,  
as i just want to reference a name/value in a group.

---

<div class="post-metadata">

**Author:** ![racke](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/racke/32/4767_2.png) [@racke](https://forum.ansible.com/u/racke)\
**Post date:** [April 12, 2020, 6:38pm UTC](https://forum.ansible.com/t/hosts-file-get-group-name-only-one-item-from-group-based-on-lookup/32659/4 "2020-04-12T18:38:24Z")

</div>

> Can i specify it on the hosts: line in the playbook is what i am trying to understand and point it to a value in the group.
> 
> hosts:  
> ['groupone']['comname2']  
> groupone.comname2
> 
> gives me warning , could not match supplied host pattern.
> 
> tried a few things like above, running into a syntax issue it seems or there isn't a way to do it. Online documentation  
> referencing a lot with referencing groups to combine or exclude. Which isn't what i want,  
> as i just want to reference a name/value in a group.

There are a number of possibilities which comes to my mind:

- create new group for comname2, which can be included into [groupone] through [groupone:children]  
- create dynamic group in the playbook with add\_host task  
- use --limit on the command line

Regards  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Racke

---

<div class="post-metadata">

**Author:** ![Adam\_McGill](https://avatars.discourse-cdn.com/v4/letter/a/a9adbd/32.png) [@Adam\_McGill](https://forum.ansible.com/u/Adam_McGill)\
**Post date:** [April 12, 2020, 8:09pm UTC](https://forum.ansible.com/t/hosts-file-get-group-name-only-one-item-from-group-based-on-lookup/32659/5 "2020-04-12T20:09:00Z")

</div>

I took the route of using children groups. Too bad there wasn’t a way to pull a group and pick a value in that group passing to the Hosts: value in a playbook. Thanks for the assist/suggestions  
Stefan.
