Day 57 & 58 Ansible Playbooks

Day 57 & 58 Ansible Playbooks

Day 57 & 58 of #90daysofdevops

Hey Techies! Welcome to this blog

In this blog, we are going to start with Ansible Playbooks.

Prerequisites

Before diving into Ansible automation, ensure that you have Ansible installed on your control machine. You can follow the installation instructions on this Blog.

Ansible Playbook

Playbooks are the files where Ansible code is written. Playbooks are written in YAML format. YAML stands for Yet Another Markup Language. Playbooks are one of the core features of Ansible and tell Ansible what to execute. They are like a to-do list for Ansible that contains a list of tasks.

Playbooks contain the steps which the user wants to execute on a particular machine. Playbooks are run sequentially. Playbooks are the building blocks for all the use cases of Ansible.

Task 1: Creating a File on a Different Server

---
- name: Create a file on a different server
  hosts: all
  become: true
  tasks:
    - name: Ensure the file exists
      file:
        path: /home/ubuntu/file.txt
        state: touch

Explanation

This playbook is designed to run on all servers specified in the inventory file. The become: true directive is used to execute tasks with elevated privileges. The single task, defined under tasks, utilizes the file module to ensure the existence of the specified file.

Let's check at one server

Task 2: Creating a New User

Ansible Playbook

---
- name: Create a new user
  hosts: all
  become: true
  tasks:
    - name: Create a user with the name ansible-user
      user:
        name: ansible-user

Explanation

This playbook targets all hosts (all) and utilizes the user module to create a new user named "ansible-user." The become: true directive is included to execute the task with elevated privileges.

Task 3: Installing Docker on a Group of Servers

Ansible Playbook

---
- name: Install Docker
  hosts: host_1 ansible_host
  become: true
  tasks:
    - name: Update apt cache
      apt:
        update_cache: yes
    - name: Install Docker
      apt:
        name: docker.io
        state: present

Explanation

This playbook targets a group of servers (docker_servers) to install Docker. The tasks involve updating the APT cache and installing the docker.io package using the apt module. As before, become: true is used to execute tasks with elevated privileges.

Let's check

Ansible Playbook Best Practices

  1. Descriptive Names: Use meaningful names for your playbooks and tasks to enhance readability.

  2. Target Specific Hosts: Clearly specify the target hosts using the hosts directive to avoid unintended changes.

  3. Privilege Escalation: Use become: true judiciously for tasks requiring elevated privileges.

  4. Documentation: Include comments and documentation within your playbooks to explain the purpose of each task.

  5. Modularity: Break down playbooks into smaller, modular files for better organization and reuse.

  6. Idempotence: Design tasks to be idempotent, ensuring that running them multiple times produces the same result.

  7. Variables: Utilize variables to make playbooks flexible and easily adaptable to different environments.

  8. Error Handling: Implement error handling and appropriate fail conditions for robust playbooks.

  9. Testing: Test playbooks in a safe environment before applying them to production.

  10. Source Control: Use version control systems like Git to manage and track changes in your Ansible code.

By following these best practices, you'll create maintainable, reliable, and efficient Ansible playbooks for automating your infrastructure.

Thank you so much for taking the time to read till the end! Hope you found this blog informative and helpful.

Feel free to explore more of my content, and don't hesitate to reach out if need any assistance from me or in case of you have any questions.

Happy Learning!

~kritika :)

Connect with me: LinkedIn