Day 87 of 100 Days : YAML Basics and Creating Ansible Playbooks

Welcome to Day 87 of my 100 Days of DevOps journey! Today, we're diving into YAML (Yet Another Markup Language) and exploring how to use it to create Ansible playbooks for infrastructure automation. Let's break it down step-by-step.


What is YAML?

YAML stands for "Yet Another Markup Language" and is commonly used in configuration files due to its simplicity and readability. It follows a human-readable data format and is heavily used in DevOps tools like Ansible, Kubernetes, and Docker Compose.

Key Features of YAML:

  • Readable: Uses indentation for structure.

  • Data Types: Supports scalars, lists, and dictionaries.

  • Key-Value Pairs: Defines configuration with ease.

Basic YAML Syntax:

  1. Key-Value Pairs:
key: value
  1. Lists:
fruits:
  - apple
  - banana
  - cherry
  1. Dictionaries:
person:
  name: John
  age: 30
  1. Comments:
# This is a comment
key: value

Pro Tip: Indentation in YAML uses spaces, not tabs. Consistency is crucial to avoid parsing errors.


Ansible and YAML

Ansible uses YAML to define playbooks, which are scripts that describe how Ansible should automate tasks.

Structure of an Ansible Playbook:

  • Hosts: The group of machines where tasks will run.

  • Tasks: The actions to perform on the hosts.

  • Modules: The tools to execute specific tasks (e.g., shell, copy, apt).

Sample Ansible Playbook

---
- name: Install and start Apache web server
  hosts: webservers
  become: yes
  tasks:
    - name: Install Apache
      apt:
        name: apache2
        state: present

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

Explanation:

  • ---: Indicates the start of a YAML document.

  • name: Provides a description of the play or task.

  • hosts: Specifies the target machines.

  • become: yes: Elevates privileges to perform tasks as root.


Hands-On: Deploying a Web Server Using Ansible

Step 1: Prerequisites

  • Ansible installed on your control node.

  • SSH access to the target servers.

  • An inventory file specifying your servers.

Step 2: Create an Inventory File

[webservers]
192.168.1.100
192.168.1.101

Step 3: Write the Playbook

Save the following YAML code as deploy_webserver.yml:

---
- name: Deploy Apache Web Server
  hosts: webservers
  become: yes
  tasks:
    - name: Update apt repository cache
      apt:
        update_cache: yes

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

    - name: Ensure Apache is running
      service:
        name: apache2
        state: started

    - name: Copy custom index.html
      copy:
        src: /path/to/index.html
        dest: /var/www/html/index.html
        owner: www-data
        group: www-data
        mode: '0644'

Step 4: Execute the Playbook

Run the playbook using the ansible-playbook command:

ansible-playbook -i inventory deploy_webserver.yml

Practical Exercises

  1. Modify the Playbook:

    • Add tasks to install additional packages like curl or git.

    • Include a step to create a backup of the existing index.html file.

  2. Use Variables in Playbooks: Create a playbook that uses variables for package names and file paths:

     ---
     - name: Deploy Web Server with Variables
       hosts: webservers
       vars:
         web_package: apache2
         html_path: /var/www/html/index.html
       tasks:
         - name: Install Web Server
           apt:
             name: "{{ web_package }}"
             state: present
    
         - name: Ensure Web Server is Running
           service:
             name: "{{ web_package }}"
             state: started
    
  3. Challenge Yourself:

    • Write a playbook to install and configure MySQL.

    • Automate the deployment of a static website hosted on multiple servers.


Interactive Section

To make learning more engaging, answer these questions:

  1. What will happen if you miss the --- at the start of a YAML document?

  2. What is the purpose of the become: yes directive in Ansible playbooks?

  3. How would you handle errors in an Ansible playbook to ensure the script continues executing?

Post your answers in the comments or try them out in your own Ansible environment!


Closing Thoughts

YAML is a cornerstone of configuration in DevOps, and Ansible playbooks offer a powerful way to automate infrastructure tasks. Mastering these tools will make you a more efficient and capable DevOps engineer.

Stay tuned for Day 88 as we continue to unlock more DevOps secrets. Feel free to share your thoughts or ask questions in the comments section. Let’s keep learning together!