When you talk about your infrastructure, you need to categorize it into layers.

Categories

At the highest level, hosts are grouped by the

  • Environment
  • Location

Environment

An environment is a collection of hosts that provide a service for either internal customers or external customers—for example, development, staging, and production.

Location

Location is where the hosts are running physically. Location breaks down into region and region into datacenters.

Your infrastructure dictates how these layers are defined. Environment and location can be used interchangeably.

For example, the environment might be referred to as DC-Prod. You might also leverage all the layers and describe your infrastructure as:

  • US-East-1 (region)
  • US-East-1-DC-1 (datacenter)
  • US-East-1-DC-1-Dev (environment)

Function

Hosts are further categorized by function. The function also has multiple layers. At the top, it describes the general purpose of the host—for example:

  • Web server
  • Database server
  • Cache server

As your infrastructure grows, function further breaks down into specific functions. Refer to a specific web server group as the onboarding web servers or the login portal web servers.

Creating groups and group variables in an inventory

Even within the simple inventory, it is necessary group hosts. After adding the Windows and Linux hosts to the hosts file, you can no longer run the ping playbooks without targeting a specific host.

Grouping the hosts into Linux and Windows solves this problem. Groups are defined within an INI inventory by placing square brackets around the group name and listing the hosts underneath.

Press + to interact
[GroupName]
host1
host2

Create groups in the inventory

Add a Linux and Windows group. Place each host under the appropriate group.

  1. Open the hosts INI inventory.

  2. Create a Linux group in the inventory.

Press + to interact
[Linux]
  1. Move the Linux host(s) under the group.
Press + to interact
[linux]
# Replace the example DNS names with the actual ones
vm-linuxweb001.eastus.cloudapp.azure.com
ec2-54-211-23-17.compute-1.amazonaws.com
  1. Create a windows group in the inventory.
Press + to interact
[windows]
  1. Move the Windows host(s) underneath the group.
Press + to interact
[windows]
# Replace the example DNS names with the actual ones
vm-winweb001.eastus.cloudapp.azure.com
ec2-3-231-5-122.compute-1.amazonaws.com

Review the hosts file for this section below:

Press + to interact
[linux]
vm-linuxweb001.eastus.cloudapp.azure.com
ec2-54-211-23-17.compute-1.amazonaws.com
[windows]
vm-winweb001.eastus.cloudapp.azure.com
ec2-3-231-5-122.compute-1.amazonaws.com

Groups have already helped organize the hosts in the inventory. Group variables do the same by allowing a group to share a standard set of variables.

Create group variables in the inventory

You will add group variables to the inventory and move the host variables into the appropriate groups.

  1. Create group variables for the [linux] group.
Press + to interact
[linux:vars]
  1. Move the Linux host variables underneath the [linux] group.
Press + to interact
# Replace the password with the actual password
[linux:vars]
ansible_user=ansible
ansible_password=<Password>
ansible_ssh_common_args='-o StrictHostKeyChecking=no'
  1. Create group variables for the [windows] group.
Press + to interact
[windows:vars]
  1. Move the Windows host variables underneath the [windows] group.
Press + to interact
[windows:vars]
ansible_user=ansible
ansible_password=<Password>
ansible_winrm_server_cert_validation=ignore
ansible_connection=winrm

By the end of this section, the hosts should look like the one below:

Press + to interact
[linux:vars]
ansible_user=ansible
ansible_password=<Password>
ansible_ssh_common_args='-o StrictHostKeyChecking=no'
[linux]
vm-linuxweb001.eastus.cloudapp.azure.com
ec2-54-211-23-17.compute-1.amazonaws.com
[windows:vars]
ansible_user=ansible
ansible_password=<Password>
ansible_winrm_server_cert_validation=ignore
ansible_connection=winrm
[windows]
vm-winweb001.eastus.cloudapp.azure.com
ec2-3-231-5-122.compute-1.amazonaws.com

Targeting groups with playbooks

Having groups gives you the ability to target the hosts in the groups when running Ansible commands.

We will review and have you run the ping_novars.yml and win_novars.yml playbooks, using the groups with the --limit option first.

Update the <Password> and the DNS names in the hosts file.

[linux:vars]
ansible_user=ansible
ansible_password=<Password> 
ansible_ssh_common_args='-o StrictHostKeyChecking=no'

[linux] 
vm-linuxweb001.eastus.cloudapp.azure.com 
ec2-54-211-23-17.compute-1.amazonaws.com

[windows:vars]
ansible_user=ansible 
ansible_password=<Password> 
ansible_winrm_server_cert_validation=ignore 
ansible_connection=winrm

[windows] 
vm-winweb001.eastus.cloudapp.azure.com 
ec2-3-231-5-122.compute-1.amazonaws.com
Target groups with playbooks

Click on the Run button and wait for the environment to set up. Once set up, Run the ping_novars.yml playbook by executing the following command:

Press + to interact
ansible-playbook ping_novars.yml -i hosts --limit linux

Similarly, run the following command to execute the win_ping_novars.yml playbook:

Press + to interact
ansible-playbook win_ping_novars.yml -i hosts --limit windows

Both of these playbooks are meant to target a specific operating system. Now that there are groups defined, the playbooks can be updated to only target the applicable operating system by using the group.

Let’s update the ping playbooks to target a group.

  1. Update the host pattern to linux.

Review the ping_novars_updated.yml playbook in the widget above.

  1. Run the playbook without a limit.

Notice it will only run against the Linux hosts.

Press + to interact
ansible-playbook ping_novars_updated.yml -i hosts
  1. Update the host pattern to windows.

Review the win_ping_novars_updated.yml playbook in the widget above.

  1. Run the playbook without a limit.

Notice it will only run against the Windows hosts.

Press + to interact
ansible-playbook win_ping_novars_updated.yml -i hosts

In this lesson, we introduced groups, using groups within an Ansible inventory, and targeting different groups in your playbooks.

The best usage of groups is what comes naturally when you speak about your infrastructure. Keep it simple. Every group adds complexity. Ensure that each has a purpose.

Get hands-on with 1300+ tech skills courses.