# using inventory group name as var?

**URL:** <https://forum.ansible.com/t/using-inventory-group-name-as-var/30305>\
**Category:** Ansible Project\
**Created:** [May 10, 2019, 7:07pm UTC](https://forum.ansible.com/t/using-inventory-group-name-as-var/30305 "2019-05-10T19:07:40Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Matthew\_Arguin](https://avatars.discourse-cdn.com/v4/letter/m/e56c9b/32.png) [@Matthew\_Arguin](https://forum.ansible.com/u/Matthew_Arguin)\
**Post date:** [May 10, 2019, 7:07pm UTC](https://forum.ansible.com/t/using-inventory-group-name-as-var/30305/1 "2019-05-10T19:07:40Z")

</div>

I am sure that i am just glossing over this in the documentation, but here goes anyway:

i am tryingto identify how i can use a hosts group name

host\_group\_1:  
host1:  
host2:

in such a way that i can call it in a task and use the actual name of the group as a variable.

if i run a playbook against host1 from above and have a task that does a lineinfile on /etc/somefile:

line1  
line2  
line3  
lineX

the line that i am inserting would be “host\_group\_1” that i would do an insertbefore “lineX”

i am trying to avoid for every host group, having some variable with the same name as the host group. seems silly, redundant and avoidable. I am guessing that i am just not searching for the right thing in the documentation. any pointers?

---

<div class="post-metadata">

**Author:** ![Claudia\_de\_Luna](https://avatars.discourse-cdn.com/v4/letter/c/ecccb3/32.png) [@Claudia\_de\_Luna](https://forum.ansible.com/u/Claudia_de_Luna)\
**Post date:** [May 13, 2019, 1:27pm UTC](https://forum.ansible.com/t/using-inventory-group-name-as-var/30305/2 "2019-05-13T13:27:15Z")

</div>

Hi Matthew,

Not sure if this is what you are looking for but I use the host group name to create a custom directory to store files (basically show command output from network devices).

I use the ansible facts group\_name list which gives a list of all the groups to which the host belongs. In my case each hosts belongs to just a single group so that list will contain one element which I reference as you see by group\_names[0] and use it to build my custom directory (which I also time stamps so I get something like logs\_cisco\_2019-05-13 as my directory). You do have to gather facts which might make your run a bit longer.

**group\_name** s is one of the Ansible “Magic” Variables and you can read more about those here:  
[https://docs.ansible.com/ansible/latest/reference\_appendices/special\_variables.html](https://docs.ansible.com/ansible/latest/reference_appendices/special_variables.html)

group\_namesList of groups the current host is part of

`

- hosts: cisco  
gather\_facts: yes

- name: Create Directory  
file:  
path: ./logs\_{{ group\_names[0] }}\_{{ ansible\_date\_time.date }}  
state: directory  
`

Not sure thats what you were looking for but thought I would suggest on the off chance it is.

---

<div class="post-metadata">

**Author:** ![Matthew\_Arguin](https://avatars.discourse-cdn.com/v4/letter/m/e56c9b/32.png) [@Matthew\_Arguin](https://forum.ansible.com/u/Matthew_Arguin)\
**Post date:** [May 14, 2019, 11:51am UTC](https://forum.ansible.com/t/using-inventory-group-name-as-var/30305/3 "2019-05-14T11:51:22Z")

</div>

ha…that did the trick!

thanks

-m
