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.
Press + to interact
mkdir group_vars
  1. Created the linux.yml group variable file.
Press + to interact
touch group_vars/linux.yml
  1. Copied the [linux:vars] variables to the group_vars/linux.yml group variable file.
Press + to interact
---
ansible_user: ansible
ansible_password: <Password>
ansible_ssh_common_args: '-o StrictHostKeyChecking=no'
  1. Created windows.yml group variable file.
Press + to interact
touch group_vars/windows.yml
  1. Copied the [windows:vars] variables to the group_vars/windows.yml group variables file.
Press + to interact
---
ansible_user: ansible
ansible_password: <Password>
ansible_winrm_server_cert_validation: ignore
ansible_connection: winrm
  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.

---
  - hosts: windows
    gather_facts: false
  
    tasks:
    - name: run win_ping
      win_ping:
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:

Press + to interact
# Review the contents of hosts file
cat hosts
# ping
ansible-playbook ping_novars.yml -i hosts
# win ping
ansible-playbook win_ping_novars.yml -i hosts

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.
Press + to interact
cd host_vars
  1. Create the Linux host file. Use the fully qualified domain name.
Press + to interact
# Replace the <LinuxHostDNSname> with the actual domain name
touch <LinuxHostDNSname>.yml
  1. Create a variable called ip and provide the public IP address as the value.
Press + to interact
# Replace the <LinuxHostDNSname> with the actual domain name
# Replace the <Public IP Address> with the actual hosts IP Address
echo "ip: <Public IP address>" > <LinuxHostDNSname>.yml

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

  1. Create the Windows host file.
Press + to interact
# Replace the <WindowsHostDNSname> with the actual domain name
touch <WindowsHostDNSname>.yml
  1. Create a variable called ip and provide the public IP address as the value.
Press + to interact
# Replace the <WindowsHostDNSname> with the actual domain name
# Replace the <Public IP Address> with the actual hosts IP Address
echo "ip: <Public IP address>" > <WindowsHostDNSname>.yml
  1. Change back into the parent directory.
Press + to interact
cd ..
  1. Use the ls -R or tree command to view the directory structure.
Press + to interact
|-- group_vars
| |-- linux
| |-- windows
|-- host_vars
| |-- LinuxHostDNSname.yml
| |-- WindowsHostDNSname.yml
|-- hosts
  1. Verify the ip host variable is loading.
Press + to interact
ansible all -i hosts -m debug -a "var=ip"
Press + to interact
ansible-inventory -i hosts --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:

Press + to interact
# Review the hosts file
cat hosts
# Create the host_vars directory
mkdir host_vars
# Change into host_vars directory
cd host_vars
# Replace the <LinuxHostDNSname> with the actual domain name
touch <LinuxHostDNSname>.yml
# Replace the <LinuxHostDNSname> with the actual domain name
# Replace the <Public IP Address> with the actual hosts IP Address
echo "ip: <Public IP address>" > <LinuxHostDNSname>.yml
# Replace the <WindowsHostDNSname> with the actual domain name
touch <WindowsHostDNSname>.yml
# Replace the <WindowsHostDNSname> with the actual domain name
# Replace the <Public IP Address> with the actual hosts IP Address
echo "ip: <Public IP address>" > <WindowsHostDNSname>.yml
# Change into the parent directory
cd ..
# Directory structure
tree
# ansible all -i hosts -m debug -a "var=ip"
ansible all -i hosts -m debug -a "var=ip"
# List the hosts and the variables
ansible-inventory -i hosts --list
---
  - hosts: windows
    gather_facts: false
  
    tasks:
    - name: run win_ping
      win_ping:
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.

Get hands-on with 1300+ tech skills courses.