Day 58: Ansible Playbook💥

Day 58: Ansible Playbook💥

Day#58 Of 90 Days Of DevOps Challenge

·

4 min read

Welcome back to our journey in the world of Ansible!🌈

In the previous days, we've explored the fundamentals of Ansible, including its core concepts and how to manage configurations and automate tasks. Today, we'll dive deeper into Ansible Playbooks – the heart and soul of Ansible automation.

👋Introduction

Ansible Playbooks are essential in the world of automation. These playbooks are YAML files that contain a set of tasks, roles, configurations, and variables to execute on remote servers.

In other words, they're like a chef's recipe book for automating tasks on multiple servers.

Playbooks are incredibly versatile and can be used to perform tasks ranging from creating files on remote servers to configuring complex, multi-server applications. They allow for a structured and organized approach to automation.

Let's walk through some common tasks you can perform with Ansible Playbooks and see the commands in action.

📜Task-01: Basic Playbook Tasks

📌Write an Ansible playbook to create a file on a different server

  - name: Create a file on a different server
    hosts: all
    become: yes
    tasks:
      - name: Create a file
        file:
          path: /home/ubuntu/day58.txt
          state: touch

After saving this playbook, run it using the ansible-playbook command:

ansible-playbook <yml_file>

Ensure that your Ansible inventory is correctly configured to connect to the remote server via SSH.

📌Write an Ansible playbook to create a new user

To create a new user using an Ansible playbook, you can use the user module.

Here's a simple Ansible playbook that creates a new user with the username "newuser".

 - name: Create a new user
    hosts: all
    become: yes
    tasks:
      - name: Add a new user
        user: name=newuser

To execute this playbook, save it to a file (e.g., create_user.yml) and run it with the ansible-playbook command:

ansible-playbook <yml_file>

Make sure to replace your_target_host with the hostname or IP address of the target system where you want to create the new user.

Additionally, ensure that you have SSH access and the necessary privileges on the target host to create a new user.

📌Write an Ansible playbook to install docker on a group of servers

To install Docker on a group of servers using Ansible, you can create a playbook like the one below.

This playbook assumes that you have Ansible installed on your local machine and that you have SSH access to the target servers.

Here's a basic Ansible playbook to install Docker:

  - name: Install Docker on multiple servers
    hosts: all
    become: yes
    tasks:
      - name: Add Docker GPG key
        apt_key:
         url: https://download.docker.com/linux/ubuntu/gpg
         state: present

      - name: Add Docker APT repository
        apt_repository:
         repo: deb https://download.docker.com/linux/ubuntu focal stable
         state: present

      - name: Install Docker
        apt:
          name: docker.io
          state: present
        become: yes
      - name: Start Docker service
        systemd:
          name: docker
          state: started
          enabled: yes

📜Task-02: Write a blog about writing Ansible playbooks with the best practices.

Here are the best practices for writing Ansible playbooks with a different approach and command line examples:

  1. Organize Playbooks Logically

    • Split Playbooks: Divide your playbooks into multiple files based on roles or functions for better organization and reusability.

Command Line Example:

    ansible-playbook site.yml
    ansible-playbook webservers.yml
    ansible-playbook appservers.yml
  • Group Tasks with Blocks: Group related tasks into blocks for improved readability.
  1. Maintain Consistency

    • Consistent Indentation: Use a common indentation style, such as 2 spaces, for consistency.
  2. Define Variables

    • Top-of-Playbook Variables: Define variables at the beginning of the playbook to make it dynamic and reusable.

    • Meaningful Variable Names: Use descriptive variable names for better understanding.

    • Avoid Hardcoding: Refrain from hardcoding values within tasks.

Command Line Example

    ansible-playbook playbook.yml -e "app_name=my_app db_name=my_app_db"
  1. Use Grouping:

    • Categorize Hosts: Group hosts logically into categories like web servers, app servers, etc.

    • Group Variables: Define variables specific to a group using group variables.

Command Line Example

    ansible-playbook playbook.yml -i inventory.ini
  1. Create Roles

    • Modular Playbooks: Create roles for related tasks to encapsulate reusable code.

Command Line Example

    ansible-galaxy init nginx-role
  1. Handle Failures Gracefully

    • Error Handling: Use failed_when:, ignore_errors:, and changed_when: to manage non-critical failures and unexpected changes.

Command Line Example

    ansible-playbook playbook.yml
  1. Add Comments

    • Documentation: Use comments to explain the playbook's purpose and usage, improving readability and maintainability.

Command Line Example

    ansible-playbook playbook.yml --list-tasks

These best practices and tips will help you organize, write, and execute Ansible code more effectively and maintainably.

🌟Conclusion

Ansible Playbooks are your go-to tools for automating tasks and managing configurations across multiple servers.

They make complex automation scenarios more manageable and maintainable. By following best practices and continuously learning, you can become an Ansible playbook pro in no time.

In our next blog post, we will dive even deeper into Ansible's capabilities and explore more advanced topics.

Until then, keep exploring, automating, and simplifying your infrastructure with Ansible!

Happy learning & may your cloud journey be filled with exciting discoveries!🎊

Thank you for joining us on this exciting Day 58 of the 90 Days of DevOps challenge. I hope you found the information helpful and insightful.💥

So please keep yourself updated with my latest insights and articles on DevOps 🚀 by following me on :

Hashnode: vishaltoyou.hashnode.dev

LinkedIn: linkedin.com/in/vishalphadnis

Â