Skip to main content
Now that you understand what an Ansible playbook is and how its structure works, let’s build one from a real-world scenario. This tutorial converts a repetitive manual process into automation so you can deploy a basic web test environment consistently across RHEL hosts. Imagine a small fleet of RHEL servers where developers often need a disposable web test site. Currently someone manually installs Apache (httpd), enables and starts the service, and drops a test page. We’ll automate those steps with a single Ansible playbook.
A slide titled "Automating Manual Web Setup" showing a developer icon connected to a group of RHEL servers. To the right are three listed steps: Install Apache, Start service, and Deploy test page.
What you’ll build
  • A small Ansible project that installs httpd, deploys a template-based index.html, ensures the service is running, and restarts httpd when content changes.
  • The playbook demonstrates inventory, ansible.cfg defaults, variables, tasks, templates, and handlers—the core building blocks of Ansible automation.
Workflow
  • Create a project folder and basic files.
  • Define an inventory that targets the managed host(s).
  • Add a minimal ansible.cfg so you don’t need extra CLI flags.
  • Write a playbook with tasks and handlers.
  • Run the playbook and verify the result on the managed host.
Environment overview
Ensure the Ansible remote user can perform privileged tasks. In this lab the sudoers entry allows the student user to use sudo without a password:
Be careful granting NOPASSWD sudo in production. Use the minimum required privileges and restrict commands where possible.
Install Ansible Core (example)
  • Install Ansible on the control host. The example below uses dnf on RHEL; replace with your platform’s package manager if needed.
Create the project directory and files
  1. Create a project folder and enter it:
  1. Create an inventory file. This example defines a webservers group with servera. Adjust ansible_host if you need an explicit IP.
  1. Create a minimal ansible.cfg so you don’t need to pass —inventory or —user on the command line. Save this as ansible.cfg in the project folder.
Files you will create Write the playbook Create playbook.yml with the content below. The playbook installs the httpd package, deploys a simple index.html template, starts the httpd service, and notifies a handler to restart httpd when the template changes.
Create the template Create the Jinja2 template index.html.j2 in the same project directory. This template uses an Ansible facts variable to include the host’s hostname in the page.
Validate and run the playbook
  1. Perform a syntax check:
  1. Run the playbook:
Example (condensed) output showing the play execution:
Verify the result on the managed host SSH to the managed host (or use a remote check) and curl the local web server to confirm the template is served:
This confirms the playbook installed httpd, deployed the index.html template, started the service, and the handler restarted httpd after the template changed. Conclusion You’ve created a minimal, reusable Ansible project that automates installing and configuring an Apache-based test page on RHEL. This covers essential Ansible concepts—inventory, configuration, variables, tasks, templates, and handlers—that form the foundation for more advanced automation. Links and references

Watch Video