Ansible is a great tool for IT automation. It’s similar to Chef and Puppet, just better!
I wrote here already an introduction to Ansible. In Ansible you have roles and playbooks. A playbook can contain several roles. Inside a role you can have tasks, files, handlers and variables. But sometimes you want to use a variable across multiple roles. Unfortunately Ansible doesn’t have really a concept of global vars, but there is a trick 😉
You can define group variables for ALL groups, that is indeed like a global variable. I manage my Ansible code in a git repo. The file tree looks like this:
The variables defined in “group_vars/all/vars.yml” are accessible from ALL roles!
3 thoughts on “Global Variables with Ansible”
Defining variables under “group_vars/all” should also work.
This is a much more elegant solution to global variables than to use vars_files, which is what I normally see people use.
Good article ,Thank you very much for the great guidence .
This was a lovelyy blog post