Streamlining Web Deployment with Ansible: A Comprehensive Tutorial

Streamlining Web Deployment with Ansible: A Comprehensive Tutorial

Day 58: Ansible Project

ยท

2 min read

Introduction

In this tutorial, we embark on a journey to streamline the deployment process of Nginx and a custom webpage across multiple Ubuntu servers using Ansible. This automation not only simplifies deployment but also ensures consistency and reliability across your infrastructure. Assuming you have already set up an Ansible master and two Ubuntu hosts on EC2 instances, let's dive into the automation magic!

Prerequisites

Ensure you have set up an Ansible master and two Ubuntu hosts on EC2 instances. If not, refer to our blog post for detailed setup instructions. Additionally, access all the code used in this tutorial from the provided GitHub repository.

Ansible Playbook Overview

---
- name: Install Nginx and deploy a custom webpage
  hosts: your_ubuntu_servers  # Replace with your target server(s) or inventory group
  become: true  # Run tasks with elevated privileges

  tasks:
    - name: Update package cache
      apt:
        update_cache: yes

    - name: Install Nginx
      package:
        name: nginx
        state: present

    - name: Start Nginx service
      service:
        name: nginx
        state: started
        enabled: yes

    - name: Copy custom webpage files
      copy:
        src: /path/to/your_webpage_files/
        dest: /var/www/html/

Explanation

  1. Update package cache: This Ansible task uses the apt module to update the package cache on the target servers, ensuring the latest package information is available.

  2. Install Nginx: The package module is employed to install Nginx on the target servers, ensuring that the web server is present for hosting the custom webpage.

  3. Start Nginx service: Utilizing the service module, this task starts the Nginx service on the target servers and ensures it is enabled to start on boot, guaranteeing seamless operation.

  4. Copy custom webpage files: Using the copy module, this task copies the custom webpage files from your specified source directory to the destination directory on the target servers, facilitating the deployment of your custom webpage.

Running the Playbook

Execute the following command to run the playbook:

bashCopy codeansible-playbook -i /etc/ansible/hosts deploy_webpage.yml

Verification

After the playbook execution is complete, visit the public IP of any host server in your web browser to verify if the webpage has been successfully deployed.

Conclusion

By automating the deployment of web services with Ansible, you not only simplify management but also ensure consistency and reliability across multiple servers. This tutorial serves as a clear and concise guide to deploying Nginx and a custom webpage using Ansible playbooks, empowering you to streamline your infrastructure deployment processes effortlessly.

Happy Learning ๐Ÿš€

Follow me on LinkedIn.

ย