Day 62 of 90 Days of DevOps Challenge: YAML Essentials & Writing Ansible Playbooks

Vaishnavi DVaishnavi D
3 min read

Yesterday, I explored Ansible Ad-Hoc Commands and the basics of Ansible Playbooks.
I learned how ad-hoc commands are useful for one-off tasks like checking connectivity, installing packages, restarting services, etc.
Additionally, I gained an understanding of how Ansible Playbooks provide a structured, repeatable way to automate configurations, deployments, and orchestrations across infrastructure.

Today’s goal was to deepen my understanding of YAML syntax, which serves as the backbone for writing Ansible playbooks. Alongside mastering the structure and formatting of YAML, I practiced creating practical playbooks to automate a variety of tasks, including pinging managed nodes, creating files, copying content into files, and installing and configuring services like Apache (httpd).

Understanding YAML (Yet Another Markup Language)

YAML is a human-readable data serialization language used extensively in configuration files across tools like Ansible, Kubernetes, Docker Compose, etc.

Key YAML Characteristics:

  • Human & machine-readable

  • Extension: .yml or .yaml

  • Indentation-sensitive: Proper indentation (spaces, not tabs) is critical

  • Represents data as key-value pairs, lists, and nested structures

Sample YAML Structures

Basic Key-Value Example

---
id: 101
name: Meena
gender: Female
hobbies:
  - music
  - chess
  - cricket
...

Nested Structures

---
person:
  id: 101
  name: Meena
  address:
    city: hyd
    state: TG
    country: India
  hobbies:
    - cricket
    - chess
    - music
...

Example: Representing Employee, Company, and Job Details

---
emp:
  id: 101
  name: Meena
  company:
    name: Microsoft
  job:
    exp: 11 Years
    type: permanent
...

Writing Ansible Playbooks

Core Sections of an Ansible Playbook

  1. Hosts Section: Target machines on which tasks will run

  2. Variables Section: Optional section to define reusable variables

  3. Tasks Section: Sequence of steps or operations to perform

Basic Playbook to Ping Managed Nodes

---
- hosts: all
  tasks:
    - name: ping all managed nodes
      ping:
...

Playbook to Create a File

---
- hosts: all
  tasks:
    - name: create a file
      file:
        path: /home/ansible/myfile.txt
        state: touch
...

Playbook to Copy Content to a File

---
- hosts: all
  tasks:
    - name: copy content to file
      copy:
        content: "Hello.. I'm Meena \n"
        dest: /home/ansible/myfile.txt
...

Playbook for Installing and Configuring Apache (httpd)

---
- hosts: webservers
  become: true
  tasks:
    - name: install httpd package
      yum:
        name: httpd
        state: latest

    - name: copy index.html file
      copy:
        src: index.html
        dest: /var/www/html/index.html

    - name: start httpd service
      service:
        name: httpd
        state: started
...

Running Playbooks & Useful Options

  • Syntax Check:

      ansible-playbook playbook.yml --syntax-check
    
  • List Hosts Affected:

      ansible-playbook playbook.yml --list-hosts
    
  • Step-By-Step Execution:

      ansible-playbook playbook.yml --step
    
  • Verbose Output:

      ansible-playbook playbook.yml -vvv
    

Final Thoughts

Today’s session was focused on mastering YAML and practicing Ansible playbooks to automate tasks like file creation, content copying, and service installation. With this, I can now write structured, idempotent playbooks to automate tasks reliably across multiple servers. Up Next, I’ll explore variables, conditionals, loops, and handlers in Ansible to build more dynamic and event-driven automation workflows.

Stay tuned as I advance further into automation with Ansible!

0
Subscribe to my newsletter

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

Written by

Vaishnavi D
Vaishnavi D