using a variable as a group name when executing a task ?

Hi all ! is there any way of achieving the following ?
( i’ve tried this ones and i get “no hosts matched” )

monitor_group = “glance”

  • hosts: “{{ monitor_group }}”
    roles:
  • { role: icinga2-check-commands, tags: [‘monitor-cloud’] }

OR

monitor_group = glance

  • hosts: “{{ monitor_group }}”
    roles:
  • { role: icinga2-check-commands, tags: [‘monitor-cloud’] }

What about:

- vars:
  monitor_group: "glance"
- hosts: "{{ monitor_group }}"
  roles:
    - { role: icinga2-check-commands, tags: ['monitor-cloud'] }

I'm not saying that this'll work as I am trying to figure out variables myself. But you could give it a try.

Thank for the response, sorry that i didnt clarify this.
The monitor_cloud variable is in the [all:vars] section of the inventory.

I'm not sure I understand which variable troubles you. Is it "monitor_group" or "monitor-cloud" or "monitor_cloud"? Is there a
typo (underscore / dash)?

My problem is that i cant use a variable for the "- host: " part to realize what servers to run on.

anyone ?

group_vars/ are loaded AFTER -hosts: so you cannot use them to set the
hosts, you can do this using extra vars though:

ansible-playbook ... play.yml ... -e 'monitor_group=<hostname>'

Genius Brian.
thanks so much !