Simplify Web Server Deployment with Ansible

ยท

3 min read

Welcome to the thrilling world of automation, where we'll embark on a journey to set up a Dockerized web server using the power of Ansible. Buckle up as we demystify complex server configurations and unleash the magic of automation. ๐Ÿš€

The Power of Ansible

Ansible is your trusted companion for automation, simplifying server configuration with its human-readable format. In this adventure, we'll craft an Ansible Playbook to:

๐Ÿณ Configure Docker: Get Docker up and running on your managed nodes.

๐Ÿš€ Start and Enable Docker Services: Ensure Docker is not just installed but running and ready to serve your containers.

๐Ÿ“ฆ Pull the HTTPD Server Image: Fetch the latest HTTPD server image from Docker Hub, so you're always up to date.

๐Ÿƒโ€โ™‚๏ธ Run the Docker Container: Launch your HTTPD server inside a Docker container and expose it to the world.

๐Ÿ“‚ Copy HTML Code: Move your website's HTML code to the /var/www/html directory within the container.

๐ŸŒ Start the Web Server: Fire up your web server, and your site is live!

Let's Get Started

Prerequisites

Before we dive in, ensure you have the following essentials:

  1. Ansible Installed: Make sure Ansible is installed on your local machine.

  2. SSH Access: You should have SSH access to the managed nodes.

  3. Docker Ready: The managed nodes should have Docker installed.

Crafting the Ansible Playbook

Create a dedicated directory for your Ansible project and move into it. Inside this directory, create a playbook YAML file, perhaps named docker_httpd.yml. Now, let's dive into the contents of this playbook:

---
- name: Configure Docker and Start HTTPD Server
  hosts: your_managed_nodes
  become: yes  # To run tasks with sudo privileges

  tasks:
    - name: Install Docker
      apt:
        name: docker.io
        state: present  # Ensure Docker is installed
      when: ansible_distribution == 'Ubuntu'  # Adjust for your OS

    - name: Start and enable Docker service
      service:
        name: docker
        state: started
        enabled: yes

    - name: Pull the HTTPD Docker image
      docker_image:
        name: httpd:latest
      register: httpd_image

    - name: Run the HTTPD Docker container
      docker_container:
        name: my_httpd_container
        image: "{{ httpd_image.image }}"
        ports:
          - "80:80"
        restart_policy: always
      register: httpd_container

    - name: Copy HTML code to /var/www/html
      copy:
        src: your_local_html_folder/index.html
        dest: /var/www/html/index.html

    - name: Start HTTPD service
      command: systemctl start httpd

In this playbook:

  1. We start by installing Docker (adjust for your OS as needed).

  2. We then start and enable the Docker service.

  3. Next, we pull the latest HTTPD Docker image from Docker Hub.

  4. We run the Docker container, exposing port 80.

  5. We copy the HTML code (replace your_local_html_folder with the actual path to your HTML files) to the /var/www/html directory.

  6. Finally, we start the HTTPD service inside the container.

Inventory File

Create an inventory file (e.g., inventory.ini) with the IP addresses or hostnames of your managed nodes:

[your_managed_nodes]
node1 ansible_host=192.168.1.101
node2 ansible_host=192.168.1.102
# Add more nodes if needed

Start the Automation Journey

Now that your playbook and inventory are set up, it's time to unleash the magic. Run the playbook with the following command:

ansible-playbook -i inventory.ini docker_httpd.yml

Sit back and watch as Ansible connects to your managed nodes and flawlessly sets up the Docker container running the HTTPD server with your HTML code.

In this exciting journey, we've harnessed the power of Ansible to simplify the deployment of a Dockerized web server. Automation is your ally in managing complex configurations, making your life as a developer or sysadmin much easier. ๐Ÿš€

ย