Hands-on Lab

#Lab: Ansible Playbooks

Automate server configuration with Ansible playbooks.

#🎯 Objectives

  • Write Ansible playbooks for server setup
  • Use variables and templates
  • Implement handlers and tasks

#📋 Prerequisites

  • Ansible installed
  • SSH access to target server (or local testing)

#⏱️ Duration: 45 minutes


#Task 1: Setup (5 min)

bash
1mkdir ~/ansible-lab && cd ~/ansible-lab
2
3# Create inventory
4cat << 'EOF' > inventory.ini
5[webservers]
6web1 ansible_host=192.168.1.10 ansible_user=ubuntu
7
8[dbservers]
9db1 ansible_host=192.168.1.20 ansible_user=ubuntu
10
11[all:vars]
12ansible_python_interpreter=/usr/bin/python3
13EOF
14
15# For local testing
16cat << 'EOF' > inventory-local.ini
17[local]
18localhost ansible_connection=local
19EOF

#Task 2: Basic Playbook (10 min)

#playbook.yml

yaml
1---
2- name: Configure web servers
3  hosts: webservers
4  become: yes
5
6  vars:
7    http_port: 80
8    app_name: myapp
9
10  tasks:
11    - name: Update apt cache
12      apt:
13        update_cache: yes
14        cache_valid_time: 3600
15
16    - name: Install required packages
17      apt:
18        name:
19          - nginx
20          - python3
21          - git
22        state: present
23
24    - name: Start nginx
25      service:
26        name: nginx
27        state: started
28        enabled: yes
29
30    - name: Create app directory
31      file:
32        path: /var/www/{{ app_name }}
33        state: directory
34        mode: '0755'
35        owner: www-data
36        group: www-data

#Run Playbook

bash
1# Syntax check
2ansible-playbook playbook.yml --syntax-check
3
4# Dry run
5ansible-playbook -i inventory.ini playbook.yml --check
6
7# Apply
8ansible-playbook -i inventory.ini playbook.yml
9
10# Local test
11ansible-playbook -i inventory-local.ini playbook.yml --connection=local

#Task 3: Variables and Templates (15 min)

#group_vars/webservers.yml

yaml
1---
2http_port: 80
3server_name: example.com
4document_root: /var/www/html

#templates/nginx.conf.j2

jinja2
1server {
2    listen {{ http_port }};
3    server_name {{ server_name }};
4    root {{ document_root }};
5
6    location / {
7        try_files $uri $uri/ =404;
8    }
9
10    access_log /var/log/nginx/{{ server_name }}_access.log;
11    error_log /var/log/nginx/{{ server_name }}_error.log;
12}

#Updated playbook.yml

yaml
1---
2- name: Configure nginx with template
3  hosts: webservers
4  become: yes
5
6  tasks:
7    - name: Deploy nginx config
8      template:
9        src: templates/nginx.conf.j2
10        dest: /etc/nginx/sites-available/{{ server_name }}
11      notify: Restart nginx
12
13    - name: Enable site
14      file:
15        src: /etc/nginx/sites-available/{{ server_name }}
16        dest: /etc/nginx/sites-enabled/{{ server_name }}
17        state: link
18      notify: Restart nginx
19
20  handlers:
21    - name: Restart nginx
22      service:
23        name: nginx
24        state: restarted

#Task 4: Conditionals and Loops (10 min)

yaml
1---
2- name: Advanced playbook examples
3  hosts: all
4  become: yes
5
6  vars:
7    users:
8      - name: deploy
9        groups: sudo
10      - name: app
11        groups: www-data
12
13  tasks:
14    - name: Install on Debian
15      apt:
16        name: nginx
17        state: present
18      when: ansible_os_family == "Debian"
19
20    - name: Install on RedHat
21      yum:
22        name: nginx
23        state: present
24      when: ansible_os_family == "RedHat"
25
26    - name: Create users
27      user:
28        name: "{{ item.name }}"
29        groups: "{{ item.groups }}"
30        append: yes
31      loop: "{{ users }}"
32
33    - name: Copy files
34      copy:
35        src: "{{ item.src }}"
36        dest: "{{ item.dest }}"
37      loop:
38        - { src: 'file1.txt', dest: '/tmp/file1.txt' }
39        - { src: 'file2.txt', dest: '/tmp/file2.txt' }

#Task 5: Common Modules (5 min)

ModulePurpose
apt/yumPackage management
serviceService management
copyCopy files
templateDeploy Jinja2 templates
fileFile/directory operations
userUser management
command/shellRun commands
gitGit operations
docker_containerDocker management

#✅ Success Criteria

  • Inventory file created
  • Basic playbook runs
  • Templates deployed
  • Handlers triggered