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_vars
directory.
mkdir group_vars
- Created the
linux.yml
group variable file.
touch group_vars/linux.yml
- Copied the
[linux:vars]
variables to thegroup_vars/linux.yml
group variable file.
---ansible_user: ansibleansible_password: <Password>ansible_ssh_common_args: '-o StrictHostKeyChecking=no'
- Created
windows.yml
group variable file.
touch group_vars/windows.yml
- Copied the
[windows:vars]
variables to thegroup_vars/windows.yml
group variables file.
---ansible_user: ansibleansible_password: <Password>ansible_winrm_server_cert_validation: ignoreansible_connection: winrm
- 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 thehosts
file. Make sure to review the file by using thecat
command. You can imitate thehosts
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:
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:
# Review the contents of hosts filecat hosts# pingansible-playbook ping_novars.yml -i hosts# win pingansible-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:
- Change into the
host_vars
directory.
cd host_vars
- Create the Linux host file. Use the fully qualified domain name.
# Replace the <LinuxHostDNSname> with the actual domain nametouch <LinuxHostDNSname>.yml
- Create a variable called
ip
and provide the public IP address as the value.
# Replace the <LinuxHostDNSname> with the actual domain name# Replace the <Public IP Address> with the actual hosts IP Addressecho "ip: <Public IP address>" > <LinuxHostDNSname>.yml
Replace <Public IP address>
with your public IP address.
- Create the Windows host file.
# Replace the <WindowsHostDNSname> with the actual domain nametouch <WindowsHostDNSname>.yml
- Create a variable called
ip
and provide the public IP address as the value.
# Replace the <WindowsHostDNSname> with the actual domain name# Replace the <Public IP Address> with the actual hosts IP Addressecho "ip: <Public IP address>" > <WindowsHostDNSname>.yml
- Change back into the parent directory.
cd ..
- Use the
ls -R
ortree
command to view the directory structure.
|-- group_vars| |-- linux| |-- windows|-- host_vars| |-- LinuxHostDNSname.yml| |-- WindowsHostDNSname.yml|-- hosts
- Verify the
ip
host variable is loading.
ansible all -i hosts -m debug -a "var=ip"
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
Theansible-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:
# Review the hosts filecat hosts# Create the host_vars directorymkdir host_vars# Change into host_vars directorycd host_vars# Replace the <LinuxHostDNSname> with the actual domain nametouch <LinuxHostDNSname>.yml# Replace the <LinuxHostDNSname> with the actual domain name# Replace the <Public IP Address> with the actual hosts IP Addressecho "ip: <Public IP address>" > <LinuxHostDNSname>.yml# Replace the <WindowsHostDNSname> with the actual domain nametouch <WindowsHostDNSname>.yml# Replace the <WindowsHostDNSname> with the actual domain name# Replace the <Public IP Address> with the actual hosts IP Addressecho "ip: <Public IP address>" > <WindowsHostDNSname>.yml# Change into the parent directorycd ..# Directory structuretree# ansible all -i hosts -m debug -a "var=ip"ansible all -i hosts -m debug -a "var=ip"# List the hosts and the variablesansible-inventory -i hosts --list
--- - hosts: windows gather_facts: false tasks: - name: run win_ping win_ping:
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.