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.
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.
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
`
If I run ansible-playbook -i inventory playbook.yml - this fails with no hosts matched because {{ groupname }} is undefined on host level.
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?
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
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”.
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.
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.