Global variables file

I need to define truly global variables. I know about using the ALL group_vars but the variable there are not truly global. For example if groupname is defined in for the ALL group in group_vars this doesn’t work:

  • host: {{ groupname }}

My questions are: Is this expected to work? If not, can we think of truly global variables or this is not a supported scenario?

In my opinion it is OK to make the variables from the all group be available globally.

What you probably want to use is ‘vars_files’ in a play and load in a YAML file. Those vars are scoped more globally, and not tied to individual hosts or groups.

http://docs.ansible.com/playbooks_variables.html#variable-file-separation

Yeah, that’s how we do it in the moment. What I don’t like is that vars_files has to be repeated on every play or include. A conventional way to include truly global variables would be great. What do you think about making from the ALL group truly global? I am no expert but it seems this would be completely backwards compatible.

The group_vars/all file alongside inventory is loaded for everything, just at lower priority.

I use it frequently for global things.

Ansible in general likes it when you only have variables defined in one place - it’s fine if you don’t - but it’s designed to be simple. Thus as long as you don’t need them to be super-high-priority, group_vars/all is a great place for that.

michael,

I use the same group_vars/all approach but it is not truly global. Example (from my first post):

playbook.yml :

`

  • host: {{ groupname }}
    tasks:
  • name: test
    raw: ls

`

inventory/group_vars/all :

`
groupname: localhost

`

  1. If I run ansible-playbook -i inventory playbook.yml - this fails with no hosts matched because {{ groupname }} is undefined on host level.
  2. If I run ansible-playbook -i inventory playbook.yml --extra-vars “groupname=localhost” - this work as expected because groupname defined on command line is trully global.

Do you think that the first scenario should work as well? Can we consider this a bug or is this by design?

This is not a thing, but if you were going to hardcode it, you could hardcode it like

–extra-vars “@vars.yml

Ansible will fail if the variable is undefined, so there’d be no worry of leaving if off.

Yeap, that’s another solution I am aware of but I think it would be cleaner to have global variables declared conventionally. The situation right now is - if you want truly global variables, you should pass them on the command line.

That’s just my opinion. Definitely this is not a showstopper just a minor workaround :wink:

I also needed global variables from time to time as well. Maybe we could add something like global_vars/ or something like that?

Or even better allow vars_file to be included on the playbook level, so it is automatically used in all plays of the playbook, that would remove the need for -e “@vars.yml”.

Sorry, this isn’t going to be a thing.

We don’t want to introduce more levels of variable features at this time and would like to keep them well managed.

But what is a problem with vars_files being defined on top of a playbook? It should work the same as if we added the same vars_files to all plays, I don’t see a downside to that and it should probably be even easy to implement it.

Alternatively, we can make variables for the all group to be truly global and not introduce any new variables sources :wink:

For me this would be a nice addition but it is not critical. I am OK with not doing anything for the moment.

Changing how group_vars/all works could possibly introduce a lot of problems to old playbooks and plays, but making it possible to set vars_files on playbook level won’t introduce any, since it wasn’t possible before.

Indeed, we don’t want to keep moving precedence controls around.

as such, we will not be making any changes.