12-Day5-3-roles
Day 5: Lab Series 2 - Introduction to Roles and Their Directory Structure
Objective:
This lab introduces participants to Ansible roles and their structured directory organization, enabling efficient and reusable playbook design. Participants will learn to create, organize, and use roles in playbooks.
Lab 2.1: Creating a Basic Role
Scenario:
Set up a web server using an Ansible role.
Steps:
Create the Role Structure:
Use the
ansible-galaxycommand to create a role namedwebserver:ansible-galaxy init ~/day5/roles/webserverDirectory structure will look like this:
~/day5/roles/webserver/ ├── defaults/ ├── files/ ├── handlers/ ├── meta/ ├── tasks/ ├── templates/ ├── tests/ └── vars/Define the Role Tasks:
Edit
~/day5/roles/webserver/tasks/main.yaml:--- - name: Install Apache yum: name: httpd state: present - name: Start and enable Apache service: name: httpd state: started enabled: trueDefine Default Variables:
Edit
~/day5/roles/webserver/defaults/main.yaml:--- web_port: 80Add a Handler:
Edit
~/day5/roles/webserver/handlers/main.yaml:--- - name: Restart Apache service: name: httpd state: restartedCreate a Template:
Save as
~/day5/roles/webserver/templates/index.html.j2:<html> <head> <title>Welcome to {{ inventory_hostname }}</title> </head> <body> <h1>Apache is running on {{ ansible_facts['os_family'] }}!</h1> </body> </html>Modify the Role Tasks to Use the Template:
Update
~/day5/roles/webserver/tasks/main.yaml:--- - name: Install Apache yum: name: httpd state: present - name: Deploy web page template: src: index.html.j2 dest: /var/www/html/index.html mode: '0644' - name: Start and enable Apache service: name: httpd state: started enabled: trueCreate a Playbook to Use the Role:
Save as
~/day5/webserver_role_playbook.yaml:--- - name: Deploy Web Server hosts: webservers roles: - role: webserverRun the Playbook:
ansible-playbook ~/day5/webserver_role_playbook.yaml -i inventory
Lab 2.2: Adding Role Variables
Scenario:
Customize the web server setup by modifying role variables.
Steps:
Update the Default Variable: Edit
~/day5/roles/webserver/defaults/main.yamlto include:Modify the Tasks to Use the Variable:
Update
~/day5/roles/webserver/tasks/main.yaml:Run the Playbook:
Exercise:
Modify the role to support SSL configuration by adding another task and variable for the SSL module.
Lab 2.3: Using Multiple Roles in a Single Playbook
Scenario:
Set up both a web server and a database server using separate roles.
Steps:
Create a Role for the Database Server:
Use
ansible-galaxy init:Define Tasks for the Database Server Role:
Edit
~/day5/roles/dbserver/tasks/main.yaml:Modify the Master Playbook:
Save as
~/day5/multi_role_playbook.yaml:Run the Master Playbook:
Optional Lab: Debugging and Troubleshooting Roles
Scenario:
Test failure scenarios in roles (e.g., missing variables, incorrect templates). Debug and fix the issues.
Remove a required file or variable in the role.
Run the playbook and observe the error.
Add error handling or default values to fix the issue.
Key Learning Points:
Ansible Role Structure: Understand the modular and organized directory structure of roles.
Reusability and Scalability: Roles make it easier to reuse and scale configurations across multiple environments.
Variables in Roles: Customizing roles using
defaults,vars, and command-line overrides (-e).Combining Roles: Use multiple roles in a single playbook for complex setups.
These exercises lay the foundation for using roles effectively in real-world deployments.
Last updated