What is Ansible Dynamic Inventory?& how its works?

What is Ansible Dynamic Inventory?& how its works?
Techiio-author
Written by Nilima PaulFebruary 3, 2022
11 min read
Ansible
1 VIEWS 0 LIKES 0 DISLIKES SHARE
0 LIKES 0 DISLIKES 1 VIEWS SHARE
Techiio-author
Nilima Paul

Technology Security Analyst

We will discuss, what is Ansible Dynamic Inventory.

Introduction to Ansible Dynamic Inventory

The accompanying article gives a diagram of to Ansible Dynamic Inventory. Ansible needs stock records to get target hubs data for running errands or plays on them. The stock record can be static or dynamic. We realize very well that utilizing static inventories is not a sensible underway climate generally. Since static records are overseen physically, yet in genuine, the framework conditions continue to change consistently much of the time. We generally make new hubs and eliminate undesirable hubs, this training is exceptionally normal and done now and again, particularly in cloud and holder conditions. So in such cases, we want Dynamic Inventory, which brings the rundown of hubs from foundation climate continuously founded on certain standards.

What is Ansible Dynamic Inventory?

For the business prerequisites, we want to make, eliminate and adjust the hosts in our foundation climate often, and to deal with those hubs using Ansible, one needs to refresh the stock record often. Along these lines, the static stock record isn't reasonable. In such a case, we want a stock record that rthathes itself.

This kind of stock record is called a Dynamic stock document, which has a dynamic substance. In our current circumstance, the wellsprings of host hubs can be AWS, OpenStack, Cobbler, LDAP frameworks. These frameworks have their rundown of hubs and Ansible backings to coordinates such outside powerful stock.

In Ansible we have two methods for interfacing with outer powerful inventories:

  • Via Scripts: This is the older way and ensures backward compatibility. But don’t use it with the latest versions.
  • Using Plugins: This is recommended over scripts for dynamic inventory, as plugins are updated with Ansible core code to take advantage of it.

Along with these two, if someone wants to use GUI, then RedHat Ansible Tower or a free version of AWX can be used, these integrate with the source of remote hosts and provide a nice view with a graphical inventory editor.

How does Ansible Dynamic Inventory work?

In Ansible, Dynamic stock is produced either by scripts which are written in a programming language like python, php and so on or utilizing accessible stock modules. While utilizing script, they gets generally constant information from the objective source conditions, similar to Cloud stages AWS, OpenStack, GCP and so on Ansible people group have effectively grown such sort of contents for larger part of stages like AWS, OpenStack, GCP. Such sort of contents can be composed by you also. Yet, utilizing scripts isn't very much reported and not fruitful each time while utilizing script, we should download the contents for source climate and set env variable according to that.

These contents are kept up with by Ansible people group. Generally the contents are written in python and named as ec2.py, open-stack_inventory.py. Alongside these python scripts there are ini documents too which are expected to set the climate. However, this is totally reliant upon the content you are utilizing. In Ansible, we have a bunch of accessible stock modules which are loaded with Ansible bundles. Comparatively you can make your own modules which suits your prerequisites and wellspring of far off hubs. The stock returns the rundown of things in JSON design. Ansible gives a lot of stock modules utilizing which you can use to get the rundown of target has, on which you will be running any undertaking or play.

Using below command you can get the list of available inventory plugins:

Code:

ansible-doc -t inventory -l

From that list you can select the plugin which is reasonable for your source environment.

Then you can get details or documentation about that plugin using command like below:

Code:

ansible-doc -t inventory <plugin name>

Below is the list of inventory plugins available to Ansible 2.9.7. This is list may get updated in future versions of Ansible.

nmap
host_list
hcloud
openstack
vultr
aws_ec2
cloudscale
virtualbox
constructed
k8s
generator
script
vmware_vm_inventory
linode
docker_machine
yaml
online
azure_rm
docker_swarm
advanced_host_list
foreman
auto
kubevirt
gitlab_runners
netbox
gcp_compute
aws_rds
openshift
toml
tower
scaleway
ini

