How you can Use Static and Dynamic Inventories in Ansible

On this Half four of Ansible Sequence, we are going to clarify how you can use static and dynamic stock to outline teams of hosts in Ansible.

In an Ansible, managed hosts or servers that are managed by the Ansible management node are outlined in a number stock file as defined in. A number stock file is a textual content file that consists of hostnames or IP addresses of managed hosts or distant servers.

Managed hosts can both be listed as particular person entries or categorized below a bunch identify as we will later see. In Ansible, there are two sorts of stock recordsdata: Static and Dynamic.

Let’s take a look at every one in all these and see how we will handle them. By now, we assume that you’ve already put in Ansible in your Management node, and configured Passwordless SSH connection to your managed hosts.

Static Host Stock File

In Ansible, a static stock file is a plain textual content file that incorporates an inventory of managed hosts declared below a number group utilizing both hostnames or IP addresses.

A number group identify is enclosed in sq. brackets i.e [group name]. The managed host entries are later listed under the group identify, every by itself line. As mentioned earlier, the hosts are listed utilizing both hostnames or IP addresses.

[group name]

Host A ip_address
Host B ip_address
Host c ip_address

For functions of illustration, we will create a static stock file.

# mkdir test_lab && cd test_lab
# vim hosts

Static Stock File




Save the file and exit.

As you’ll be able to see within the stock file above, we have now created 2 host teams: webservers and database_servers. Additionally, we have now created an extra group known as datacenter that features a group of host teams denoted by ‘: kids’ suffix as seen above.

Ansible additionally permits teams of hosts to be positioned below a bunch identify. Within the stock file above, the webservers and database_servers teams have been positioned below the datacenter.

NOTE: It’s not necessary to put managed hosts in a number group. You’ll be able to merely record them utilizing their hostnames or IP addresses for instance.

Let’s now use a couple of Ansible instructions for referencing the host stock file. The fundamental syntax for stock administration is as proven.

$ ansible -i /path/of/stock/file –list-hosts

For instance,

$ ansible all -i /root/test_labs/hosts –list-hosts

List Ansible Hosts

Record Ansible Hosts

Alternatively, you should use the wildcard character * to interchange ‘all’ argument.

$ ansible * -i /root/test_labs/hosts –list-hosts

List Ansible Hosts Using Wildcards

Record Ansible Hosts Utilizing Wildcards

To record hosts in a bunch, specify the host group within the place of host-pattern.

$ ansible webservers -i /root/test_labs/hosts –list-hosts

List Ansible Hosts in Group

Record Ansible Hosts in Group

Dynamic Host Stock File

In a configuration – particularly a cloud setup reminiscent of AWS the place the stock file retains consistently altering as you add or decommission servers, holding tabs on the hosts outlined within the stock file turns into an actual problem. It turns into inconvenient going again to the host file and updating the record of hosts with their IP addresses.

And that is the place a dynamic stock involves play. So what’s a dynamic stock? A dynamic stock is a shell script written in Python, PHP or some other programming language. It turns out to be useful in cloud environments reminiscent of AWS the place IP addresses change as soon as a digital server is stopped and began once more.

Ansible already has developed stock scripts for public cloud platforms reminiscent of Google Compute Engine, Amazon EC2 occasion, OpenStack, RackSpace, cobbler, amongst others.

What are some great benefits of a dynamic stock over a static stock?
  • Dynamic inventories do an ideal job of decreasing human error as data is gathered utilizing scripts.
  • Minimal effort is required in managing inventories.

You’ll be able to write your individual customise dynamic stock in a programming language of your alternative. The stock ought to return a format in JSON when acceptable choices are handed.

Make the most of an Present Dynamic Stock Script

A script that’s used to create a dynamic stock must be made executable in order that Ansible can use it.

To retrieve details about the hosts inside a dynamic stock script merely run.

# ./script –list

As pointed earlier, the output ought to be in JSON within the format under.

A dictionary comprising of teams (i.e webservers, database_Servers)
  • An inventory of managed hosts per group
  • A dictionary of variables
Meta dictionary
  • Hosts and hostvars
Pattern Output

“webservers”: {
“hosts”: [
“vars”: {}
“database_servers”: {
“hosts”: [
“vars”: {}
“_meta”: {
“hostvars”: {
“mysql_db2”: {},
“”: {},
“”: {},
“mysql_db1”: {}


On this article, we have now demonstrated how you can create each static and dynamic inventories. In abstract, a static stock file is a plain textual content file containing an inventory of managed hosts or distant nodes whose numbers and IP addresses stay pretty fixed.

Then again, a dynamic host file retains altering as you add new hosts or decommission outdated ones. The IP addresses of hosts are additionally dynamic as you cease and begin new host techniques. We do hope that you just discovered this tutorial informative.


Germany Devoted Server

Leave a Reply