Ansible Roles: Day 4 of Ansible Zero to Hero Series

Jun 27, 2024

Ansible Roles: Day 4 of Ansible Zero to Hero Series

Introduction

  • Episode 4 of Ansible Zero to Hero series
  • Recap of Episodes 1-3
    • Episode 1: Introduction to Ansible and getting started
    • Episode 2: Ansible ad-hoc commands
      • Passwordless authentication
      • Using the inventory file
      • Example ad-hoc commands
    • Episode 3: Writing the first Ansible Playbook
      • Basics of YAML
      • Ansible Playbook structure
      • Ansible plays, modules, and tasks
      • Installed Apache and deployed a web application using the Playbook

Focus of Day 4: Ansible Roles

  • Objective: Learn basics of Ansible roles:
    • Understand what Ansible roles are
    • Why use Ansible roles
    • Folder structure of Ansible roles
    • Compare roles with Playbooks
    • Create a sample role
  • Future Plans:
    • Day 5: Deep dive into Ansible roles
      • Shared roles across organizations
      • Writing complicated roles

Recap of Day 3: Ansible Playbook Structure

  • Ansible Playbook is a YAML file
  • Collection of plays within a Playbook
    • Each play has fields like host, variables, remote user, tasks
  • Tasks: Collection of actions for target servers
    • Provide a module in each task
  • Workflow:
    1. DevOps engineer writes Playbook
    2. Playbook fed as input to control node
    3. Control node processes the tasks and executes them on managed nodes

Today's Topic: Ansible Roles

  • Introduction to Ansible Roles:
    • Simplifies managing complex Playbooks
    • Improve readability and modularity
    • Roles break down Playbooks into several directories and files
    • Easier to manage Playbooks with many tasks and variables
  • Why use Ansible Roles?:
    • Improves readability
    • Enhances modularity
    • Easier to understand and troubleshoot
    • Can be shared across teams and organizations
    • Use cases: Complicated tasks like Kubernetes installation, database configuration
  • Creating Ansible Roles:
    • Command: ansible-galaxy role init <role_name>
    • Role created with necessary folder structure
    • Splits tasks and other sections into respective directories
  • Folder Structure of Ansible Roles:
    • vars: Contains variables
    • tasks: Contains tasks
    • meta: Metadata about the role
    • handlers: Tasks executed upon specific events
    • files: Static files needed for tasks
    • templates: Dynamic files using Jinja2 for variable replacement
    • defaults: Default values for variables

Practical Example: Converting Playbook to Role

  • Steps: Convert Day 3 Playbook into a Role
    • Use ansible-galaxy role init httpd to create role
    • Move tasks, files, and other parts into the respective folders
    • Adjust file paths in tasks to point to new directory structure
  • Advantages of Roles:
    • Reduces complexity
    • Modularizes tasks
    • Improves code sharing and reuse

Execution of Role

  • Run ansible-playbook -i inventory.ini first_playbook.yaml
  • Results after conversion:
    • GATHERING FACTS, install Apache, copy files, no changes detected (idempotency)
    • Tasks run successfully upon manual file removal, verifying role works as expected

Summary

  • Ansible roles improve readability, modularity, and code sharing
  • Next episode will cover more advanced topics related to roles and Ansible Galaxy

Conclusion

  • Thank you for watching, hope you found it useful
  • Stay tuned for the next episode