Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Ansible variable defined in group_vars/all not found

Assume the Ansible structure:

.
├── group_vars
│   └── all
└── site.yml

Where all contains my_test_variable: yes, and site.yml is:

- hosts: all

  tasks:
    - name: Variable test
      debug: msg={{ my_test_variable }}

I'm using Vagrant to run it locally, so the command looks like:

$ ansible-playbook site.yml -i /path-to-vagrant/.vagrant/provisioners/ansible/inventory/vagrant_ansible_inventory --private-key=/path-to-vagrant/.vagrant/machines/default/virtualbox/private_key -u vagrant

Vagrant's generated invertory file:

# Generated by Vagrant

default ansible_ssh_host=127.0.0.1 ansible_ssh_port=2222

And the output:

PLAY: ***************************************************************************

TASK [setup] ********************************************************************
ok: [default]

TASK [Variable test] ************************************************************
fatal: [default]: FAILED! => {"msg": "ERROR! the field 'args' has an invalid value, which appears to include a variable that is undefined. The error was: 'my_test_variable' is undefined", "failed": true}

PLAY RECAP **********************************************************************
default                    : ok=1    changed=0    unreachable=0    failed=1 

I know this vagrant inventory is not inside any group - because there isn't any - but all groups inherit from all, right?

Why doesn't it work? What did I miss?


I'm Quite new to Ansible. Read a lot of the docs, couple of examples and even some SO questions like Ansible doesn't pick up group_vars without loading it manually -- not quite my problem -- and Cannot get ansible to recognize group variables -- close but not there either.


Edit

I'm following the recommended project structure from Ansible's doc, and in the variables doc entry they mention the global_vars/all:

Site wide defaults should be defined as a ‘group_vars/all’ setting.

Even though there is no direct reference on how to load these default values, I assume I don't have to explicitly add them (like suggested in the answer by @thiago-borges). Or do I?

The reason for this is that I intend to have group vars inheriting from all, like:

.
├── group_vars
│   └── all
│   └── production
│   └── staging
└── site.yml

And when I execute ansible-playbook for each, different files are loaded without I having to explicitly set them in the play file, eg:

ansible-playbook -i production site.yml


Edit 2

The issue was a bug on ansible. After an update it worked as documented.

Should I delete this question then?

like image 701
alfetopito Avatar asked Jul 10 '15 14:07

alfetopito


People also ask

What is Group_vars all?

group_vars/all is used to set variables that will be used for every host that Ansible is ran against.

How does Group_vars work in Ansible?

The group_vars in Ansible are a convenient way to apply variables to multiple hosts at once. Group_vars is an Ansible-specific folder as part of the repository structure. This folder contains YAML files created to have data models, and these data models apply to all the devices listed in the hosts. ini file.

Where is Group_vars in Ansible?

In this, group_vars directory is under Ansible base directory, which is by default /etc/ansible/. The files under group_vars can have extensions including '. yaml', '. yml', '.

What is the difference between Group_vars & Host_vars directory?

The names of the YAML files in group_vars must match the group defined in the inventory and also the YAML files in host_vars must be named exactly as the hosts in the inventory.


1 Answers

On your site.yml, try setting the var_files configuration.

- hosts: all
  vars_files:
    - group_vars/all
  tasks:
    - name: Variable test
      debug: msg={{ my_test_variable }}
like image 66
Thiago Borges Avatar answered Sep 22 '22 09:09

Thiago Borges