<html>
<head><title>Updated Nginx Page</title></head>
<body>
<h1>Hello from Ansible!</h1>
</body>
</html>11 Week 10 (4-Dez-2025)
11.1 Use Ansible to Modify an index.html file in Nginx Containers
11.1.1 Prepare the New index.html
Create the file to be copied later into the containers.
File /path/to/new_index.html
11.1.2 Build a Docker Image from a Dockerfile
Exercise: Create a simple
Dockerfilethat uses thenginxbase image and include thessh serverandpythoninto the container.Solution: Create a directory for your project.
mkdir nginx-ssh-container
cd nginx-ssh-container- Create a file named
Dockerfile.
# Use the official Nginx image as the base
FROM nginx:latest
# Update package list and install OpenSSH server
RUN apt-get update && \
apt-get install -y openssh-server && \
mkdir /var/run/sshd
# Install Python
RUN apt-get update && \
apt-get install -y python3 python3-pip
# Set a password for the root user (replace 'password' with a secure password)
RUN echo 'root:password' | chpasswd
# Allow root login via SSH
RUN sed -i 's/#PermitRootLogin prohibit-password/PermitRootLogin yes/' /etc/ssh/sshd_config
# Expose ports for Nginx and SSH
EXPOSE 80 22
# Start both SSH and Nginx services
CMD service ssh start && nginx -g 'daemon off;'- Build the Docker image.
docker build -t my-nginx-with-ssh .- Expected Output: Successful build message with a new image tagged as
my-nginx-with-ssh.
11.1.3 Setup Two Nginx Containers
- Deploy two Nginx containers based on the created Docker image.
docker run -d -p 8081:80 -p 2221:22 --name nginx1 my-nginx-with-ssh
docker run -d -p 8082:80 -p 2222:22 --name nginx2 my-nginx-with-ssh- Verify the contents by accessing the Nginx containers in a browser:
- For nginx1: http://localhost:8081
- For nginx2: http://localhost:8082
- Confirm that you can also login to those nginx servers. Use the password defined in the
Dockerfileabove.
ssh root@localhost -p 2221
ssh root@localhost -p 2222- Exit from both hosts.
11.1.4 Generate a SSH Key Pair
- Generate a Key Pair (Private & Public Keys). This will allow you to connect (over SSH) to any host without a password.
- When generating the keys, just press
Enteron any inputs requests.
ssh-keygen -t rsa -b 4096 -C "ansible_control_machine"- Copy your public key into the nginx servers to be able to login without a password. Enter the password when needed.
- After this step, you are now able to login into the nginx servers without password.
ssh-copy-id -i ~/.ssh/id_rsa.pub -p 2221 root@localhost
ssh-copy-id -i ~/.ssh/id_rsa.pub -p 2222 root@localhost- Login again to those nginx servers and confirm that you don’t need to enter the password anymore.
ssh root@localhost -p 2221
ssh root@localhost -p 222211.1.5 Create a New Ansible Playbook
Write an Ansible playbook to modify the index.html file inside the containers.
Create file
update_nginx_index.yml
- name: Update index.html in Nginx containers
hosts: nginx_containers
tasks:
- name: Ensure the new index.html exists inside the container
copy:
src: /path/to/new_index.html
dest: /usr/share/nginx/html/index.html
remote_src: no- Create file
inventory.yml
all:
hosts:
nginx1:
ansible_host: localhost
ansible_port: 2221
ansible_user: root
ansible_python_interpreter: /usr/bin/python3
nginx2:
ansible_host: localhost
ansible_port: 2222
ansible_user: root
ansible_python_interpreter: /usr/bin/python3
children:
nginx_containers:
hosts:
nginx1:
nginx2:- Check that you can ping the hosts by executing the following command.
ansible -i inventory.yml -m ping- If you have success, now you can run the playbook.
11.1.6 Run the Playbook
- Use the following command to execute the playbook and update the containers.
ansible-playbook -i inventory.yml update_nginx_index.yml11.1.7 Expected Outcome
The index.html file inside both nginx1 and nginx2 containers is replaced with the custom version.
Restarting the containers reflects the updated content.
Verify by accessing the Nginx containers in a browser:
- For nginx1: http://localhost:8081
- For nginx2: http://localhost:8082
Both should display the updated message: “Hello from Ansible!”