inventory_groupname variable (similar to inventory_hostname)

29 views
Skip to first unread message

Darshan Shroff

unread,
Jul 30, 2017, 10:49:02 PM7/30/17
to Ansible Project
Ansible FAQ clarifies
"‘inventory_hostname’ is a magic variable that indicates the current host you are looping over in the host loop."

Is there a similar concept of inventory_groupname that can be made available to be used inside a playbook ?


e.g. If we are running a playbook in this manner

ansible-playbook -i inventory/prod site.yml --limit service_a


And 'prod' inventory is defined as below

[service_a]

host1

host2


[service_b]

host2

host3


Then is it feasible to reference the group name - service_a - inside site.yml in this manner -  {{ inventory_groupname }}


Kai Stian Olstad

unread,
Jul 31, 2017, 5:18:50 AM7/31/17
to ansible...@googlegroups.com
On 31. juli 2017 04:49, Darshan Shroff wrote:
> Ansible FAQ clarifies
> "‘inventory_hostname’ is a magic variable that indicates the current host
> you are looping over in the host loop."
>
> Is there a similar concept of *inventory_groupname* that can be made
> available to be used inside a playbook ?

Unfortunately not.


> e.g. If we are running a playbook in this manner
>
> ansible-playbook -i inventory/prod site.yml --limit service_a
>
>
> And 'prod' inventory is defined as below
>
> [service_a]
> host1
> host2
>
> [service_b]
> host2
> host3
>
>
> Then is it feasible to reference the group name - service_a - inside
> site.yml in this manner - {{ inventory_groupname }}

You'll need to provide that with --extra-vars

ansible-playbook -i inventory/prod site.yml --limit service_a
--extra-vars mylimit=service_a


I think this should work so you don't have to specify both limit and mylimit

ansible-playbook -i inventory/prod site.yml -e mylimit=service_a

site.yml
---
- hosts: all:&{{ mylimit | default('all') }}


--
Kai Stian Olstad
Reply all
Reply to author
Forward
0 new messages