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.
[GroupName]host1host2
Create groups in the inventory
Add a Linux and Windows group. Place each host under the appropriate group.
-
Open the
hosts
INI inventory. -
Create a Linux group in the inventory.
[Linux]
- Move the Linux host(s) under the group.
[linux]# Replace the example DNS names with the actual onesvm-linuxweb001.eastus.cloudapp.azure.comec2-54-211-23-17.compute-1.amazonaws.com
- Create a
windows
group in the inventory.
[windows]
- Move the Windows host(s) underneath the group.
[windows]# Replace the example DNS names with the actual onesvm-winweb001.eastus.cloudapp.azure.comec2-3-231-5-122.compute-1.amazonaws.com
Review the hosts
file for this section below:
[linux]vm-linuxweb001.eastus.cloudapp.azure.comec2-54-211-23-17.compute-1.amazonaws.com[windows]vm-winweb001.eastus.cloudapp.azure.comec2-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.
- Create group variables for the
[linux]
group.
[linux:vars]
- Move the Linux host variables underneath the
[linux]
group.
# Replace the password with the actual password[linux:vars]ansible_user=ansibleansible_password=<Password>ansible_ssh_common_args='-o StrictHostKeyChecking=no'
- Create group variables for the
[windows]
group.
[windows:vars]
- Move the Windows host variables underneath the
[windows]
group.
[windows:vars]ansible_user=ansibleansible_password=<Password>ansible_winrm_server_cert_validation=ignoreansible_connection=winrm
By the end of this section, the hosts
should look like the one below:
[linux:vars]ansible_user=ansibleansible_password=<Password>ansible_ssh_common_args='-o StrictHostKeyChecking=no'[linux]vm-linuxweb001.eastus.cloudapp.azure.comec2-54-211-23-17.compute-1.amazonaws.com[windows:vars]ansible_user=ansibleansible_password=<Password>ansible_winrm_server_cert_validation=ignoreansible_connection=winrm[windows]vm-winweb001.eastus.cloudapp.azure.comec2-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
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:
ansible-playbook ping_novars.yml -i hosts --limit linux
Similarly, run the following command to execute the win_ping_novars.yml
playbook:
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.
- Update the
host
pattern 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.
ansible-playbook ping_novars_updated.yml -i hosts
- Update the
host
pattern 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.
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.