☑️Day 59: Learning Ansible Playbooks🚀

🔹Table of Contents :

  • Introduction

  • Writing a Playbook

  • Real-Time Scenarios for Playbooks

  • Hands-On Practice Tasks

    • Task 1: Display Date on Remote Servers

    • Task 2: Install NGINX on a Server

    • Task 3: Set Up a Python Environment

  • Benefits of Using Ansible Playbooks


Today, I delved into creating Ansible Playbooks, a powerful tool to automate tasks on remote servers. Here’s everything I learned about playbooks, why they’re essential, and a step-by-step guide with tasks to get hands-on practice.


What is an Ansible Playbook?

  • Definition: An Ansible Playbook is a YAML file where we define a series of tasks for Ansible to execute on remote servers.

  • Purpose: Playbooks allow us to automate repetitive, complex tasks efficiently and consistently across multiple environments.

  • Why We Need It: Playbooks make configurations and deployments standardized, easy to reproduce, and simple to maintain.


Key Elements of Playbooks

  • Hosts: Specifies the target servers for task execution.

  • Tasks: Defines actions for Ansible to perform, such as installing packages, copying files, or running commands.

  • Handlers: Only triggered when specific conditions (like a configuration change) occur.

  • Variables: Define values (e.g., package names) to make playbooks more adaptable.


How to Write a Playbook

Playbooks are written in YAML format, making them straightforward and human-readable. Here’s how to create one step-by-step:

  1. Define Hosts:

    • Start by mentioning the target servers or groups.

    • Example: hosts: all (targets all servers in the inventory file).

  2. Add Tasks:

    • Use tasks to define the actions for Ansible to perform.

    • Example: name: Display current date.

  3. Use Handlers (Optional):

    • Add handlers to trigger actions like restarting services.

Real-Time Scenarios

  1. Configuration Management: Updating packages, setting up configuration files.

  2. Application Deployment: Installing necessary software and setting up environments.

  3. Continuous Integration: Setting up build environments or creating base configurations.


Hands-On Practice Tasks

Task 1: Display the Date on Remote Servers

Objective: Create a playbook to display the current date on remote servers.

Playbook:

yamlCopy code- name: Display Date on Remote Servers
  hosts: all
  tasks:
    - name: Show current date
      command: date

Commands:

  1. Create the playbook:

     nano display_date.yml
    
  2. Run the playbook:

     ansible-playbook -i inventory display_date.yml
    

Real-Time Use: This can be part of monitoring scripts to check the server’s time configuration across multiple servers.


Task 2: Install NGINX on the Server

Objective: Set up NGINX on a server for web applications.

Playbook:

- name: Install NGINX
  hosts: web_servers
  tasks:
    - name: Update apt repositories
      apt:
        update_cache: yes

    - name: Install NGINX
      apt:
        name: nginx
        state: present

    - name: Start NGINX service
      service:
        name: nginx
        state: started

Commands:

  1. Create the playbook:

     nano install_nginx.yml
    
  2. Run the playbook:

     ansible-playbook -i inventory install_nginx.yml
    

Real-Time Use: Automate the setup of web servers for application deployments, saving time during scaling or setting up new environments.


Task 3: A Common DevOps Playbook – Setting Up a Python Environment

Objective: Install Python and necessary packages on the server.

Playbook:

- name: Setup Python Environment
  hosts: app_servers
  tasks:
    - name: Install Python
      apt:
        name: python3
        state: present

    - name: Install pip for Python 3
      apt:
        name: python3-pip
        state: present

    - name: Install Flask for app development
      pip:
        name: flask

Commands:

  1. Create the playbook:

     nano setup_python_env.yml
    
  2. Run the playbook:

     ansible-playbook -i inventory setup_python_env.yml
    

Real-Time Use: Automates the environment setup for application developers, allowing them to have consistent versions of Python and Flask across all environments.


Key Benefits of Using Ansible Playbooks

  • Standardization: Ensures configurations and software are uniform across servers.

  • Scalability: Easily repeat tasks for hundreds of servers with minimal changes.

  • Time-Saving: Automates repetitive tasks, allowing more focus on development and problem-solving.

Ansible Playbooks are invaluable tools in DevOps, making server management and deployment consistent and efficient. Whether installing packages, configuring applications, or setting up environments, playbooks enable streamlined and standardized processes across the infrastructure.


Stay tuned for more hands-on tasks and in-depth learning!

🚀Thanks for joining me on Day 59! Let’s keep learning and growing together!

Happy Learning! 😊

#90DaysOfDevOps

10
Subscribe to my newsletter

Read articles from Kedar Pattanshetti directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Kedar Pattanshetti
Kedar Pattanshetti