Ansible - Day 2(Ansible Playbooks & Modules)

Ansible - Day 2(Ansible Playbooks & Modules)

In the last blog post of this series, you have learnt about the fundamentals of Ansible. In this blog, you will explore more about two fundamental components of Ansible - Playbooks and Modules.

To add to it, you will also learn how to implement a real-world Ansible playbook to install and configure MySql Database on an ec2 instance in AWS.

What is a Playbook?

Ansible Playbooks are files written in YAML format, which define a series of tasks and configurations. These tasks can include anything from installing software and updating configurations to restarting services. Playbooks are like a recipe that tells Ansible what to do on each target system, and they can be version-controlled, shared, and reused across different projects.

How do Playbooks Work?

Playbooks consist of a series of "plays," each of which defines a set of tasks to be carried out on specific hosts or groups of hosts. Ansible uses SSH to connect to these hosts and execute the tasks specified in the playbook. Playbooks are highly versatile, as they allow you to define the order in which tasks are executed and even include conditional statements.

Example Playbook:

---
- name: Configure Web Servers
  hosts: webservers
  tasks:
    - name: Install Apache
      apt:
        name: apache2
        state: present

    - name: Start Apache Service
      service:
        name: apache2
        state: started

In this example, the playbook "Configure Web Servers" installs Apache and starts the Apache service on machines belonging to the "webservers" group.

What are Ansible Modules?

Ansible Modules, on the other hand, are small, standalone scripts that Ansible uses to perform specific tasks on target systems. These modules are designed to be idempotent, which means they can be run multiple times without causing unexpected changes. There are numerous built-in modules for common tasks like managing packages, files, users, and services. However, you can also write custom modules if your needs are more specialized.

Exploring Ansible Modules

Ansible comes with a vast library of modules that cover a wide range of tasks. Here are some common modules you might encounter:

  • apt/yum: These modules allow you to manage packages on Linux systems. You can install, update, or remove packages easily.

  • copy/template: These modules let you manage files. You can copy files to remote servers or use templates to generate configuration files dynamically.

  • user/group: These modules help you manage user accounts and groups on target systems.

  • service/systemd: These modules allow you to control services. You can start, stop, restart, or enable/disable services as needed.

  • shell/command: While Ansible promotes idempotent actions, these modules let you execute arbitrary shell commands when necessary.

    To learn more about modules you can refer here

Building Real-World Playbooks

In this example we will learn how to install and configure MySQL on an EC2 instance using Ansible, you can create a playbook that performs the necessary tasks. Below is a step-by-step playbook to achieve this:

Step 1: Prerequisites

Before you begin, ensure that you have the following prerequisites in place:

  1. An AWS EC2 instance with SSH access enabled.

  2. Ansible is installed on your local machine.

Step 2: Create an Ansible Playbook

Create a new Ansible playbook file, let's name it mysql_installation.yml, and add the following content:

---
- name: Install and Configure MySQL on EC2
  hosts: your_ec2_instance
  become: yes
  vars:
    mysql_root_password: your_root_password
  tasks:
    - name: Update APT cache (for Ubuntu) or YUM cache (for CentOS)
      become: yes
      apt:
        update_cache: yes
      when: ansible_os_family == 'Debian'
      tags:
        - update_cache

    - name: Install MySQL Server
      become: yes
      apt:
        name: mysql-server
        state: present
      when: ansible_os_family == 'Debian'
      tags:
        - install_mysql

    - name: Install MySQL Server (for CentOS)
      become: yes
      yum:
        name: mysql-server
        state: present
      when: ansible_os_family == 'RedHat'
      tags:
        - install_mysql

    - name: Start MySQL service
      become: yes
      service:
        name: mysql
        state: started
      tags:
        - start_mysql

    - name: Secure MySQL installation
      mysql_user:
        name: root
        password: "{{ mysql_root_password }}"
        host: localhost
      tags:
        - secure_mysql

Let's break down what's happening in this playbook:

  • We define a play named "Install and Configure MySQL on EC2" that targets your EC2 instance.

  • We set become: yes to execute tasks with elevated privileges using sudo.

  • We define a variable mysql_root_password to specify the root password for MySQL. Be sure to replace your_root_password with your desired password.

The tasks in the playbook:

  1. Update the APT cache for Ubuntu or YUM cache for CentOS, depending on the Linux distribution.

  2. Install MySQL Server using the appropriate package manager for your distribution.

  3. Start the MySQL service.

  4. Secure the MySQL installation by setting the root user's password.

Step 3: Run the Playbook

Save the playbook file and run it using the ansible-playbook command:

ansible-playbook mysql_installation.yml -i "your_ec2_instance_ip," --private-key=/path/to/your/private/key.pem

Replace your_ec2_instance_ip with the IP address of your EC2 instance and /path/to/your/private/key.pem with the path to your SSH private key file.

Ansible will execute the tasks defined in the playbook on your EC2 instance, installing and configuring MySQL. Once the playbook execution is successful, you should have MySQL installed and configured on your EC2 instance.

Well done, You have successfully implemented your first ever real world ansible playbook.