Ansible is ignoring my group vars, hoping I'm doing something obviously wrong...

33 views
Skip to first unread message

David Sudia

unread,
May 9, 2020, 7:32:49 PM5/9/20
to Ansible Project
Here is my directory structure:

dba.yml (a playbook)
inventory
/
 
union.gcp.yml (a dynamic gcp inventory)
  group_vars
/
    all
/
      vars
.yml
      vault
.yml
    dba
/
      vars
.yml
roles
/
  someroleshere
/

The command I'm running is
$ ansible-playbook -i inventory --vault-password-file=/path/to/file dba.yml

Ansible reads my inventory just fine and connects to correct VM. The first variable it wants is
disks
which is an array of dicts to format and mount.

Whether I put disks in the dba/vars.yml (the correct place for it to be) or all/vars.yml, Ansible gets to that variable and says it's undefined.

I have this big messy Ansible repo where everything works but it's a mess, that I'm starting fresh on because I read back through the docs and realized I wasn't following any best practices, and now I'm trying to follow them to the letter and can't get it to work.

Any help appreciated, thanks!

Vladimir Botka

unread,
May 9, 2020, 10:54:39 PM5/9/20
to David Sudia, ansible...@googlegroups.com
On Sat, 9 May 2020 16:32:49 -0700 (PDT)
David Sudia <dsu...@gospotcheck.com> wrote:

> dba.yml (a playbook)
> inventory/
> union.gcp.yml (a dynamic gcp inventory)
> group_vars/
> all/
> vars.yml
> vault.yml
> dba/
> vars.yml
> roles/
> someroleshere/
>
> $ ansible-playbook -i inventory --vault-password-file=/path/to/file dba.yml
>
> Whether I put disks in the dba/vars.yml (the correct place for it to be) or
> all/vars.yml, Ansible gets to that variable and says it's undefined.

There is nothing wrong with this

shell> cat group_vars/dba/vars.yml
disks: var from group_vars/dba/vars.yml

shell> cat inventory/hosts
[dba]
test_01
test_02
test_03

shell> cat dba.yml
- hosts: dba
tasks:
- debug:
var: disks

shell> ansible-playbook -i inventory/hosts dba.yml

PLAY [dba] **********

TASK [debug] ********
ok: [test_02] => {
"disks": "var from group_vars/dba/vars.yml"
}
ok: [test_01] => {
"disks": "var from group_vars/dba/vars.yml"
}
ok: [test_03] => {
"disks": "var from group_vars/dba/vars.yml"
}

--

David Sudia

unread,
May 10, 2020, 2:21:18 PM5/10/20
to Ansible Project
Hello Vladimir, thank you for the response!

My understanding was that group_vars needed to be in the same directory as the inventory to work, but in your example it looks like group_vars is one directory up, is that correct?

Vladimir Botka

unread,
May 10, 2020, 3:31:50 PM5/10/20
to David Sudia, ansible...@googlegroups.com
Hi David,

On Sun, 10 May 2020 11:21:18 -0700 (PDT)
David Sudia <dsu...@gospotcheck.com> wrote:

> My understanding was that group_vars needed to be in the same directory as
> the inventory to work, but in your example it looks like group_vars is one
> directory up, is that correct?

Yes. It is correct. Ansible loads host and group variable files by searching
paths relative to the inventory file or the playbook.

David Sudia

unread,
May 11, 2020, 1:17:33 PM5/11/20
to Ansible Project
To close this out, I realized from your example that in my debug statements I was putting the var in "{{}}", which was giving me bad info and making it look like the var was undefined. Once I took it out of the quotes/curlies, I saw that the var I was trying to use was being overridden by a var provided by my host.

So thank you for the help! Problem solved!
Reply all
Reply to author
Forward
0 new messages