Day 58 - Ansible Playbooks πŸ“πŸ‘€

Nilkanth MistryNilkanth Mistry
4 min read

Hello DevOps enthusiasts! πŸŽ‰ Welcome to Day 58 of the #90DaysOfDevOps challenge. Today, we’re diving into Ansible Playbooks! πŸ› οΈβœ¨

Ansible Playbooks are essential tools in DevOps, allowing us to automate complex tasks, manage configurations, and deploy applications seamlessly. Imagine them as instruction manuals πŸ“š that help you get multiple servers working together efficiently. πŸ“–πŸ’‘

What We'll Cover Today:

  1. Creating a file on a different server using Ansible Playbooks. πŸ“

  2. Creating a new user with an Ansible Playbook. πŸ‘€

  3. Installing Docker on a group of servers using Ansible Playbooks. 🐳

  4. Best practices for writing Ansible Playbooks. 🌟

Let's get started! πŸš€πŸ”₯

Day 4: Understanding Ansible Playbooks β€” The Basics πŸ“šβœ¨ | by Vinoth Subbiah  | Medium

Task 1: Creating a File on a Different Server πŸ“„

Step 1: Write an Ansible Playbook ✍️

Create a playbook file named create_file.yml with the following content to create a file on the server:

---
- name: Create a file on a different server
  hosts: all
  become: true
  tasks:
    - name: Create a file
      file:
        path: /tmp/ansible_file.txt
        state: touch

Step 2: Execute the Playbook πŸš€

Run the playbook using the ansible-playbook command:

ansible-playbook create_file.yml -i <inventory-file-path> --private-key=<private-key-path>

Step 3: Verify the File Creation βœ…

Log into the server and check if the file /tmp/ansible_file.txt has been created:

ssh -i <private-key-path> <user>@<server-ip>
ls /tmp/ansible_file.txt

Task 2: Installing Docker on a Group of Servers 🐳

Step 1: Write an Ansible Playbook ✍️

Create a playbook file named install_docker.yml to install Docker on the servers:

---
- name: Install Docker on servers
  hosts: docker_group
  become: true
  tasks:
    - name: Update apt cache and install Docker
      apt:
        name: docker.io
        state: present
        update_cache: yes
      when: ansible_os_family == 'Debian'

    - name: Install Docker
      yum:
        name: docker
        state: present
      when: ansible_os_family == 'RedHat'

    - name: Start Docker service
      service:
        name: docker
        state: started
        enabled: yes

Step 2: Execute the Playbook πŸš€

Run the playbook using the ansible-playbook command:

ansible-playbook install_docker.yml -i <inventory-file-path> --private-key=<private-key-path>

Step 3: Verify Docker Installation βœ…

Log into one of the servers and check the Docker installation:

ssh -i <private-key-path> <user>@<server-ip>
docker --version

Task 3: Best Practices for Writing Ansible Playbooks πŸ“

Ansible playbooks are YAML files that define a set of tasks and configurations to be executed on remote systems. Playbooks allow you to automate various infrastructure management tasks, such as provisioning servers, configuring services, deploying applications, and more. πŸ’Ό

Examples of Ansible Playbooks πŸ“˜

Installing Packages πŸ“¦

---
- name: Install packages
  hosts: web_servers
  become: true
  tasks:
    - name: Update package cache
      apt:
        update_cache: yes
      when: ansible_os_family == 'Debian'

    - name: Install nginx
      apt:
        name: nginx
        state: present
      when: ansible_os_family == 'Debian'

    - name: Install httpd
      yum:
        name: httpd
        state: present
      when: ansible_os_family == 'RedHat'

Configuring Services πŸ”§

---
- name: Configure Nginx
  hosts: web_servers
  become: true
  tasks:
    - name: Copy Nginx configuration file
      copy:
        src: nginx.conf
        dest: /etc/nginx/nginx.conf
      notify: restart nginx

  handlers:
    - name: restart nginx
      service:
        name: nginx
        state: restarted

Deploying Applications πŸš€

---
- name: Deploy myapp
  hosts: app_servers
  become: true
  tasks:
    - name: Clone Git repository
      git:
        repo: https://github.com/myusername/myapp.git
        dest: /var/www/myapp
        version: master

    - name: Install application dependencies
      command: npm install
      args:
        chdir: /var/www/myapp

    - name: Start the application
      command: npm start
      args:
        chdir: /var/www/myapp

User Management πŸ‘₯

---
- name: Create user accounts
  hosts: all
  become: true
  vars:
    users:
      - username: user1
        password: "{{ 'user1_password' | password_hash('sha512') }}"
      - username: user2
        password: "{{ 'user2_password' | password_hash('sha512') }}"
  tasks:
    - name: Create user accounts
      user:
        name: "{{ item.username }}"
        password: "{{ item.password }}"
        state: present
      with_items: "{{ users }}"

Firewall Configuration πŸ”₯

---
- name: Configure firewall
  hosts: web_servers
  become: true
  vars:
    allowed_ports:
      - 80
      - 443
  tasks:
    - name: Allow incoming HTTP and HTTPS traffic
      ufw:
        rule: allow
        port: "{{ item }}"
      with_items: "{{ allowed_ports }}"

Database Backup πŸ’Ύ

---
- name: Backup database
  hosts: db_servers
  become: true
  tasks:
    - name: Stop database service
      service:
        name: mysql
        state: stopped

    - name: Backup database
      command: mysqldump -u root -p{{ db_password }} my_database > /tmp/my_database_backup.sql
      args:
        warn: false

    - name: Start database service
      service:
        name: mysql
        state: started

Thank you for reading! 😊

0
Subscribe to my newsletter

Read articles from Nilkanth Mistry directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Nilkanth Mistry
Nilkanth Mistry

Embark on a 90-day DevOps journey with me as we tackle challenges, unravel complexities, and conquer the world of seamless software delivery. Join my Hashnode blog series where we'll explore hands-on DevOps scenarios, troubleshooting real-world issues, and mastering the art of efficient deployment. Let's embrace the challenges and elevate our DevOps expertise together! #DevOpsChallenges #HandsOnLearning #ContinuousImprovement