Day 58: Ansible Playbooks 🚀

·

3 min read

Today’s task is all about understanding the power of Ansible Playbooks—the heart of Ansible’s automation. Playbooks allow you to execute tasks sequentially, making configuration management a breeze. Let’s dive into the tasks and explore how to automate some essential operations.


Task-01: Writing Ansible Playbooks

1. Playbook to Create a File on a Different Server

This playbook creates a file named myfile.txt on a remote server.

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

2. Playbook to Create a New User

This playbook creates a new user named devops_user.

---
- name: Create a new user
  hosts: all
  tasks:
    - name: Add user
      ansible.builtin.user:
        name: devops_user
        shell: /bin/bash

3. Playbook to Install Docker on a Group of Servers

This playbook installs Docker on multiple servers.

---
- name: Install Docker on servers
  hosts: all
  become: yes
  tasks:
    - name: Install prerequisites
      ansible.builtin.yum:
        name: "{{ item }}"
        state: present
      with_items:
        - yum-utils
        - device-mapper-persistent-data
        - lvm2

    - name: Add Docker repo
      ansible.builtin.shell: |
        yum-config-manager --add-repo https://download.docker.com/linux/centos/docker-ce.repo

    - name: Install Docker
      ansible.builtin.yum:
        name: docker-ce
        state: present

    - name: Start and enable Docker
      ansible.builtin.service:
        name: docker
        state: started
        enabled: yes

Task-02: Blog on Best Practices for Writing Ansible Playbooks

Title: Best Practices for Writing Ansible Playbooks: Streamlining Your Automation

Introduction
Ansible Playbooks simplify automation, but writing effective and efficient playbooks requires following best practices. Here are some tips to level up your Ansible skills.


1. Use Clear and Meaningful Names
Each play, task, and variable should have descriptive names to make the playbook readable and maintainable.

Example:

- name: Install Apache on web servers
  hosts: web
  tasks:
    - name: Install Apache package
      ansible.builtin.yum:
        name: httpd
        state: present

2. Group Tasks Logically
Organize tasks into roles or separate files to maintain clarity.


3. Use Variables
Avoid hardcoding values. Define variables in group_vars or host_vars for better flexibility.


4. Idempotency is Key
Ensure your playbooks can run multiple times without causing issues. Use Ansible modules that support idempotency like ansible.builtin.file.


5. Test Playbooks Locally First
Use tools like ansible-playbook --check to verify changes before running them on production systems.


6. Notify Handlers for Critical Changes

Use handlers to restart or reload services when configuration changes are made.

Example:

---
- name: Configure Nginx
  hosts: web
  tasks:
    - name: Copy Nginx configuration
      ansible.builtin.copy:
        src: nginx.conf
        dest: /etc/nginx/nginx.conf
      notify: Restart Nginx

handlers:
  - name: Restart Nginx
    ansible.builtin.service:
      name: nginx
      state: restarted

Conclusion
By following these best practices, you can write efficient, reusable, and error-free playbooks. Let’s automate with confidence! 💻✨

Â