Ansible Playbooks

Ansible Playbooks

What is Ansible Playbooks:

Ansible Playbooks are at the core of Ansible's configuration management and automation capabilities. They are written in YAML (YAML Ain't Markup Language) format and define a set of tasks, organized into plays and roles, that Ansible should execute on remote systems. Playbooks provide a way to describe desired system states and configurations, enabling consistent and repeatable deployments across infrastructure.

Let's dive into a real-time example of using Ansible Playbooks to deploy and configure a web application across multiple servers in a hypothetical scenario.

Scenario: Deploying a Web Application with Ansible Playbooks

Background: Suppose we have a web application consisting of a frontend served by Nginx and a backend served by Node.js. We want to deploy this application on a fleet of servers running Ubuntu Linux.

Objective: Our goal is to automate the deployment process using Ansible Playbooks, ensuring consistency and reliability across all servers.

Steps:

  1. Inventory Setup: First, we need to define an inventory file that lists all the servers we want to manage with Ansible. We'll create an inventory file named hosts:

     [web_servers]
     server1 ansible_host=192.168.1.101
     server2 ansible_host=192.168.1.102
     server3 ansible_host=192.168.1.103
    
  2. Playbook Structure: We'll create a playbook named deploy_web_app.yml to orchestrate the deployment process. This playbook will consist of plays, each containing tasks for specific roles.

  3. Role Definition: We'll organize our tasks into roles for better modularity and maintainability. We'll create the following roles:

    • common: Configures common settings for all servers.

    • nginx: Installs and configures Nginx.

    • nodejs: Installs Node.js and sets up the backend.

    • web_app: Deploys the web application files.

  4. Writing the Playbook: Now, let's define our playbook deploy_web_app.yml:

     ---
     - name: Deploy Web Application
       hosts: web_servers
       become: true
    
       roles:
         - common
         - nginx
         - nodejs
         - web_app
    
  5. Defining Roles: Each role contains tasks and files specific to its purpose. For example, the nginx role may include tasks to install Nginx and configure its settings.

  6. Running the Playbook: We execute the playbook using the ansible-playbook command:

     ansible-playbook -i hosts deploy_web_app.yml
    

Real-Time Example:

In our scenario, the Ansible Playbook orchestrates the following actions:

  • Common Configuration: Configures common settings like updating package repositories and installing essential packages across all servers.

  • Nginx Setup: Installs Nginx on each server and configures it to serve as a reverse proxy for our web application.

  • Node.js Installation: Ensures Node.js is installed on each server to run the backend of our web application.

  • Web Application Deployment: Copies the web application files to appropriate directories on each server and configures any necessary settings.

Benefits of Using Ansible Playbooks:

  1. Consistency: Ensures consistent configurations across all servers, reducing the risk of configuration drift.

  2. Repeatability: Playbooks can be executed multiple times with the same results, making deployments predictable and reliable.

  3. Modularity: Roles promote code reuse and maintainability by organizing tasks into reusable components.

  4. Automation: Automates the deployment process, saving time and effort compared to manual configuration.

  5. Auditability: Playbooks provide a clear and documented way of defining infrastructure configurations, aiding in auditing and compliance efforts.

  6. Scalability: Easily scale deployments to hundreds or thousands of servers with minimal effort.

Why we need Ansible Playbooks:

Ansible Playbooks are an essential component of Ansible, providing a structured and repeatable way to define and execute tasks for configuration management, application deployment, and automation. Here are several key reasons why Ansible Playbooks are indispensable:

  1. Declarative Configuration Management: Playbooks allow you to declare the desired state of your infrastructure rather than specifying the exact steps needed to achieve that state. This declarative approach simplifies configuration management and abstracts away low-level details, making it easier to manage complex environments.

  2. Infrastructure as Code (IaC): With Ansible Playbooks, infrastructure configurations are written as code, which can be version-controlled, reviewed, and tested just like application code. This promotes collaboration, reproducibility, and consistency across environments.

  3. Repeatability and Consistency: Playbooks enable you to automate repetitive tasks and ensure consistent configurations across all your servers and environments. By defining infrastructure configurations in a playbook, you can repeatedly deploy and update your infrastructure with confidence, knowing that it will be consistent every time.

  4. Modularity and Reusability: Ansible Playbooks encourage modularity by organizing tasks into roles and tasks. Roles can be reused across different playbooks and environments, promoting code reuse and reducing duplication of effort. This modular approach also makes it easier to maintain and update configurations over time.

  5. Orchestration and Workflow Automation: Playbooks allow you to orchestrate complex workflows and dependencies across multiple hosts. You can define dependencies between tasks, execute tasks in parallel or serially, and handle error conditions gracefully. This enables you to automate entire workflows, such as deploying applications, provisioning infrastructure, and performing maintenance tasks.

  6. Idempotent Execution: Ansible Playbooks are idempotent, meaning that running the playbook multiple times produces the same result as running it once. This ensures that you can safely rerun playbooks without causing unintended changes to your infrastructure, even in dynamic or changing environments.

  7. Auditability and Documentation: Playbooks serve as documentation of your infrastructure configurations and deployment processes. By reviewing the playbook, you can gain insights into how your infrastructure is configured and understand the steps involved in deploying and managing it.

  8. Scalability and Manageability: Ansible Playbooks are designed to scale from small environments to large, distributed infrastructures. You can manage thousands of servers and devices using the same playbook syntax, allowing you to scale your automation efforts as your infrastructure grows.

In summary, Ansible Playbooks provide a powerful, flexible, and scalable way to automate infrastructure management and deployment. By leveraging playbooks, organizations can achieve greater efficiency, consistency, and reliability in managing their IT environments.

Prerequisites

Write an ansible playbook to install docker on a group of servers

So we need to install docker service to server1 and server2. so we need to make ansible-playbook to master-server.

docker_install.yml:

- name: "Install and Start Docker Service on Ubuntu"
  hosts: servers
  gather_facts: true
  become: true

  tasks:
    - name: "Add Docker GPG key"
      apt_key:
        url: https://download.docker.com/linux/ubuntu/gpg
        state: present

    - name: "Add Docker repository"
      apt_repository:
        repo: deb [arch=amd64] https://download.docker.com/linux/ubuntu {{ ansible_distribution_release }} stable
        state: present

    - name: "Update apt cache"
      apt:
        update_cache: yes

    - name: "Install Docker"
      apt:
        name: docker-ce
        state: present

    - name: "Start Docker Service"
      service:
        name: docker
        state: started
        enabled: yes

go the master-server service, create a directory to ansible-master and creat a file that is docker_install.yml

to run this file using the command:

ansible-playbook docker_install.yml

For check docker install to server1 and server2, so using adhoc command.

ansible -a "systemctl status docker" servers