Automate Your Web Server Setup: A Real-World Example Using Ansible in Docker
Razedul Islam
System Administrator | Database and Network Specialist | Shell Scripting | DevOps Learner
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:
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:
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
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