Introduction
Group variables in Ansible allow defining variables that apply to entire groups of hosts. They provide a way to customize Ansible's behavior for specific host groups and configure common settings. This challenge will teach you how to define and use group variables in your Ansible inventory.
Defining Group Variables
Let's start by learning how to define group variables in your Ansible inventory. This is a fundamental skill that will allow you to efficiently manage configurations across different groups of hosts.
Tasks
Define group variables for specific host groups:
- Create a file named
inventoryin the/home/labex/projectdirectory with the following content:- Define groups named
webanddb - Add the variable
http_port=80to thewebgroup - Add the variable
db_port=3306to thedbgroup - Using
localhostfor simplicity, add it to both thewebanddbgroups
- Define groups named
Requirements
- Ansible is pre-installed
- SSH connectivity to managed hosts is configured
Examples
After completing this step, you should be able to use Ansible's ping module to check the status of hosts in the web group:
ansible web -i inventory -m ping
Example output:
localhost | SUCCESS => {
"changed": false,
"ping": "pong"
}
Accessing Group Variables in Playbooks
Now that we've defined our group variables, let's move on to the next crucial step: accessing these variables in Ansible playbooks. This skill will enable you to create dynamic and flexible playbooks that can adapt to different host groups.
Tasks
Create a new playbook file named
my-playbook.yamlin the/home/labex/projectdirectory with the following content:- Set the
hoststoweb - Use the
debugmodule to access and display thehttp_portgroup variable
- Set the
Run the Ansible playbook using the
ansible-playbookcommand, specifying the inventory file.
Requirements
- Ansible is pre-installed
- SSH connectivity to managed hosts is configured
- Inventory file is created and configured
Examples
After completing this step, running the playbook should produce output similar to:
PLAY [Print group variables] **************************************************
TASK [Gathering Facts] ********************************************************
ok: [localhost]
TASK [Print http_port variable] ************************************************
ok: [localhost] => {
"http_port": 80
}
PLAY RECAP *********************************************************************
localhost : ok=2 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
Summary
Congratulations! You have completed the Ansible Group Variables challenge. You have learned to define group variables in your inventory and access them in playbooks. These skills form a crucial part of efficient Ansible usage, allowing you to manage and customize configurations for different host groups within your infrastructure.
By utilizing group variables effectively, you can create more flexible and modular automation workflows. As you continue your Ansible journey, experiment with different group variables and explore how they can enhance your automation processes. Remember, practice is key to mastering these concepts!


