# Global variables file

**URL:** <https://forum.ansible.com/t/global-variables-file/16346>\
**Category:** Ansible Developer\
**Created:** [September 25, 2014, 8:41pm UTC](https://forum.ansible.com/t/global-variables-file/16346 "2014-09-25T20:41:12Z")\
**Posts on this page:** 13\
**Page:** 1

<div class="post-metadata">

**Author:** ![Ivaylo\_Bratoev](https://avatars.discourse-cdn.com/v4/letter/i/f9ae1b/32.png) [@Ivaylo\_Bratoev](https://forum.ansible.com/u/Ivaylo_Bratoev)\
**Post date:** [September 25, 2014, 8:41pm UTC](https://forum.ansible.com/t/global-variables-file/16346/1 "2014-09-25T20:41:12Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![sivel](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/sivel/32/1198_2.png) [@sivel](https://forum.ansible.com/u/sivel)\
**Post date:** [September 25, 2014, 9:54pm UTC](https://forum.ansible.com/t/global-variables-file/16346/2 "2014-09-25T21:54:34Z")

</div>

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](http://docs.ansible.com/playbooks_variables.html#variable-file-separation)

---

<div class="post-metadata">

**Author:** ![Ivaylo\_Bratoev](https://avatars.discourse-cdn.com/v4/letter/i/f9ae1b/32.png) [@Ivaylo\_Bratoev](https://forum.ansible.com/u/Ivaylo_Bratoev)\
**Post date:** [September 26, 2014, 8:42pm UTC](https://forum.ansible.com/t/global-variables-file/16346/3 "2014-09-26T20:42:49Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![Michael\_DeHaan1](https://avatars.discourse-cdn.com/v4/letter/m/5e9695/32.png) [@Michael\_DeHaan1](https://forum.ansible.com/u/Michael_DeHaan1)\
**Post date:** [October 1, 2014, 12:37pm UTC](https://forum.ansible.com/t/global-variables-file/16346/4 "2014-10-01T12:37:04Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![Ivaylo\_Bratoev](https://avatars.discourse-cdn.com/v4/letter/i/f9ae1b/32.png) [@Ivaylo\_Bratoev](https://forum.ansible.com/u/Ivaylo_Bratoev)\
**Post date:** [October 2, 2014, 12:27pm UTC](https://forum.ansible.com/t/global-variables-file/16346/5 "2014-10-02T12:27:06Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![Michael\_DeHaan1](https://avatars.discourse-cdn.com/v4/letter/m/5e9695/32.png) [@Michael\_DeHaan1](https://forum.ansible.com/u/Michael_DeHaan1)\
**Post date:** [October 2, 2014, 8:19pm UTC](https://forum.ansible.com/t/global-variables-file/16346/6 "2014-10-02T20:19:40Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![Ivaylo\_Bratoev](https://avatars.discourse-cdn.com/v4/letter/i/f9ae1b/32.png) [@Ivaylo\_Bratoev](https://forum.ansible.com/u/Ivaylo_Bratoev)\
**Post date:** [October 3, 2014, 6:58pm UTC](https://forum.ansible.com/t/global-variables-file/16346/7 "2014-10-03T18:58:43Z")

</div>

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 😉

---

<div class="post-metadata">

**Author:** ![Strahinja\_Kustudic2](https://avatars.discourse-cdn.com/v4/letter/s/b4bc9f/32.png) [@Strahinja\_Kustudic2](https://forum.ansible.com/u/Strahinja_Kustudic2)\
**Post date:** [October 5, 2014, 7:40pm UTC](https://forum.ansible.com/t/global-variables-file/16346/8 "2014-10-05T19:40:17Z")

</div>

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”.

---

<div class="post-metadata">

**Author:** ![Michael\_DeHaan1](https://avatars.discourse-cdn.com/v4/letter/m/5e9695/32.png) [@Michael\_DeHaan1](https://forum.ansible.com/u/Michael_DeHaan1)\
**Post date:** [October 6, 2014, 12:26pm UTC](https://forum.ansible.com/t/global-variables-file/16346/9 "2014-10-06T12:26:03Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![Strahinja\_Kustudic1](https://avatars.discourse-cdn.com/v4/letter/s/cc9497/32.png) [@Strahinja\_Kustudic1](https://forum.ansible.com/u/Strahinja_Kustudic1)\
**Post date:** [October 6, 2014, 12:33pm UTC](https://forum.ansible.com/t/global-variables-file/16346/10 "2014-10-06T12:33:46Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![Ivaylo\_Bratoev](https://avatars.discourse-cdn.com/v4/letter/i/f9ae1b/32.png) [@Ivaylo\_Bratoev](https://forum.ansible.com/u/Ivaylo_Bratoev)\
**Post date:** [October 6, 2014, 1:18pm UTC](https://forum.ansible.com/t/global-variables-file/16346/11 "2014-10-06T13:18:56Z")

</div>

Alternatively, we can make variables for the _all_ group to be _truly_ global and not introduce any new variables sources 😉

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

---

<div class="post-metadata">

**Author:** ![Strahinja\_Kustudic1](https://avatars.discourse-cdn.com/v4/letter/s/cc9497/32.png) [@Strahinja\_Kustudic1](https://forum.ansible.com/u/Strahinja_Kustudic1)\
**Post date:** [October 6, 2014, 2:17pm UTC](https://forum.ansible.com/t/global-variables-file/16346/12 "2014-10-06T14:17:20Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![Michael\_DeHaan1](https://avatars.discourse-cdn.com/v4/letter/m/5e9695/32.png) [@Michael\_DeHaan1](https://forum.ansible.com/u/Michael_DeHaan1)\
**Post date:** [October 6, 2014, 8:32pm UTC](https://forum.ansible.com/t/global-variables-file/16346/13 "2014-10-06T20:32:31Z")

</div>

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

as such, we will not be making any changes.
