Automate Server Setup with Ansible and Terraform: Simplify Your Web Server Deployment

Areeba HassanAreeba Hassan
3 min read

Deploying and managing web servers manually can be time-consuming and error-prone, especially when you need consistent environments across development, staging, and production.

In this project, I built a solution to automate the entire setup of production-ready web servers using Ansible and Terraform — two powerful tools for Infrastructure as Code (IaC) and automation.


What This Project Does

  • Automatically configures Nginx as the web server

  • Sets up UFW (Uncomplicated Firewall) for enhanced security

  • Installs and manages Docker for containerized applications

  • Provisions AWS infrastructure such as EC2 instances using Terraform

This lets you quickly spin up new servers with identical configurations, reducing manual effort and minimizing configuration drift.


Why Use Ansible and Terraform?

  • Ansible: Agentless automation tool for configuring servers, installing software, and managing users with repeatable playbooks.

  • Terraform: Cloud infrastructure provisioning tool that uses declarative configs to create and manage resources like EC2 instances.

Together, they provide a powerful IaC workflow for modern infrastructure management.


How to Get Started

Prerequisites

  • Ansible (v2.9+):
    Install with pip:

      pip install ansible
    
  • Terraform (v1.5+):
    Download from Terraform Downloads

  • AWS CLI:
    Configure with your credentials:

      aws configure
    
  • Python 3

  • Git

  • An AWS account with permissions to create EC2 instances and security groups


Project Structure

The project includes:

automate-server-ansible/
├── ansible.cfg                # Ansible configuration
├── group_vars/all.yml         # Shared variables for hosts
├── inventory/aws_ec2.yml      # AWS EC2 inventory
├── playbook.yml               # Main Ansible playbook
├── roles/                     # Modular Ansible roles
│   ├── common/tasks/main.yml  # System updates
│   ├── docker/tasks/main.yml  # Docker setup
│   ├── nginx/                 # Nginx setup
│   │   ├── handlers/main.yml
│   │   ├── tasks/main.yml
│   │   └── templates/nginx.conf.j2
│   ├── ufw/tasks/main.yml     # UFW firewall setup
│   └── user/tasks/main.yml    # User account setup
├── terraform/                 # AWS infrastructure
│   ├── main.tf                # Terraform config
│   ├── outputs.tf             # Output definitions
│   ├── variables.tf           # Variables
│   └── terraform.tfstate      # Terraform state file
├── .github/workflows/lint.yml # CI linting
├── .gitignore                 # Git ignore rules
├── LICENSE                    # MIT License
└── README.md                  # Project documentation

Basic Usage

  1. Clone the repository

     git clone https://github.com/reehassan/automate-server-ansible.git
     cd automate-server-ansible
    
  2. Set up AWS credentials

     export AWS_ACCESS_KEY_ID="your-access-key"
     export AWS_SECRET_ACCESS_KEY="your-secret-key"
    
  3. Provision AWS infrastructure

     cd terraform
     terraform init
     terraform apply
    
  4. Run Ansible playbook to configure servers

     ansible-playbook -i inventory/aws_ec2.yml playbook.yml
    
  5. Verify your setup

     curl http://<server-ip>
     ssh <server-ip> docker --version
     ssh <server-ip> sudo ufw status
    

Final Thoughts

Automating server setups saves time, reduces errors, and enforces best practices in infrastructure management. This project is ideal for DevOps engineers, sysadmins, and developers aiming to build scalable and reliable infrastructure with ease.


Explore the Code

Check out the full source code and get started here:
https://github.com/reehassan/automate-server-ansible.git

Feel free to reach out if you have questions or want to collaborate!


Tags

#DevOps #Ansible #Terraform #InfrastructureAsCode #AWS #Automation #CloudComputing #ServerSetup

0
Subscribe to my newsletter

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

Written by

Areeba Hassan
Areeba Hassan

I’m Areeba Hassan, a driven Software Engineering student a at VU, with a passion for DevOps and cloud-native technologies. I’m fascinated by automating workflows and building scalable systems that power modern applications. With a solid foundation in tools like Docker, Kubernetes, Terraform, and Jenkins, I thrive on tackling complex challenges and exploring innovative solutions. My coursework in Data Structures, Operating Systems, and Software Engineering fuels my curiosity and technical growth. I’m excited to share my learning journey on LinkedIn and GitHub, connect with the tech community, and grow as a DevOps engineer. Let’s collaborate and innovate! 🚀