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
inventory
in the /home/labex/project
directory with the following content:
- Define groups named
web
and db
- Add the variable
http_port=80
to the web
group
- Add the variable
db_port=3306
to the db
group
- Using
localhost
for simplicity, add it to both the web
and db
groups
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"
}