Use Groups
Use groups to bundle hosts by environment, location, and functionality.
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.
Create groups in the inventory#
Add a Linux and Windows group. Place each host under the appropriate group.
-
Open the
hostsINI inventory. -
Create a Linux group in the inventory.
- Move the Linux host(s) under the group.
- Create a
windowsgroup in the inventory.
- Move the Windows host(s) underneath the group.
Review the hosts file for this section below:
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.
- Create group variables for the
[linux]group.
- Move the Linux host variables underneath the
[linux]group.
- Create group variables for the
[windows]group.
- Move the Windows host variables underneath the
[windows]group.
By the end of this section, the hosts should look like the one below:
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
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:
Similarly, run the following command to execute the win_ping_novars.yml playbook:
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.
- Update the
hostpattern tolinux.
Review the ping_novars_updated.yml playbook in the widget above.
- Run the playbook without a limit.
Notice it will only run against the Linux hosts.
- Update the
hostpattern towindows.
Review the win_ping_novars_updated.yml playbook in the widget above.
- Run the playbook without a limit.
Notice it will only run against the Windows 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.
