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-galaxy
command to create a role namedwebserver
:ansible-galaxy init ~/day5/roles/webserver
Directory 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: true
Define Default Variables:
Edit
~/day5/roles/webserver/defaults/main.yaml
:--- web_port: 80
Add a Handler:
Edit
~/day5/roles/webserver/handlers/main.yaml
:--- - name: Restart Apache service: name: httpd state: restarted
Create 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: true
Create a Playbook to Use the Role:
Save as
~/day5/webserver_role_playbook.yaml
:--- - name: Deploy Web Server hosts: webservers roles: - role: webserver
Run 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.yaml
to include:--- web_port: 8080
Modify the Tasks to Use the Variable:
Update
~/day5/roles/webserver/tasks/main.yaml
:--- - name: Configure Apache to Listen on Custom Port lineinfile: path: /etc/httpd/conf/httpd.conf regexp: '^Listen ' line: "Listen {{ web_port }}" state: present notify: Restart Apache
Run the Playbook:
ansible-playbook ~/day5/webserver_role_playbook.yaml -e web_port=8080 -i inventory
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
:ansible-galaxy init ~/day5/roles/dbserver
Define Tasks for the Database Server Role:
Edit
~/day5/roles/dbserver/tasks/main.yaml
:--- - name: Install MariaDB yum: name: mariadb-server state: present - name: Start and enable MariaDB service: name: mariadb state: started enabled: true
Modify the Master Playbook:
Save as
~/day5/multi_role_playbook.yaml
:--- - name: Deploy Web Server hosts: webservers roles: - role: webserver - name: Deploy Database Server hosts: dbservers roles: - role: dbserver
Run the Master Playbook:
ansible-playbook ~/day5/multi_role_playbook.yaml -i inventory
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