Ansible Assignment– 4

Roles & Reusability

Basic Questions

  1. Create a new Ansible Role named webserver.
  2. Generate the default directory structure of a Role using ansible-galaxy init.
  3. List all the directories and files created when initializing a Role.
  4. Create a Role that installs Apache using the package module.
  5. Add a task in the Role to start the Apache service.
  6. Add a task in the Role to enable Apache service on boot.
  7. Create a Role that installs MySQL using the package module.
  8. Add a handler in the Role to restart Apache after configuration changes.
  9. Create a Role that creates a user named deploy.
  10. Use the Role webserver in a Playbook to install Apache on a managed node.
  11. Use the Role database in a Playbook to install MySQL on a managed node.
  12. Create a Playbook that applies both webserver and database Roles.
  13. Create a Role variable for Apache package name and use it in tasks.
  14. Create a Role variable for MySQL package name and use it in tasks.
  15. Add a default variable in the Role and print it using the debug module.
  16. Import a task file into a Role and run it.
  17. Use include_tasks to import a task dynamically inside a Role.
  18. Add a template file in a Role to configure Apache.
  19. Copy a static file using a Role.
  20. Run the Role and verify that all tasks execute successfully.

Intermediate Questions

  1. Create a Role that installs Nginx and deploys a custom index.html.
  2. Add handlers in the Role to restart Nginx when index.html changes.
  3. Create a Role that installs multiple packages using a loop.
  4. Add a Role that creates multiple users using a loop.
  5. Import tasks from another Role into the current Role.
  6. Reuse the webserver Role across two different Playbooks.
  7. Create a Role with group variables for different server groups.
  8. Create a Role with host-specific variables.
  9. Use vars_files inside a Role to include external variable files.
  10. Create a Role that configures Apache Virtual Hosts using templates.
  11. Create a Role that configures MySQL database users using variables.
  12. Use tags to run only specific tasks from a Role.
  13. Run a Playbook and skip tasks from a Role using tags.
  14. Use a Role downloaded from Ansible Galaxy in your Playbook.
  15. Customize a Galaxy Role by overriding default variables.
  16. Create a Role that deploys a simple static web app (HTML files).
  17. Add a handler in the Role to restart Apache only when configuration changes.
  18. Create a Role that installs and configures firewalld service.
  19. Reuse tasks from the common Role in the webserver Role.
  20. Combine multiple Roles (common, webserver, database) in one Playbook.

Advanced Questions

  1. Create a reusable Role that deploys a web application using Apache.
  2. Extend the Role to deploy the same application on Nginx as an alternative.
  3. Add templates, handlers, and variables in the Role for flexible configuration.
  4. Create a Role that deploys a database schema to MySQL.
  5. Import common tasks (like creating directories) into multiple Roles.
  6. Download and use a Galaxy Role to configure a load balancer.
  7. Create a Role that configures SSL certificates for Apache.
  8. Write a Playbook that applies multiple Roles (webserver, database, firewall) for a complete stack deployment.
  9. Create a Role that deploys a multi-tier web app with separate tasks for frontend, backend, and database.
  10. Develop a full reusable Role: Install Apache → Copy application files → Configure Virtual Hosts → Start service → Notify handler → Deploy app.