Day 58: Ansible Playbooks

Day 58: Ansible Playbooks

·

3 min read

  • Ansible playbooks run multiple tasks, assign roles, and define configurations, deployment steps, and variables. If you’re using multiple servers, Ansible playbooks organize the steps between the assembled machines or servers and get them organized and running in the way the users need them to. Consider playbooks as the equivalent of instruction manuals.

  • Ansible playbooks serve as the backbone of efficient automation, orchestrating tasks, defining configurations, and streamlining deployments across multiple servers. These playbooks act as comprehensive instruction manuals, guiding the execution of various tasks across your infrastructure.

Task-01

Let's delve into some practical playbook examples:

Write an ansible playbook to create a file on a different server

  • Playbook 1: Create a File on a Different Server
# vim create-file-playbook.yml
-------------------------------------------------------------------------------
-
  name: Createing a file using playbook
  hosts: all
  become: yes
  tasks:
    - name: Create a file
      file:
        path: /home/ubuntu/ansible.txt
        state: touch

  • Execute your create-file-playbook.yml use this command
ansible-playbook -v create-file-playbook.yml

  • Navigate to your servers 1, 2, and 3, and you'll find that the ansible.txt file has been created on each server.

  • Playbook 2: Create a New User
# vim create-user-playbook.yml
-------------------------------------------------------------------------------
-
  name: This play book will create a user
  hosts: all
  become: yes
  tasks:
    - name: To create a username devopsparthu
      user: name=devopsparthu

  • Execute your create-user-playbook.yml use this command
ansible-playbook -v create-user-playbook.yml

  • Navigate to your servers 1, 2, and 3, and you'll find that the devopsparthu user has been created on each server.
cat /etc/passwd

  • Playbook 3: Install Docker on a Group of Servers
# vim install-docker-playbook.yml
-------------------------------------------------------------------------------
-
  name: This playbook will install docker
  hosts: all
  become: yes
  tasks:
    - name: Added Docker GPG apt key
      apt_key:
        url: https://download.docker.com/linux/ubuntu/gpg
        state: present

    - name: Added Docker Repository
      apt_repository:
        repo: deb https://download.docker.com/linux/ubuntu focal stable
        state: present

    - name: Install Docker
      apt:
        name: docker-ce
        state: latest

  • Execute your install-docker-playbook.yml use this command
ansible-playbook -v install-docker-playbook.yml

  • Navigate to your servers 1, 2, and 3, and use this command that you docker is installed or not
systemctl status docker

Task-02

Best Practices for Writing Ansible Playbooks

  • When crafting Ansible playbooks, adhering to best practices ensures maintainability, reliability, and scalability:
1. Modularity and Reusability:
  • Break down playbooks into reusable roles and tasks, enhancing readability and facilitating easier management.
2. Clear Structure and Comments:
  • Use meaningful names for tasks and roles.

  • Add comments to explain playbook functionality, aiding understanding for others and future reference.

3. Parameterization and Variables:
  • Utilize variables for flexibility and ease of configuration changes across different environments.
4. Error Handling and Idempotence:
  • Implement error handling mechanisms to manage failures gracefully.

  • Ensure tasks are idempotent, allowing repeated execution without altering the final state.

5. Version Control and Testing:
  • Store playbooks in version control for tracking changes and collaboration.

  • Validate playbooks in a test environment before deploying to production.

6. Security Considerations:
  • Follow security best practices when dealing with sensitive information like credentials or keys.

  • Limit access to privileged tasks and systems.

By adopting these practices, you elevate the effectiveness and reliability of your Ansible playbooks, enabling smoother automation and better infrastructure management.


Happy Learning

Thanks For Reading! :)

-SriParthu💝💥