Organize Hosts and Group Variables
Organize hosts and group variables to keep inventories clean and manageable.
We'll cover the following
Storing variables in the inventory works well when it is small. However, as more hosts and variables are added, it quickly becomes cluttered. Storing host and group variables in separate files helps you to keep the inventory clean and organize the variables.
Ansible allows you to define your variables in separate files. It searches for these files under a directory called group_vars relative to the inventory or playbook file. Within the group_vars directory, you place files with names that correlate to the group names or hostnames listed in the inventory.
Host and group variable files must use YAML syntax. A file extension for these files is optional, but valid file extensions include .yml, .yaml, and .json.
Group variables files#
We have created a group_vars directory and variable files for the linux and windows groups.
We have performed the following steps:
- Created the
group_varsdirectory.
- Created the
linux.ymlgroup variable file.
- Copied the
[linux:vars]variables to thegroup_vars/linux.ymlgroup variable file.
- Created
windows.ymlgroup variable file.
- Copied the
[windows:vars]variables to thegroup_vars/windows.ymlgroup variables file.
- Removed the group variables from the
hostsfile.
Replace the <Password> with your passwords in the group_vars files.
hostsfile
We will use the environment variables and set the contents of thehostsfile. Make sure to review the file by using thecatcommand. You can imitate thehostsfile for your local projects.
Populate the environment variables with DNS names of the hosts deployed to the provider of your choice and leave the rest as they are.
/
- windows.yml
Warning
You might come across warnings like “AWS Windows EC2 Instance not provided”, indicating that the environment variables were not set.
Make sure to provide the environment variables for the instances and cloud providers of your choice.
- Run the ping playbooks.
Click on the Run button, wait for the environment to set up, and execute the following commands in the terminal:
Host variable files#
Some variables are not shared. Certain variables only pertain to a certain host. That’s where host vars comes in. The directory host_vars is used to store variable files that pertain to a specific host. The file names correlate to the hostname entry in the hosts file.
We have already created the host_vars directory and host variable files to store IP address information about each host.
Make use of the terminal to perform the following steps:
- Change into the
host_varsdirectory.
- Create the Linux host file. Use the fully qualified domain name.
- Create a variable called
ipand provide the public IP address as the value.
Replace <Public IP address> with your public IP address.
- Create the Windows host file.
- Create a variable called
ipand provide the public IP address as the value.
- Change back into the parent directory.
- Use the
ls -Rortreecommand to view the directory structure.
- Verify the
iphost variable is loading.
The inventory command gives you a list of the hosts, along with their variables. It combines all the variables and lists them alongside the host entries.
ansible-inventory command
Theansible-inventorycommand also graphs your inventory by using the--graphoption.
Practice all the above commands in the widget below. Click on the Run button, wait for the environment to set up, and run the commands one by one. Find the summarized view of the commands below:
/
- windows.yml
In this lesson, you organized your host and group variables to keep your inventories clean as the number of variables grows. You created group_vars and host_vars as Ansible searches for the respective directories’ variables.