To utilize a module you need to empower it, of course in ansible.cfg we have enable_plugins boundary characterizing it. Likewise auto is set default to it which recognizes the right module to parse in light of given documents.

Yet, to empower a particular stock module, you should give completely qualified name of it like underneath in ansible.cfg.

Code:

[inventory] enable_plugins = namespace.collection_name.inventory_plugin_name

After enabling a plugin, we must provide an inventory source to system to parse it. This is a file in same relative path to inventory or playbook.

Examples of Ansible Dynamic Inventory

Given below are the examples of ansible dynamic inventory:

As this is an advance feature in Ansible, we will use a very useful and common inventory plugin named aws_ec2.

Example #1

For this we create a source file named demo_aws_ec2.yaml. which have below contents.

Code:

plugin: aws_ec2 regions:
ap-south-1 filters:
tag:tagtype: testing

Likewise, we want to set and product beneath env factors for making association with AWS climate.

The qualities provide for these are Access Key ID and Secret Access Key of your record.

  • AWS_ACCESS_KEY_ID
  • AWS_SECRET_ACCESS_KEY

Once above is finished, you should have beneath necessities on your Ansible regulator hub:

  • Boto3
  • botocore

Presently, in our current circumstance, we have EC2 cases labeled in an unexpected way.

We attempt to get rundown of same utilizing labels like beneath:

Code

ansible-inventory -i demo_aws_ec2.yaml –graph

Output:

We will get output like below, because we have only one ec2 instance which have one tag matching to our source file:

Example #2

Now we update this source file like below to use private ipv4 address of matched EC2 instances:

Code:

plugin: aws_ec2 regions:
ap-south-1 filters:
tag:tagtype: testing compose:
ansible_host: private_ip_address

We have a sample playbook to create a file on target node with contents like below:

Code:

hosts:
all tasks:
name: here we create a file on target nodes file:
state: touch
path: /var/tmp/from_dyn_inv.txt

Code:

ansible-playbook ansible_dyn_inv.yaml -i demo_aws_ec2.yaml -v

Output:

blogpost

Example #3

Now when we have another EC2 with matching condition of source file like same tag, ideally the playbook should now automatically run on that hosts as well.

First check the list of matching EC2 instances like below after updating the tag value of another EC2 instance:

Code:

ansible-inventory -i demo_aws_ec2.yaml --graph

Output:

blogpost

Example #4

Now when we run the same playbook on this inventory, sample file will be created on both hosts.

Code:

ansible-playbook ansible_dyn_inv.yaml -i demo_aws_ec2.yaml -v

Output:

blogpost

Conclusion

We saw that utilizing and setting up Ansible Dynamic Inventories is a perplexing errand and ward on the conditions for which you set it up. Yet, when the arrangement is finished and we begin getting ongoing rundown of target hubs, then, at that point, you can basically remove the errand of refreshing inventories from your day by day assignment's agenda.

Ansible
ansib facts
Python
1 VIEWS 0 LIKES 0 DISLIKES SHARE
0 LIKES 0 DISLIKES 1 VIEWS SHARE
Was this blog helpful?
techiio-price-plantechiio-price-plantechiio-price-plantechiio-price-plantechiio-price-plan
You must be Logged in to comment
Code Block
Techiio-author
Nilima Paul
Technology Security Analyst
Techiio-followerTechiio-followerTechiio-follower
201 Blog Posts
0 Discussion Threads
Trending Technologies
15
Software91
DevOps48
Frontend Development24
Backend Development20
Server Administration17
Linux Administration28
Data Center24
Sentry24
Terraform23
Ansible83
Docker70
Penetration Testing16
Kubernetes21
NGINX20
JenkinsX17
Techiio-logo

Techiio is on the journey to build an ocean of technical knowledge, scouring the emerging stars in process and proffering them to the corporate world.

Follow us on:

Subscribe to get latest updates

You can unsubscribe anytime from getting updates from us
Developed and maintained by Wikiance
Developed and maintained by Wikiance