Ansible Inventory Management: HAProxy and Web Servers — Step-by-Step Guide

Ansible Inventory & Jinja2

Ansible Inventory is a key element in automating infrastructure management. In this post, we will explore how to efficiently manage your servers using Ansible Inventory. We’ll also demonstrate a real-world scenario by setting up HAProxy and multiple web servers. By the end, you’ll know how to manage your hosts and automate tasks seamlessly.

To better understand the basics, check out my blog on how Ansible works for a deeper dive into its core functionality

Understanding Ansible Inventory

In Ansible, the Inventory plays a crucial role in organizing servers. Whether you’re automating tasks for 5 or 500 machines, a well-structured inventory ensures smooth and effective operation.

Types of Ansible Inventory

  1. Static Inventory
    The static inventory consists of INI or YAML files. It is suitable for environments with a fixed list of servers, where you manually define hosts.
  2. Dynamic Inventory
    The dynamic inventory is generated in real-time, fetching host details from external sources like cloud providers. This is perfect for rapidly changing infrastructures.

Using an inventory allows for better organization of your hosts, making it easier to automate commands and playbooks across various systems.

Watch This Video for a Step-by-Step Tutorial

If you prefer visual learning, check out the video below where I demonstrate how to configure Ansible Inventory and deploy HAProxy with web servers. It’s a great way to complement this guide and see the setup in action.

Organizing Hosts and Groups in Ansible Inventory

Ansible allows us to group hosts based on their roles. Using groups, commands or playbooks can be run against specific group rather than individual servers, which improves scalability and efficiency.

For instance:

  • [webservers]: This group includes all the web server hosts.
  • [loadbalancers]: This group includes the HAProxy instances managing traffic.

Automation will work faster when we are using Ansible inventory.


Hands-On: Real-World Ansible Inventory Setup with HAProxy and Web Servers

Step 1: Set Up HAProxy and Web Servers

For this demonstration, we will configure a load balancer using HAProxy and connect it to multiple web servers.

  • HAProxy will act as the load balancer.
  • Web Servers will handle traffic distribution.

This scenario shows the power of Ansible inventory when managing various components in a real-world setup.

Step 2: Writing the Ansible Inventory File

We need to write a ansible inventory file in yaml or INI format.. Here’s how to define hosts and groups for this setup:

[lb]
172.31.39.145

[webservers]
172.31.34.24
172.31.45.161

In this inventory, we’ve categorized HAProxy under “loadbalancers” and two web servers under “webservers.”

For more detailed information about Ansible inventory check out official documentation.


Step 3: Playbook to Automate Deployment

Next, we create a playbook to automate the installation and configuration of HAProxy and web servers. This playbook checks the information about the hosts in the inventory file.

- hosts: lb
tasks:
- name: Install haproxy package
ansible.builtin.package:
name: haproxy
state: present

- name: update configuration for haproxy
ansible.builtin.template:
src: haproxy.cfg.j2
dest: /etc/haproxy/haproxy.cfg

- name: start haproxy service
ansible.builtin.service:
name: haproxy
state: started
enabled: yes

- hosts: webservers
tasks:
- name: install webserver package
ansible.builtin.package:
name: httpd
state: present

- name: update web page
ansible.builtin.template:
src: index.html.j2
dest: /var/www/html/index.html

- name: start webserver service
ansible.builtin.service:
name: httpd
state: started
enabled: yes

The Ansible playbook uses the inventory to target groups: first installing HAProxy on the load balancer, and then installing Apache on the web servers as per the information updated in the inventory file.

Step 4: Testing the Setup

Once the playbook has been executed, we can test the HAProxy load balancer. By accessing the HAProxy IP in your browser, traffic will be directed to one of the web servers, confirming successful automation.


Mastering Ansible Inventory Management is essential for automating your infrastructure effectively. With Ansible, you can deploy HAProxy and manage web servers, automating repetitive tasks to enhance the resilience of your setup. Regular updates to your inventory are crucial, and exploring dynamic inventory can help you handle more complex environments seamlessly.

Now that you have a solid grasp of Ansible Inventory Management, it’s time to implement these skills in your own projects. Take the leap into automation and watch your infrastructure management become more streamlined and efficient!


If you’re new to Ansible or looking to dive deeper, check out my Ansible Playlist on YouTube for step-by-step tutorials.

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top