Ansible Playbooks

This is Day 58 of #90daysofdevops challenge

Ansible Playbooks

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.

Task-01:

-> Write an Ansible playbook to create a file on a different server

Before we start writing playbooks please make sure to check this blog for better understanding:

  • Let's create 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

  • Now use this command to run the playbook.
  ansible-playbook create_file.yml -i (Path-of-inventory)

  • Verify that the file has been created at the specified location on a different server.

  • We can also check this by using a simple command given below:
  ansible all -a "ls /home/ubuntu" -i (Path-Of-Inventory)

-> Write an ansible playbook to create a new user.

  • Let's create an ansible playbook to create a new user.
  ---
  - name: Create a new user
    hosts: all
    become: yes
    tasks:
      - name: Add a new user sompandey
        user: name=sompandey

  • Now use this command to run the playbook.
  ansible-playbook create_user.yml -i (Path-of-inventory)

  • To check the created user from the master server use the below command:
  ansible all -a "cat /etc/passwd" -i (Path-Of-Inventory)

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

  • Let's create an Ansible playbook to install docker on a group of servers.
   ---
  - 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

  • Now use this command to run the playbook.
  ansible-playbook install_docker.yml -i (Path-of-inventory)

  • To verify that Docker has been installed on multiple servers use the command given below:
  ansible all -a "docker --version" -i (Path-Of-Inventory)

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

Ansible playbooks are a powerful way to automate server configuration and application deployments. However, poorly written playbooks can quickly become a mess. Here are some best practices to follow when writing Ansible playbooks:

Use a logical structure

  • Split playbooks into multiple files based on roles or functions. This keeps each file focused and reusable.
  site.yml
  webservers.yml
  appservers.yml
  • Group related tasks into blocks for readability.

  • Use consistent indentation (2 spaces is common).

Define variables

  • Define all variables at the top of the playbook. This makes playbooks dynamic and reusable.

  • Use meaningful variable names.

  • Avoid hardcoding values within tasks.

  vars:
    app_name: my_app  
    db_name: my_app_db

Use groups

  • Group your hosts into logical categories like web servers, app servers, etc.

  • Use group variables to define variables specific to a group.

  webservers:
    hosts:
      server1:
      server2:

  appservers:
    hosts:  
      server3:
      server4:

Create roles

  • Create roles for each set of related tasks.

  • Roles encapsulate reusable code and make playbooks modular.

  - name: Configure webservers 
    hosts: webservers  
    roles:
      - { role: nginx }

Handle failure gracefully

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

  • This keeps playbook execution going despite minor issues.

  - name: Install packages  
    apt: name={{item}}   
    with_items: [pkg1, pkg2]
    ignore_errors: yes

Add comments

  • Use comments to document what a playbook does and how to use it.

  • Good commenting improves readability and maintainability.

"Thank you for enjoying my DevOps blog! Your positive response fuels my passion to dive deeper into technology and innovation.

Stay tuned for more captivating DevOps articles, where we'll explore this dynamic field together. Follow me on Hashnode and connect on LinkedIn (https://www.linkedin.com/in/som-shanker-pandey/) for the latest updates and discussions.

Did you find this article valuable?

Support Som Pandey's blog by becoming a sponsor. Any amount is appreciated!