Reputation: 10457
My ansible.cfg does not setup inventory, so I do not use /etc/ansible/hosts to decide inventory.
said Ansible determines variables based on precedence. But to do that, Ansible needs to first find the variable files, and then understand what types the files are, only after that it can determine the precedence right? so my questions are:
1/ if there are multiple sub-folders under the main ansible playbook folder, how does Ansible know where to find the variable files? Or maybe it simply collects all files under the main folder?
2/ How does Ansible know the type of a variable file, i.e. whether they are inventory group_vars/all, playbook group_vars/all, inventory host_vars/*, etc? Note Ansible needs to know the types of the files in order to determined the variable precedence.
I have these questions because I work on a Ansible playbook, its current layout is:
main_playbook.yaml
inventories/dev/rds/group_vars/all.yaml
inventories/dev/rds/group_vars/rds_something.yaml
Although Ansible playbook does not mention path, the Ansible magically knows the variables defined in all the variable files.
If Ansible can magically collect all these files, then how do I design the project so that it will know when to collect which files under different environments? Currently it only has dev environment but I plan to have test environment as well. But I do not know where to put variable files about test environment.
Upvotes: 3
Views: 11573
Reputation: 6326
This is a good question, ansible does not make it's magic clear enough.
1/ if there are multiple sub-folders under the main ansible playbook folder, how does Ansible know where to find the variable files? Or maybe it simply collects all files under the main folder?
In ansible there is no real concept of a main folder. That said, for purposes of variable resolution, you could think about the "main" folder being wherever your inventory/hosts file is.
Ansible always looks for host_vars/
and group_vars/
relative to your inventory file. "relative to your inventory file" means "in the same folder as your inventory file"
So the location of your inventory (hosts) file is important.
So your project layout should look like this for multiple environments, if you want to keep them 100% separate:
project
├── inventories/
│ ├── dev/
│ │ └── rds/
│ │ ├── group_vars/
│ │ │ ├── all.yaml
│ │ │ └── rds_something.yaml
│ │ └── inventory.yml
│ └── test/
│ └── rds/
│ ├── group_vars/
│ │ ├── all.yaml
│ │ └── rds_something.yaml
│ └── inventory.yml
└── main_playbook.yml
2/ How does Ansible know the type of a variable file, i.e. whether they are inventory group_vars/all, playbook group_vars/all, inventory host_vars/*, etc?
Ansible knows based on what folder it is in.
If there is a file named all.yml
in the group_vars
folder, then ansible knows it contains variables that apply to all groups.
If there is a file named rds_something.yml
in the group_vars
folder, then ansible knows it contains variables for all hosts in the group rds_something
.
If there is a file named db01.yml
in the host_vars
folder, then ansible knows it contains variables that apply only to the db01
host. If db01
is not in your inventory file, then ansible just ignores it.
Upvotes: 11