Day 59: Ansible Project

Day 59: Ansible Project

Introduction

In this tutorial, we'll walk through the process of creating an Ansible playbook to install Nginx and deploy a custom webpage on multiple Ubuntu servers. This automation simplifies the deployment process, ensuring consistency across your infrastructure. We assume that you have already set up an Ansible master and two Ubuntu hosts on EC2 instances.

Prerequisites

  1. Ansible master and two Ubuntu hosts set up on EC2 instances.

  2. Process to set up ansible is explained in this blog post.

  3. All the code used in this tutorial is available in this 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:

    • Ansible task using the apt module to update the package cache on the target servers.
  2. Install Nginx:

    • Ansible task utilizing the package module to install Nginx on the target servers.
  3. Start Nginx service:

    • Ansible task with the service module to start the Nginx service on the target servers and enable it to start on boot.
  4. Copy custom webpage files:

    • Ansible task using the copy module to copy the custom webpage files from your specified source directory to the destination directory on the target servers.

Running the Playbook: Execute the following command to run the playbook:

ansible-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

Automating the deployment of web services with Ansible simplifies the management and ensures consistency across multiple servers. This tutorial provides a clear and concise guide to deploying Nginx and a custom webpage using Ansible playbooks.

Follow me on LinkedIn.

Checkout my GitHub profile.