Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Automatically load host specific variables for ansible

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?

like image 829
SiliconMind Avatar asked Sep 08 '15 17:09

SiliconMind


1 Answers

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

like image 83
udondan Avatar answered Oct 18 '22 13:10

udondan