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

40 views
Skip to first unread message

JD

unread,
Dec 23, 2014, 2:39:54 PM12/23/14
to ansible...@googlegroups.com
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.

Brian Coca

unread,
Dec 23, 2014, 2:45:08 PM12/23/14
to ansible...@googlegroups.com
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


--
Brian Coca

JD

unread,
Dec 23, 2014, 2:53:20 PM12/23/14
to ansible...@googlegroups.com
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?

Brian Coca

unread,
Dec 23, 2014, 2:54:41 PM12/23/14
to ansible...@googlegroups.com
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.


--
Brian Coca

JD

unread,
Dec 23, 2014, 2:58:17 PM12/23/14
to ansible...@googlegroups.com
Ok, now I understand, thank you for help !
Reply all
Reply to author
Forward
0 new messages