Possible to use a variable value for the playbook "hosts" attribute?

Hi ! I would like to use a variable value for the playbook “hosts” clause, eg.:

  • name: PLAY_NAME
    hosts: “{{myvariable}}”
    sudo: yes
    tasks:

where the variable is defined elsewhere as eg.:

myvariable: “1.domain.com”

or:

myvariable: “group1:group2:group3”

However, when I attempt something like this, I always get the “skipping: no hosts matched” message when running the playbook (even if I put myvariable: “exact.host.name”).

Is it at all possible to use a variable here? Putting the pattern itself to every playbook is extremely unDRY, and driving me nuts.

It is possible to use a variable, your issue might be where you are defining it.

if you pass on the command line -e 'myvariable=group1:group2:group3',
it should work

So to clarify, it’s impossible to put the variable in eg. a group_vars/all file?

It it possible to put in in any other place than the command line?

you can put the variable in the groups_vars/all file, it just won't be
evaluated for the - hosts: entry, the play needs to know which hosts
it targets before using the inventory variables.

Ok, now I understand, thank you for help !