Automate Your Web Server Setup: A Real-World Example Using Ansible in Docker
Ansible beginner guide

Automate Your Web Server Setup: A Real-World Example Using Ansible in Docker

What is Ansible?

Ansible is an open-source automation tool used for configuration management, application deployment, and task automation. It allows you to manage and configure systems, deploy software, and orchestrate more complex IT tasks, all from a central location.

Why Use Ansible in Docker?

Running Ansible in a Docker container allows you to create a consistent environment for running Ansible tasks (called "playbooks"). This setup is useful for testing, development, or even running Ansible automation without worrying about the host machine's configuration.


Simple Reasons to Use Ansible with Docker:

  1. Same Setup Everywhere: Ansible in Docker gives you the same environment no matter where you run it, avoiding setup issues.
  2. Move Easily: You can move your Ansible setup inside Docker to any machine, and it will work the same way.
  3. Quick Testing: Docker lets you quickly test your Ansible scripts without affecting your main system.
  4. Fast Setup: You can quickly start and stop environments using Docker, which is great for trying out things or testing.
  5. Saves Resources: Docker uses less memory and CPU than running full virtual machines.
  6. Control Versions: Docker makes it easy to use the exact version of Ansible you need.
  7. Automate Everything: Use Ansible with Docker to automate tasks like setting up servers or deploying apps.

These points make it easy to understand why Ansible and Docker are a great combo, especially for beginners!


Automation using Ansible :-Configure docker and deploy webserver using ansible diagram like below :


..... Create a DockerFile to install ansible on ubuntu server: ......

First of all Create Dockerfile that installs Ansible on an Ubuntu 20.04 base image. Here's a breakdown of how to create this Dockerfile and what each line does:

create a file named DockerFile and put this code into the file.


..... Build the DockerFile ......


..... Create and start new docker container .....


Let's walk through a real-world example where you use Ansible to automate the setup of a web server.

In this scenario, we'll use Ansible to:

  1. install the Apache web server.
  2. Deploy a simple HTML website.
  3. Ensure the Apache service is running.


Step 1: Create the Playbook

Connect to Your Docker Container:

If you're not already connected to your Docker container, use:


2. Create a Directory for the Playbook:

Inside the container, create a directory to store your playbook and website files:


3. Create the Playbook File:

Create a file named setup_webserver.yml:


4. Add the Following Content to the Playbook:


Explanation:

hosts: localhost: This playbook runs on the local machine (inside the Docker container).

become: yes: Allows Ansible to run tasks with elevated (root) privileges.

tasks: The list of tasks includes:

Update apt package manager: Updates the package list.

Install Apache web server: Installs Apache (apache2 package).

Deploy a simple HTML file: Creates an index.html file with a simple HTML page and places it in the Apache web root (/var/www/html/).

Ensure Apache is running: Starts the Apache service and ensures it runs on boot.

Save and Exit the Editor:

Press Ctrl + X, then Y, and Enter to save and exit the nano editor.


Step 2: Run the Playbook

Run the Playbook:

Use the following command to execute the playbook:


Observe the Output:

Ansible will run each task and display the results in the terminal. You should see a message indicating that Apache has been installed, the HTML file has been deployed, and the Apache service is running.

Step 3: Verify the Setup

  1. Check the Apache Service:

Verify that the Apache service is running by using:


2. Access the Web Page:

Since you are running Apache inside a Docker container, you would typically map the container’s port 80 to the host machine's port 80 when running the container. If you did that, you can open a web browser and navigate to https://localhost or https://<your_container_ip>.

You should see the HTML page with the message:

Summary

In this real-world example, you used Ansible to automate the installation and configuration of an Apache web server, deployed a simple HTML file, and ensured the Apache service was running. This kind of automation can be applied to more complex scenarios, such as deploying entire web applications, managing multiple servers, or orchestrating complex IT workflows.

If you have any questions or need further guidance, feel free to ask!

#Ansible #Automation #DevOps

#Docker #ConfigurationManagement


要查看或添加评论,请登录

Razedul Islam的更多文章

社区洞察

其他会员也浏览了