This lab demonstrates how to use Ansible to configure a remote server as a web server. It covers installing Nginx, managing its service, and creating a custom welcome page using a playbook.
workspace/
├── inventory.ini
└── site.ymlmkdir -p /home/user/workspace
cd /home/user/workspace/[web]
server1 ansible_host=server1 ansible_user=server1_admin ansible_ssh_pass=server1_admin@123! ansible_become_pass=server1_admin@123!Defines server1 and enables Ansible to connect with sudo privileges.
- name: Configure server1 as a web server
hosts: web
become: true
tasks:
- name: Install Nginx
apt:
name: nginx
state: present
update_cache: yes
tags:
- nginx
- name: Start and enable Nginx service
service:
name: nginx
state: started
enabled: true
tags:
- nginx
- name: Create welcome page
copy:
dest: /var/www/html/index.html
content: "Welcome to Ansible Lab!"
tags:
- welcomeansible-playbook -i /home/user/workspace/inventory.ini /home/user/workspace/site.ymlExecutes the playbook and configures server1 as a web server.
- Confirm that Nginx is installed and running:
ansible -i inventory.ini web -m service -a "name=nginx state=started"- Verify the custom welcome page exists:
ansible -i inventory.ini web -m command -a "cat /var/www/html/index.html"Should display:
Welcome to Ansible Lab!
- Playbooks for multi-task automation
- Installing packages with the
aptmodule - Managing services with the
servicemodule - Creating files with the
copymodule - Using
becomefor privilege escalation
This lab demonstrates how Ansible can automate Nginx deployment and configuration on a remote server, ensuring repeatable and consistent web server setup.