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.

Group

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.

Linux group
  1. Move the Linux host(s) under the group.
Linux group and hosts
  1. Create a windows group in the inventory.
Windows group
  1. Move the Windows host(s) underneath the group.
Windows group and hosts

Review the hosts file for this section below:

Hosts

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.
Linux group variables
  1. Move the Linux host variables underneath the [linux] group.
Linux host variables
  1. Create group variables for the [windows] group.
Windows group variables
  1. Move the Windows host variables underneath the [windows] group.
Windows host variables

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

Hosts

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.

/
hosts
ping_novars.yml
win_ping_novars.yml
ping_novars_updated.yml
win_ping_novars_updated.yml
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:

Ping Linux hosts

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

Ping Windows hosts

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.

Execute the playbook
  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.

Execute the playbook

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.

Introduction to Inventories
Organize Hosts and Group Variables
Mark as Completed
Report an Issue