Hands-on Lab

#Lab: Configuration Management with Ansible

Configure servers using Ansible.

#🎯 Objectives

  • Create inventory
  • Write playbook
  • Apply configuration

#Task 1: Setup Inventory

ini
1# inventory.ini
2[webservers]
3web1 ansible_host=192.168.1.10
4web2 ansible_host=192.168.1.11
5
6[all:vars]
7ansible_user=ubuntu
8ansible_python_interpreter=/usr/bin/python3

#Task 2: Create Playbook

yaml
1# webserver.yml
2- name: Configure web servers
3  hosts: webservers
4  become: yes
5
6  tasks:
7    - name: Update apt cache
8      apt:
9        update_cache: yes
10        cache_valid_time: 3600
11
12    - name: Install nginx
13      apt:
14        name: nginx
15        state: present
16
17    - name: Start nginx
18      service:
19        name: nginx
20        state: started
21        enabled: yes

#Task 3: Run Playbook

bash
1# Dry run
2ansible-playbook -i inventory.ini webserver.yml --check
3
4# Apply
5ansible-playbook -i inventory.ini webserver.yml

#✅ Success Criteria

  • Inventory configured
  • Playbook syntax valid
  • Nginx installed and running