DEV Community

Cover image for Day 22: Automating Web Server Setup with Ansible on Ubuntu
Arbythecoder
Arbythecoder

Posted on

Day 22: Automating Web Server Setup with Ansible on Ubuntu

Introduction

Hello everyone, it's Day 22 of my 90 Days of DevOps journey, and I'm thrilled to dive into the world of automation with Ansible. As an SRE and cloud security engineer, I'm always looking for ways to streamline my workflow and improve efficiency. Ansible seems like the perfect tool to help me achieve that.

Project Goal

Our goal is to leverage Ansible to streamline the process of setting up a web server, eliminating manual configuration steps and ensuring consistency across deployments. We'll be focusing on deploying a basic web server on an Ubuntu machine, but the principles we learn can be applied to a wide range of automation tasks.

Step-by-Step Project Guide

1. Installing Ansible on Ubuntu

  • Update Package List:
  sudo apt update
Enter fullscreen mode Exit fullscreen mode
  • Install Ansible:
  sudo apt install ansible
Enter fullscreen mode Exit fullscreen mode
  • Verify Installation:
  ansible --version
Enter fullscreen mode Exit fullscreen mode

2. Preparing the Ansible Environment

  • Create a Project Directory:
  mkdir ~/ansible-project
  cd ~/ansible-project
Enter fullscreen mode Exit fullscreen mode
  • Set Up an Inventory File:

    • Create an inventory.ini file:
    touch inventory.ini
    
    • Edit inventory.ini to include your server details:
    [webservers]
    server1 ansible_host=your_server_ip ansible_user=your_username
    
    [all:vars]
    ansible_python_interpreter=/usr/bin/python3
    

    Replace your_server_ip with your server's IP address and your_username with your SSH username.

3. Creating Your First Playbook

  • Create a Playbook File:
  touch webserver.yml
Enter fullscreen mode Exit fullscreen mode
  • Write the Playbook:
  ---
  - name: Set up a web server
    hosts: webservers
    become: true
    tasks:
      - name: Ensure Apache is installed
        apt:
          name: apache2
          state: present

      - name: Ensure Apache is running
        service:
          name: apache2
          state: started
          enabled: true

      - name: Deploy custom index.html
        copy:
          src: /path/to/your/local/index.html
          dest: /var/www/html/index.html
          mode: 0644
Enter fullscreen mode Exit fullscreen mode

Explanation:

  • hosts: Specifies the target group (webservers) from the inventory file.
  • become: true: Uses sudo to execute commands as a superuser.
  • tasks: A list of tasks to perform:
    • Install Apache: Uses the apt module to ensure Apache is installed.
    • Start and Enable Apache: Uses the service module to start and enable the Apache service.
    • Deploy a Custom Index.html: Uses the copy module to deploy a custom index.html file to the web server's root directory. The mode: 0644 ensures the file has the correct permissions.

4. Deploying Your Configuration with Ansible

  • Test the Connection:
  ansible all -m ping -i inventory.ini
Enter fullscreen mode Exit fullscreen mode

You should see a "pong" response if the connection is successful.

  • Run the Playbook:
  ansible-playbook -i inventory.ini webserver.yml
Enter fullscreen mode Exit fullscreen mode

Review the output to ensure all tasks were completed successfully.

5. Verifying the Deployment

  • Access the Web Server: Open a web browser and navigate to http://your_server_ip (replace with your server's IP address). You should see the content of the index.html file deployed by Ansible.

Challenges Faced

  • Connection Refused: One common challenge is encountering a "Connection Refused" error when trying to connect to the remote server. This could be due to firewall settings, SSH port blocking, or incorrect server IP/username.
  • Permission Errors: You might encounter permission errors when trying to install or configure software on the remote server. This usually requires adjusting permissions or using sudo appropriately.
  • Module Errors: Ansible modules can sometimes fail due to dependencies or configuration issues. Carefully review error messages and consult module documentation for troubleshooting.

Best Practices for Windows Users

  • Leverage Windows Subsystem for Linux (WSL): Since I'm a Windows user, I chose to install Ubuntu through WSL. This provides a seamless Linux environment within Windows, allowing me to run Ansible commands directly. This is a great option for Windows users who want to take advantage of Ansible's power without switching to a full Linux system.
  • Install Ansible on WSL: Follow the same installation steps as outlined in the guide, but within your WSL distribution (e.g., Ubuntu).
  • Use Git Bash (Optional): While WSL provides a Bash shell, you can also use Git Bash for a more familiar Bash experience within Windows. This is especially helpful if you're already comfortable with Git Bash for other tasks.
  • Manage Inventory with a Separate Tool (Optional): For complex environments with numerous servers, consider using tools like Ansible Tower or configuration management systems like Puppet or Chef to manage your inventory. These tools can simplify the process of managing and organizing your servers, especially when you have many to work with.

Conclusion

Ansible simplifies the process of managing and configuring systems, making it a valuable tool for automating tasks like web server setup. By following the steps outlined in this guide, you can quickly and efficiently deploy a web server on a remote Ubuntu machine. Remember to troubleshoot any challenges you encounter and explore additional Ansible modules to expand your automation capabilities.

Resources:

I hope this article has been helpful. Stay tuned for more exciting DevOps adventures in the coming days!

Top comments (1)

Collapse
 
ugbedam profile image
Damian Oguche

Dear Arbythecoder, am really impressed with this 90 days of DevOps marathon. Am about to start learning on that. I will like to collaborate with you on this journey too. I sent you a mail. Please check and let's connect. Thank you