Create an Ansible static inventory file in YAML format that categorizes two servers into webserver and db groups and define some example group variables for these groups.
lab02
inside ansible-labs
lab02
folderansible.cfg
file from lab01
folder to lab02
folderCreate a folder named inventory
inside lab02
folder.
Inside the inventory
folder, create a file named inventory.yml
.
Add the following content to the file:
webserver:
hosts:
servidor-0:
ansible_host: 10.0.3.100
db:
hosts:
servidor-1:
ansible_host: 10.0.3.101
Test your inventory file using the ansible-inventory
command:
ansible-inventory -i inventory/inventory.yml --list
This will display your inventory in a JSON format, showing the groups, hosts, and variables.
To test, run a simple Ansible command like ping:
ansible -i inventory/inventory.yml all -m ping
This will ping both servers in your inventory. You should see output similar to the following:
servidor-0 | SUCCESS => {
"changed": false,
"ping": "pong"
}
servidor-1 | SUCCESS => {
"changed": false,
"ping": "pong"
}
Now let’s try to run a command on a specific group. Run the following command:
ansible -i inventory/inventory.yml webserver -m command -a "hostname"
You should see output similar to the following:
servidor-0 | CHANGED | rc=0 >>
servidor-0
Now let’s try to run a command on the other group. Run the following command:
ansible -i inventory/inventory.yml db -m command -a "hostname"
You should see output similar to the following:
servidor-1 | CHANGED | rc=0 >>
servidor-1
Finally let’s try to run on a specific host. Run the following command:
ansible -i inventory/inventory.yml servidor-0 -m command -a "hostname"
Create one directory named group_vars
inside inventory
folder.
Inside the group_vars
folder, create two files named webserver.yml
and db.yml
.
Add the following content to the webserver.yml
file:
http_port: 80
max_clients: 200
Add the following content to the db.yml
file:
db_port: 3306
db_name: mydatabase
Test your inventory file using the ansible-inventory
command:
ansible-inventory -i inventory/inventory.yml --list
Now run an ansible command to check the variables on webserver
group:
ansible -i inventory/inventory.yml webserver -m ansible.builtin.debug -a "var=http_port"
You should see output similar to the following:
servidor-0 | SUCCESS => {
"http_port": 80
}
Now run an ansible command to check the variables on db
group:
ansible -i inventory/inventory.yml db -m ansible.builtin.debug -a "var=db_port"
By completing this lab, you will have created a static YAML inventory file for Ansible, categorized two servers into different groups, and defined custom variables for each group. This setup is fundamental for organizing your managed nodes and configuring them based on their roles in your infrastructure.