I need to set up quite few host specific variables. Up until now I was adding them to the hosts file but the list of variables is getting ridiculously long.
What I'd like to do is to tell ansible to load a host specific file that will contain all the necessary variables. Let's say my hosts are:
[webservers]
hydrogen
helium
lithium
And that variables for them are structured like this:
.
├── variables
│ ├── hydrogen.yml
│ ├── helium.yml
│ └── lithium.yml
└── blahblah
How can I load those variables for each host automatically?
Ansible has the concept of group and host vars. No need to manually tell it to load vars from a file, it's all built in. Simply create files relative to your playbook (or inventory file) in the folders group_vars
and/or host_vars
:
.
├── group_vars
│ └── webservers.yml
├── host_vars
│ ├── hydrogen.yml
│ ├── helium.yml
│ └── lithium.yml
└── playbook.yml
Also see Best Practices: Group And Host Variables
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With