Howdy folks,
I was hoping to get some tips on how best to organize variables in
Ansible. Right now, I don't have hard-and-fast rules, but I'm generally
sticking to the following patterns:
* 'group_vars/all' contains any variables which I will need to access
over and over, such as domain-level DNS settings, user information,
Yum server URL, etc.
* Files for individual groups under 'group_vars' contain any variables
which must be accessible for all machines in a group (but not other
machines), and any variables which need to be overridden from
'group_vars/all'
* Files for individual machines under 'host_vars' almost exclusively
contain information related to network device configuration, but
also may occasionally contain overrides of group variables.
* In roles, I use the 'vars/main.yml' for any variables which:
a) I won't need to access from outside the role.
b) I deem more closely associated with the role itself than, say,
the group(s) which will be having that role applied.
* I pretty much tend not to use 'defaults/main.yml', because:
a) I don't want to accidentally have inventory variables override
them because of accidental namespace collisions.
b) I like the explicitness of passing overrides to roles in the
play itself.
Now, this mostly seems to work, but I've noticed a few issues:
* Most of these choices are based on some variant of where I might
need to access the variables, which strikes me as *almost* an
implementation detail, with no real semantic significance.
* 'group_vars/all' has the potential to get _huge_. I could see
hitting thousands of lines before too long. This can be very
annoying and error-prone to edit and browse.
* Deciding whether a variables should be a group variable or a role
variable is a pretty arbitrary process. I can feel my brain trying
to ask what in the hell is the difference whenever I am making the
decision. Part of this is that groups and roles often map 1:1, so
in practice there may be no real distinction between the two.
Now, the middle issue is sort of the clincher. I find myself, because
of this, wanting to put more things into role variables, because then
they are kept near their associates tasks and templates, but I often
cannot because I want to be able to access then later on from hosts
which will not have that role applied.
As a concrete example, I'm currently writing a playbook to set up
a Gitolite installation, and am going to define the needed repositories
in a hash table and then write them out to the configuration file in
a template. This is likely to be quite verbose.
My initial inclination is to put this in
'roles/gitolite/vars/main.yml', because it would be nice and
centralized. I am going, however, to want to use some values from these
when configuring other roles to deploy applications kept here, doing
something like:
- name: Deploy software from git repository.
git: repo=ssh://{{ gitolite_server }}/{{ repo.foobar.path }} ...
So in practice I will need to put the repository definitions in
'group_vars/all' or perhaps in some specific group's variable file and
then access it indirectly via something like:
'{{ hostvars[groups['some_group'][0]]['repos'] }}'
Which would give me a really kludgey kind of namespacing.
I'm feeling as though I'm doing something silly here. So how do other
folks organize variables? Am I thinking about this insane?
Thanks in advance for any help.