Day 58 of 90 Days of DevOps Challenge: Ansible Playbooks

Welcome back to our DevOps journey! Today, we dive deeper into Ansible Playbooks, which are essential for automating configurations, deployments, and task execution across multiple servers.


What is an Ansible Playbook?

Ansible playbooks are a way to define and orchestrate automation tasks. They allow you to group multiple tasks and apply them to multiple machines, making it easier to manage configurations, deploy applications, and enforce desired states across your infrastructure.

Think of them as the instruction manuals that describe the steps required to get your systems running exactly the way you want.


Task-01: Hands-on with Ansible Playbooks

1. Ansible Playbook to Create a File on a Different Server

This playbook will create a file on a remote server:

---
- name: Create a file on the remote server
  hosts: all
  become: yes
  tasks:
    - name: Create a file
      file:
        path: /tmp/myfile.txt
        state: touch
        owner: root
        group: root
        mode: '0644'

This playbook:

  • Touches a file named myfile.txt on the /tmp directory on the remote server.

  • Ensures the file is owned by root, and gives it read/write permissions for the owner.

2. Ansible Playbook to Create a New User

This playbook creates a new user called devuser:

---
- name: Create a new user
  hosts: all
  become: yes
  tasks:
    - name: Add user 'devuser'
      user:
        name: devuser
        state: present
        shell: /bin/bash
        groups: sudo

This playbook:

  • Creates a new user devuser.

  • Adds the user to the sudo group, and sets the default shell to /bin/bash.

3. Ansible Playbook to Install Docker on a Group of Servers

This playbook installs Docker on all servers:

---
- name: Install Docker on all servers
  hosts: all
  become: yes
  tasks:
    - name: Update APT package manager
      apt:
        update_cache: yes
    - name: Install necessary packages
      apt:
        name: "{{ item }}"
        state: present
      with_items:
        - 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 https://download.docker.com/linux/ubuntu focal stable
    - name: Install Docker
      apt:
        name: docker-ce
        state: present
    - name: Start and enable Docker service
      systemd:
        name: docker
        enabled: yes
        state: started

This playbook:

  • Installs Docker on the remote servers.

  • Updates the package manager, adds the Docker GPG key, and installs Docker.

  • Ensures Docker is started and enabled.


Task-02: Best Practices for Writing Ansible Playbooks

When writing Ansible playbooks, it's important to follow best practices to ensure readability, maintainability, and performance. Here are some key tips:

1. Use Roles to Organize Playbooks

Ansible roles allow you to structure your playbooks logically. Use roles to break down tasks into smaller, reusable components. This way, your playbooks will be modular and easier to manage.

Example directory structure:

├── site.yml
├── roles/
│   ├── common/
│   │   ├── tasks/
│   │   │   └── main.yml
│   └── webserver/
│       ├── tasks/
│       │   └── main.yml

2. Use Variables for Flexibility

Use variables to make your playbooks flexible and reusable across different environments.

Example:

- name: Install NGINX
  hosts: all
  vars:
    nginx_package: nginx
  tasks:
    - name: Install NGINX
      apt:
        name: "{{ nginx_package }}"
        state: present

3. Idempotency

Ensure that your playbooks are idempotent, meaning running them multiple times should not change the system unless necessary. Use Ansible modules like file, package, and service to maintain idempotency.

4. Use Handlers for Services

Use handlers to manage services. Handlers are triggered by tasks and ensure that services are restarted only when changes are made.

tasks:
  - name: Update NGINX config
    template:
      src: nginx.conf.j2
      dest: /etc/nginx/nginx.conf
    notify: restart nginx

handlers:
  - name: restart nginx
    service:
      name: nginx
      state: restarted

5. Test Playbooks Before Use

Always test your playbooks on a staging environment before applying them to production.

By following these practices, you'll ensure that your Ansible playbooks are not only effective but also easy to maintain and scale as your infrastructure grows.


Conclusion

Ansible playbooks are powerful tools for orchestrating and automating tasks across multiple servers. Whether you're setting up new users, installing software like Docker, or configuring environments, playbooks make infrastructure management easy and repeatable.

Keep practicing, and you'll master the art of writing efficient Ansible playbooks!