DevOps Classroom notes 30/Jul/2025

Ansible playbooks

  • Ansible playbook is written in YAML file and is collection of play’s.
  • Each Play is collection of tasks (and handlers)
  • Each task represents a unit of activity which is acheived by module.

Ansible module

  • This is smallest (atomic) unit in ansible where we can express desired state.
  • Ansible comes inbuilt with many modules. In addition to this we can add community modules and we can develop our own modules.

Sample playbook

  • Sample Ansible playbook
---
- name: install apache
  become: yes
  hosts: all
  tasks:
    - name: update packages and install apache
      ansible.builtin.apt:
        name: apache2
        update_cache: yes
        state: present
    - name: enable and start apache service
      ansible.builtin.systemd:
        name: apache2
        enabled: yes
        state: started
  • The equivelent of this in shell script
#!/bin/bash
sudo apt update
sudo apt install apache2 -y
sudo systemctl enable apache2
sudo systemctl start apache2

Basic playbook structure

  • Playbook is collection of plays
- name: play1
- name: play2
..
- name: playn
  • Each play will have tasks and
    • hosts: we need to tell where to execute
    • become: do i need to run this entire play with sudo
    • tasks: here we describe the desired state of individual steps
```yaml
- name: play1
  hosts: <where to run>
  become: yes
  tasks:
    - name: task1
      <module>:
         parameter1: <value1>
         ..
         parametern: <valuen>
         state: <desired state>

WOW (Ways of Working)

  • Find out all the manual steps required to configure
  • Ensure they are working
  • For each step(s) try to find a ansible module where you can express desired state.

Example 1: Lets install Apache and PHP using Ansible playbook

Manual steps

  • Create a vm for manual steps
  • Find out commands to install apache and php

1. Update System Packages

Before installing any new software, it’s always good practice to update your package index:

sudo apt update

2. Install Apache Web Server

To install (or ensure you have) the Apache2 web server:

sudo apt install apache2 -y
  • Start Apache and enable it to start at boot:
    bash
    sudo systemctl start apache2
    sudo systemctl enable apache2
  • Check Apache status:
    bash
    sudo systemctl status apache2

You can verify installation by visiting http://YOUR_SERVER_IP in a browser — you should see the Apache default welcome page.

3. Install PHP

Ubuntu 24.04 comes with PHP 8.x in its default repositories. Install PHP and the Apache PHP module:

sudo apt install php libapache2-mod-php -y
  • For some common extensions needed in web development:
    bash
    sudo apt install php-cli php libapache2-mod-php -y
  • Verify PHP installation:
    bash
    php -v
  • (Optional) To see loaded PHP modules:
    bash
    php -m

With this, Apache will auto-process .php files.

4. Test Apache + PHP

You can create a test.php file in /var/www/html/ to verify PHP is working with Apache:

echo '<?php phpinfo(); ?>' | sudo tee /var/www/html/test.php

Visit http://YOUR_SERVER_IP/test.php in your browser. If you see the PHP info page, installation is successful.

Finding modules

  • find the command
sudo apt install xyz -y => apt
sudo dnf install xyz -y => dnf
sudo systemctl restart xyz => systemctl
  • Now open google and type <command> in ansible

  • Now fill the task section Refer Here for the changes done.
  • Now to run the playbook command is
ansible-playbook -i hosts apache.yaml

Published
Categorized as Uncategorized Tagged

By continuous learner

devops & cloud enthusiastic learner

Leave a comment

Turn on JavaScript to leave a comment.

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Please turn AdBlock off
Social Media Icons Powered by Acurax Web Design Company
Exit mobile version
%%footer%%