magic variable for target group?

14 views
Skip to first unread message

Guy Matz

unread,
Dec 1, 2020, 11:41:53 AM12/1/20
to ansible...@googlegroups.com
Hi!  Does anyone know if there's a variable set for the target group of a play?

For example, for this:
- hosts: prime
  roles:
      - nginx
      - postfix

Does any variable get set to "prime" in a deterministic way?

Thanks!
Guy

Vladimir Botka

unread,
Dec 1, 2020, 1:16:24 PM12/1/20
to Guy Matz, ansible...@googlegroups.com
On Tue, 1 Dec 2020 11:41:20 -0500
Guy Matz <guy...@gmail.com> wrote:

> Hi! Does anyone know if there's a variable set for the target group of a
> play?
>
> For example, for this:
> - hosts: *prime*
> roles:
> - nginx
> - postfix
>
> Does any variable get set to "prime" in a deterministic way?

No. There is no such "special variable"
https://docs.ansible.com/ansible/latest/reference_appendices/special_variables.html

There is only variable *group_names*

"List of groups the current host is part of"

and variable "groups"

"A dictionary/map with all the groups in inventory and each group
has the list of hosts that belong to it"

Ansible doesn't store the structure of inventory groups. Quoting from
"How variables are merged"
https://docs.ansible.com/ansible/latest/user_guide/intro_inventory.html#how-variables-are-merged

"... This keeps Ansible focused on the Host and Task, so groups
don’t really survive outside of inventory and host matching. ..."


Do you have any use-case which needs such variable?

--
Vladimir Botka

Vladimir Botka

unread,
Dec 1, 2020, 4:40:24 PM12/1/20
to Guy Matz, Ansible Project
> > > Hi! Does anyone know if there's a variable set for the target group of a
> > > play?
> > > For example, for this:
> > > - hosts: *prime*
> > > roles:
> > > - nginx
> > > - postfix
> > > Does any variable get set to "prime" in a deterministic way?

> > No. There is no such "special variable"
> > https://docs.ansible.com/ansible/latest/reference_appendices/special_variables.html
> >
> > ... variable "groups"
> >
> > "A dictionary/map with all the groups in inventory and each group
> > has the list of hosts that belong to it"
> > ...
> > Do you have any use-case which needs such variable?

> Yes. I would like to have common roles, e.g. httpd, that could be
> "app-aware" based on the target group.

What do you mean by "'app-aware' role based on the target group"?

--
Vladimir Botka
Reply all
Reply to author
Forward
0 new messages