This experiment demonstrates the implementation of configuration management and automation using Ansible over multiple Docker containers acting as remote servers. The entire setup is executed within a Windows environment using Windows Subsystem for Linux (WSL), ensuring compatibility with native Linux-based tools.
The architecture consists of a control node and multiple managed nodes.
Control Node:
Managed Nodes:
Communication:
Ansible is an open-source automation tool used for configuration management, application deployment, and orchestration. It operates without agents and uses SSH for communication.
Docker is used to create lightweight containers that simulate multiple servers locally.
sudo dpkg –configure -a sudo apt –fix-broken install -y
sudo apt update sudo apt install ansible -y
ansible –version
ssh-keygen -t rsa -b 4096
cp ~/.ssh/id_rsa . cp ~/.ssh/id_rsa.pub .
FROM ubuntu
RUN apt update -y RUN apt install -y python3 openssh-server RUN mkdir -p /var/run/sshd
RUN mkdir -p /run/sshd &&
echo ‘root:password’ | chpasswd &&
sed -i ‘s/#PermitRootLogin prohibit-password/PermitRootLogin yes/’ /etc/ssh/sshd_config &&
sed -i ‘s/#PasswordAuthentication yes/PasswordAuthentication no/’ /etc/ssh/sshd_config &&
sed -i ‘s/#PubkeyAuthentication yes/PubkeyAuthentication yes/’ /etc/ssh/sshd_config
RUN mkdir -p /root/.ssh COPY id_rsa /root/.ssh/id_rsa COPY id_rsa.pub /root/.ssh/authorized_keys
RUN chmod 600 /root/.ssh/id_rsa RUN chmod 644 /root/.ssh/authorized_keys
EXPOSE 22 CMD [“/usr/sbin/sshd”, “-D”]
docker build -t ubuntu-server .
for i in {1..4}; do docker run -d -p 220$i:22 –name server$i ubuntu-server done
[servers] localhost:2201 localhost:2202 localhost:2203 localhost:2204
[servers:vars] ansible_user=root ansible_ssh_private_key_file=~/.ssh/id_rsa ansible_connection=ssh
name: Configure servers hosts: servers become: yes
tasks:
name: Update packages apt: update_cache: yes
name: Install packages apt: name: [“vim”, “htop”, “wget”] state: present
name: Create file copy: dest: /root/test_file.txt content: “Configured using Ansible automation”
ansible-playbook -i inventory.ini playbook.yml
ansible all -i inventory.ini -m command -a “cat /root/test_file.txt”
The experiment successfully demonstrates the power of Ansible in automating infrastructure tasks across multiple nodes using a Windows-based setup with WSL.
. ├── Dockerfile ├── inventory.ini ├── playbook.yml ├── README.md └── screenshots