Skip to content

Latest commit

 

History

History
119 lines (82 loc) · 2.25 KB

File metadata and controls

119 lines (82 loc) · 2.25 KB

Ansible Use Case: Nginx Deployment

Overview

This lab demonstrates how to use Ansible to configure a remote server as a web server. It covers installing Nginx, managing its service, and creating a custom welcome page using a playbook.


Project Structure

workspace/
├── inventory.ini
└── site.yml

Implementation Steps

1. Create Workspace

mkdir -p /home/user/workspace
cd /home/user/workspace/

2. Create Inventory File

[web]
server1 ansible_host=server1 ansible_user=server1_admin ansible_ssh_pass=server1_admin@123! ansible_become_pass=server1_admin@123!

Defines server1 and enables Ansible to connect with sudo privileges.


3. Create Playbook (site.yml)

- name: Configure server1 as a web server
  hosts: web
  become: true

  tasks:
    - name: Install Nginx
      apt:
        name: nginx
        state: present
        update_cache: yes
      tags:
        - nginx

    - name: Start and enable Nginx service
      service:
        name: nginx
        state: started
        enabled: true
      tags:
        - nginx

    - name: Create welcome page
      copy:
        dest: /var/www/html/index.html
        content: "Welcome to Ansible Lab!"
      tags:
        - welcome

4. Run Playbook

ansible-playbook -i /home/user/workspace/inventory.ini /home/user/workspace/site.yml

Executes the playbook and configures server1 as a web server.


5. Validation

  • Confirm that Nginx is installed and running:
ansible -i inventory.ini web -m service -a "name=nginx state=started"
  • Verify the custom welcome page exists:
ansible -i inventory.ini web -m command -a "cat /var/www/html/index.html"

Should display:

Welcome to Ansible Lab!

Key Concepts

  • Playbooks for multi-task automation
  • Installing packages with the apt module
  • Managing services with the service module
  • Creating files with the copy module
  • Using become for privilege escalation

Summary

This lab demonstrates how Ansible can automate Nginx deployment and configuration on a remote server, ensuring repeatable and consistent web server setup.