Introduction

Ansible is an open-source automation tool used for configuration management, application deployment, and task automation. It is known for its simplicity and ease of use, leveraging SSH for communication and YAML for configuration files.

Key Concepts

  • Playbooks: YAML files that define a series of tasks to be executed on remote hosts.
  • Inventory: A list of hosts or groups of hosts where Ansible will run tasks.
  • Modules: Reusable, standalone scripts that Ansible runs on your behalf.
  • Roles: A way to group multiple tasks, variables, files, templates, and handlers into a single unit.

Setting Up Ansible

Installation

To install Ansible on a Linux system, you can use the package manager. For example, on Ubuntu:

sudo apt update
sudo apt install ansible -y

Verifying Installation

Check the installed version to verify the installation:

ansible --version

Basic Ansible Configuration

Inventory File

The inventory file lists the hosts and groups of hosts that Ansible will manage. Create a file named hosts:

[webservers]
web1.example.com
web2.example.com

[dbservers]
db1.example.com
db2.example.com

Ansible Configuration File

Ansible's behavior can be customized using the ansible.cfg file. Create a basic configuration file:

[defaults]
inventory = ./hosts
remote_user = your_username

Writing Your First Playbook

Playbook Structure

A playbook is a YAML file that defines one or more plays. Each play specifies a set of tasks to be executed on a group of hosts.

---
- name: Ensure Apache is installed
  hosts: webservers
  become: yes

  tasks:
    - name: Install Apache
      apt:
        name: apache2
        state: present

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

Running the Playbook

To run the playbook, use the ansible-playbook command:

ansible-playbook playbook.yml

Advanced Playbook Features

Variables

Variables can be defined in the playbook or in separate files. They help in making the playbook more dynamic.

---
- name: Install and configure Apache
  hosts: webservers
  become: yes
  vars:
    apache_package: apache2

  tasks:
    - name: Install Apache
      apt:
        name: "{{ apache_package }}"
        state: present

Handlers

Handlers are tasks that are triggered by other tasks. They are useful for actions like restarting a service after a configuration change.

---
- name: Install and configure Apache
  hosts: webservers
  become: yes

  tasks:
    - name: Install Apache
      apt:
        name: apache2
        state: present
      notify:
        - Restart Apache

  handlers:
    - name: Restart Apache
      service:
        name: apache2
        state: restarted

Templates

Templates allow you to create dynamic configuration files using Jinja2 templating language.

Create a template file index.html.j2:

<html>
  <head>
    <title>Welcome to {{ ansible_hostname }}</title>
  </head>
  <body>
    <h1>Hello from {{ ansible_hostname }}</h1>
  </body>
</html>

Use the template in your playbook:

---
- name: Deploy a custom index.html
  hosts: webservers
  become: yes

  tasks:
    - name: Copy index.html from template
      template:
        src: index.html.j2
        dest: /var/www/html/index.html

Practical Exercise

Exercise: Automate Apache Installation and Configuration

  1. Objective: Write an Ansible playbook to install Apache, deploy a custom index.html, and ensure the service is running.
  2. Steps:
    • Create an inventory file with at least one web server.
    • Write a playbook that:
      • Installs Apache.
      • Deploys a custom index.html using a template.
      • Ensures the Apache service is started.
    • Run the playbook and verify the results.

Solution

Inventory File (hosts):

[webservers]
web1.example.com

Playbook (playbook.yml):

---
- name: Install and configure Apache
  hosts: webservers
  become: yes

  tasks:
    - name: Install Apache
      apt:
        name: apache2
        state: present
      notify:
        - Restart Apache

    - name: Deploy custom index.html
      template:
        src: index.html.j2
        dest: /var/www/html/index.html

  handlers:
    - name: Restart Apache
      service:
        name: apache2
        state: restarted

Template (index.html.j2):

<html>
  <head>
    <title>Welcome to {{ ansible_hostname }}</title>
  </head>
  <body>
    <h1>Hello from {{ ansible_hostname }}</h1>
  </body>
</html>

Run the Playbook:

ansible-playbook playbook.yml

Conclusion

In this section, you learned the basics of Ansible, including how to set it up, write playbooks, and use advanced features like variables, handlers, and templates. Ansible is a powerful tool that can significantly simplify the management and automation of your infrastructure. In the next module, we will delve into more advanced topics such as Linux Kernel Tuning.

© Copyright 2024. All rights reserved