DEV Community

Udoh Deborah
Udoh Deborah

Posted on

Day 58 : Ansible Playbooks

Day 58: Ansible Playbooks

Step 1 – What is a Playbook?
• A playbook in Ansible is a YAML file containing instructions (plays) that define tasks to run on remote servers.
• Each play targets a group of hosts and describes what should be done (installing software, creating users, configuring services, etc.).
• Think of it as an instruction manual that Ansible follows across one or many servers.

Step 2 – Example Playbooks

  • Playbook 1: Create a File on a Remote Server
- name: Create a file on remote server
  hosts: all
  become: true
  tasks:
    - name: Create a new file
      file:
        path: /home/ubuntu/devops_day58.txt
        state: touch

Enter fullscreen mode Exit fullscreen mode
  • Playbook 2: Create a New User
- name: Create a new user on servers
  hosts: all
  become: true
  tasks:
    - name: Add user 'devopsuser'
      user:
        name: devopsuser
        state: present
        shell: /bin/bash

Enter fullscreen mode Exit fullscreen mode
  • Playbook 3: Install Docker on Group of Servers
- name: Install Docker on servers
  hosts: docker_nodes
  become: true
  tasks:
    - name: Update apt packages
      apt:
        update_cache: yes

    - name: Install required packages
      apt:
        name: "{{ item }}"
        state: present
      loop:
        - apt-transport-https
        - ca-certificates
        - curl
        - software-properties-common

    - 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 focal stable
        state: present

    - name: Install Docker
      apt:
        name: docker-ce
        state: latest
Enter fullscreen mode Exit fullscreen mode

Step 3 – Best Practices for Ansible Playbooks
• Keep playbooks modular – break down into smaller reusable roles.
• Use variables instead of hardcoding values (usernames, paths, etc.).
• Add comments for clarity.
• Idempotency: ensure running the playbook multiple times won’t break the system.
• Use handlers for restarting services only when changes happen.
• Version control: store playbooks in Git for collaboration.

Automation in DevOps isn’t just about running one-off commands — it’s about consistency, repeatability, and scalability. That’s where Ansible Playbooks shine.

What Are Playbooks?

Playbooks are YAML-based instruction manuals for Ansible. They let you run multiple tasks, organize deployments, and manage configurations across multiple servers. Unlike ad-hoc commands, playbooks are structured, reusable, and scalable.

Examples in Action
• Creating files on remote servers
• Adding new users automatically
• Installing Docker across multiple nodes

These are just small examples of how powerful playbooks can be.

Best Practices
1. Keep playbooks modular by using roles.
2. Use variables for flexibility and reusability.
3. Add comments and clear task names to improve readability.
4. Ensure idempotency so the same playbook can run multiple times safely.
5. Version control your playbooks with Git for better collaboration.

Final Thoughts

With playbooks, Ansible goes beyond quick fixes — it becomes a reliable tool for managing infrastructure at scale. Following best practices ensures your automation is not only powerful but also maintainable.

Top comments (0)