How to use a range of hosts in Ansible Inventory With Examples?

Using Range of Hosts in Ansible Inventory: A Comprehensive Guide

One of the more sophisticated features of Ansible is its ability to manage large groups of hosts. These host groups, defined in Ansible Inventory files, can significantly streamline your Ansible playbooks and roles. In this post, we’ll go over how to use a range of hosts in Ansible Inventory, an indispensable tool when dealing with many hosts.

Ansible Inventory Basics

An Ansible Inventory file contains the hosts or nodes that will be managed by Ansible. We can either specify each host individually or use a range of hosts to define a group. This ability to define ranges of hosts is particularly useful when managing large groups of hosts, as it eliminates the need to list each host individually.

Using Range of Hosts in Ansible Inventory

When defining a large set of hosts, it can be cumbersome and error-prone to list each IP address. This is where the range functionality comes in handy. Let’s say we have a set of hosts whose IP addresses vary only by the last digit. Instead of listing each one, we can use a range of hosts.

For example:

In the above configuration, the web_servers group consists of 11 hosts ranging from to The square brackets [] define the range, and the colon : separates the start and end of the range.

You can even use multiple ranges in one line. For instance, if you have hosts on two different subnets:

This configuration will include hosts from to and to

More Examples: Using Alphabetic Ranges

In addition to numeric ranges, Ansible Inventory also supports alphabetic ranges, which are especially useful when you are dealing with named hosts.

For example:

Here, the db_servers group includes hosts named db-server-a through db-server-f.


As you can see, using a range of hosts in your Ansible Inventory file simplifies managing large groups of hosts. Whether your hosts are numbered or named, Ansible’s range functionality can make your Inventory files more manageable and your playbook execution more efficient. Whether you’re a seasoned Ansible user or just getting started with this powerful automation tool, knowing how to use the host range feature effectively is a valuable skill.