Organize Hosts and Group Variables

Organize hosts and group variables to keep inventories clean and manageable.

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:

  1. Created the group_vars directory.
Create a directory
  1. Created the linux.yml group variable file.
Create a file
  1. Copied the [linux:vars] variables to the group_vars/linux.yml group variable file.
group_vars/linux.yml
  1. Created windows.yml group variable file.
Create a file
  1. Copied the [windows:vars] variables to the group_vars/windows.yml group variables file.
group_vars/windows.yml
  1. Removed the group variables from the hosts file.

Replace the <Password> with your passwords in the group_vars files.

hosts file
We will use the environment variables and set the contents of the hosts file. Make sure to review the file by using the cat command. You can imitate the hosts file 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.

This code requires the following environment variables to execute:
AWS_Linux_EC2_Instance_DNS_Name
Not Specified...
AWS_Windows_EC2_Instance_DNS_Name
Not Specified...
Azure_Linux_VM_Instance_DNS_Name
<Linux>
Azure_Windows_VM_Instance_DNS_Name
<Windows>
/
win_ping_novars.yml
ping_novars.yml
hosts
group_vars
windows.yml
linux.yml
Group Vars

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.

  1. Run the ping playbooks.

Click on the Run button, wait for the environment to set up, and execute the following commands in the terminal:

Execute the ping playbooks

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:

  1. Change into the host_vars directory.
Change directory
  1. Create the Linux host file. Use the fully qualified domain name.
Create the Linux host file
  1. Create a variable called ip and provide the public IP address as the value.
Add content to the file

Replace <Public IP address> with your public IP address.

  1. Create the Windows host file.
Create the Windows host file
  1. Create a variable called ip and provide the public IP address as the value.
Add content to the file
  1. Change back into the parent directory.
Change directory
  1. Use the ls -R or tree command to view the directory structure.
directory structure
  1. Verify the ip host variable is loading.
ad-hoc command with the debug module
ansible-inventory command --list

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
The ansible-inventory command also graphs your inventory by using the --graph option.

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:

Commands
This code requires the following environment variables to execute:
AWS_Linux_EC2_Instance_DNS_Name
Not Specified...
AWS_Windows_EC2_Instance_DNS_Name
Not Specified...
Azure_Linux_VM_Instance_DNS_Name
<Linux>
Azure_Windows_VM_Instance_DNS_Name
<Windows>
/
win_ping_novars.yml
ping_novars.yml
hosts
group_vars
windows.yml
linux.yml
host_vars
Host Vars

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.

Use Groups
Secure Secrets with Ansible Vault: Use Encrypted Files
Mark as Completed
Report an Issue