group_vars not visible to playbook

131 views
Skip to first unread message

John Wang

unread,
Jul 17, 2014, 12:09:18 AM7/17/14
to ansible...@googlegroups.com
Hi,

It's seemed that variables defined in the group_vars are not visible to playbooks. Then where should I keep the those variables? 
Here is what I'm trying to do:

directory tree:
-----------------------
foo.yml
hosts
group_vars
..|--  all
..|__ g1 
roles
..|-- bar
..|_ baa

foo.yml:
---------------------
---
- hosts: "{{ var1 }}"
  remote_user: john
  roles:
    - bar


group_vars/all
---------------------
---
var1: my_cluster
var2: some_value


hosts
---------------------
[my_cluster]


The problem in the above configuration is, the variable "var1" in the foo.yml cannot be resolved to "my_cluster" defined in group_vars/all . I have to use the -e option of ansible-playbook explicitly.  Is this how ansible works ? And how to realize my above purpose ?

Thanks and Regards
John

Serge van Ginderachter

unread,
Jul 17, 2014, 3:38:23 AM7/17/14
to ansible...@googlegroups.com

On 17 July 2014 06:09, John Wang <john.w...@gmail.com> wrote:
The problem in the above configuration is, the variable "var1" in the foo.yml cannot be resolved to "my_cluster" defined in group_vars/all . I have to use the -e option of ansible-playbook explicitly.  Is this how ansible works ? And how to realize my above purpose ?


You cant't define something that points to the inventory (hosts: in the play) with something that comes from the inventory (group_vars)​. That would create a black hole.

John Wang

unread,
Jul 17, 2014, 5:29:05 AM7/17/14
to ansible...@googlegroups.com
I felt that. Then is there a way to define variable which can be used in the position of the mentioned "var1" in foo.yml ? Is the command line options (i.e., -e) is the only way ?

Serge van Ginderachter

unread,
Jul 17, 2014, 5:33:30 AM7/17/14
to ansible...@googlegroups.com

On 17 July 2014 11:29, John Wang <john.w...@gmail.com> wrote:
I felt that. Then is there a way to define variable which can be used in the position of the mentioned "var1" in foo.yml ? Is the command line options (i.e., -e) is the only way ?


​I believe so yes. Not sure if there's a way to use a var from var: of vars_files: section, but maybe if set in a previous play.​

Michael DeHaan

unread,
Jul 17, 2014, 8:31:38 AM7/17/14
to ansible...@googlegroups.com
If you want to pass in the host specifier, you'll need to use "-e".  Or maybe a lookup plugin reading an environment variable.  -e is a bit clear.

-e @filename.yml is also a valid syntax.




--
You received this message because you are subscribed to the Google Groups "Ansible Project" group.
To unsubscribe from this group and stop receiving emails from it, send an email to ansible-proje...@googlegroups.com.
To post to this group, send email to ansible...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/ansible-project/CAEhzMJCeYJcE3MNUGtiyPx%2BdgpZ4RNFJGd42c2NWAXPT5NQk0w%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.

John Wang

unread,
Jul 17, 2014, 10:58:22 AM7/17/14
to ansible...@googlegroups.com

> If you want to pass in the host specifier, you'll need to use "-e".  Or maybe a lookup plugin reading an environment variable.  -e is a bit clear.
>
> -e @filename.yml is also a valid syntax.

Ok, I see. Thanks for mentioning the @filename.yml syntax also.

Reply all
Reply to author
Forward
0 new messages