inventory_groupname variable (similar to inventory_hostname)

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 }}

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