top of page
Search
jecabe2020

Static And Dynamic Inventories In Ansible: A Comprehensive Reference



The following article provides an outline for Ansible Dynamic Inventory. Ansible need inventory files to get target nodes information for running tasks or plays on them. The inventory file can be static or dynamic. We know very well that using static inventories are not realistic in production environment mostly. Because static files are managed manually, but in real, the infrastructure environments keep changing continuously very frequently. We always create new nodes and removes unwanted nodes, this practice is very common and done frequently, especially in cloud and container environments. So in such cases, we need Dynamic Inventory, which fetches the list of nodes from infrastructure environment in real time based on some criteria.


In this variable and Dynamic infrastructure setup, you cannot keep updating (or) removing the servers into the ansible host group or inventory file, to solve this problem Ansible has this Dynamic inventory concept where you could use some scripts as your inventory source rather defining a static ansible_hosts inventory file with a specific list of hosts.




How To Use Static And Dynamic Inventories In Ansible



In this blog post, we will dive deep into different concepts around building an Ansible inventory, along with best practices. We will look into basic functionality, managing variables, and combining multiple inventory sources and options for working with dynamic inventories.


Many modern environments are dynamic, cloud-based, possibly spread across multiple providers, and constantly changing. In these cases, maintaining a static list of managed nodes is time-consuming, manual, and error-prone.


Ansible has two methods to properly track and target a dynamic set of hosts: inventory plugins and inventory scripts. The official suggestion is to prefer inventory plugins that benefit from the recent updates to ansible core.


To see a list of available inventory plugins you can leverage to build dynamic inventories, you can execute ansible-doc -t inventory -l. We will look at one of them, the amazon.aws.aws_ec2, to get hosts from Amazon Web Services EC2.


In this article, we explored Ansible inventory basics and various use cases for defining groups and variables in static inventories. We also learned how to combine multiple inventory sources and an example of fetching our inventory of hosts dynamically from AWS.


This will install the necessary role files in .ansible/roles of your home directory. Once installed, you can add the Datadog Agent role to your playbook and automatically install the Agent on all matching instances pulled from your dynamic inventory.


Inventory files can either be static or dynamic. A static inventory file is an INI- or YAML- formatted list of hosts, groups, and variables, while a dynamic inventory file may simply list a region (e.g., us-east-2) to pull hosts from. A dynamic inventory will query the AWS API to get the most up-to-date list of virtual machines every time you run the playbook, which is particularly useful for environments that automatically scale up and down to reflect real-time traffic. Though we are focusing on dynamic inventory in this guide, you can read more about configuring static inventory in the Ansible documentation.


By default, the Ansible binary looks for an inventory file called hosts, located in the /etc/ansible directory. This file can also be created as a subdirectory, so that you can store multiple inventory files for Ansible to concatenate into a full inventory. This gives you the flexibility to create multiple dynamic or static files for your environment.


In the /etc/ansible/hosts/ directory, create a new dynamic inventory file. You can name the file anything you would like, but it does need to end with aws_ec2.yaml in order for it to be associated with the AWS dynamic inventory plugin:


You can create an Ansible playbook that utilizes your dynamic inventory and Datadog Agent role. Playbooks can be stored and organized anywhere you would like on your Ansible host, but Ansible also provides some best practices for organizing complex projects with multiple playbooks and inventories.


In the example above, Ansible automatically pulled an EC2 instance that matched the tagging group (datadog:yes) and the region defined in the dynamic inventory configured earlier (us-east-2). With the inventory correctly reporting, you can now run the playbook with the ansible-playbook command on your Ansible host:


However, there is an easy solution called ansible dynamic inventory. Dynamic inventory is an ansible plugin that makes an API call to AWS to get the instance information in the run time. It gives you the ec2 instance details dynamically to manage the AWS infrastructure.


If you want to use the dynamic inventory as a default Ansible inventory, edit the /etc/ansible/ansible.cfg file and search for inventory parameters under defaults. Change the inventory parameter value as shown below.


This approach becomes impractical as our inventory grows and instances launch and terminate in the cloud. For this example we will use Oracle Cloud and a dynamic inventory Python script provided as part of oci-ansible-modules project (we will not be using other features from that project).


If the location given to -i in Ansible is a directory (or as so configured in ansible.cfg), Ansible can use multiple inventory sourcesat the same time. When doing so, it is possible to mix both dynamic and statically managed inventory sources in the same ansible run. Instanthybrid cloud!


When defining groups of groups in the static inventory file, the child groupsmust also be defined in the static inventory file, or ansible will return anerror. If you want to define a static group of dynamic child groups, definethe dynamic groups as empty in the static inventory file. For example:


Inventories are necessary for running pretty much anything in Ansible. These can be simple, static lists with static groups, or they can be dynamically generated, with one or more inventory plugins to source the data. This article will detail how to build a dynamic inventory in tower, by using an ansible inventory plugin. The inventory plugin to be used in this demonstration can be found here. Specifically, the MSSQL inventory plugin, rather than the Neo4j plugin. This was written specifically to be a demonstration of how one might reference data in a SQL database for use as inventory.


Once created, it can be populated with machines. This is where a static inventory deviates from a dynamic inventory. Dynamic inventories are populated by sources. To add a source, select the SOURCES button, then click add.


In Ansible and Ansible Tower, as you know, everything starts with an inventory. There are a several methods how inventories can be created, starting from simple static definitions over importing inventory files to dynamic and smart inventories.


In Ansible, there is a static inventory and a dynamic inventory. In a cloud world, it is often the case that Virtual Machines are spinning up and down in response to business demands. In this case, static inventories that list the IP Addresses and/or host names will not serve the needs of your business. Dynamic inventories are the solution to this problem.


We are telling ansible to use the ping module (-m ping) using the dynamic inventory file (-i ansible_azure_rm.yml) where the dynamic inventory file is filtering for all VMs in the vmblog Azure Resource Group. In our case, vmblog01 is the only VM in the vmblog Azure Resource Group. Therefore, Ansible ends up pinging vmblog01.


We also discussed dynamic inventory. If theinventory file is executable, Ansible executes the file and uses the output as an inventory. The inventory format differs from static and dynamic inventory so I played a few to make both works.


It is worth mentioning that Ansible can use multiple inventory sources at the same time. It is also possible to combine static and dynamic inventories in the same ansible run. To do so you have to collect all inventory files in one directory and set that directory as ansible inventory. You can use command line option -i or inventory parameter in ansible.cfg. In such directory all executable files will be handled as dynamic inventories. Files with: , .orig, .bak, .ini, .cfg, .retry, .pyc, .pyo extensions will be ignored. You can modify that list with parameter inventory_ignore_extensions in ansible.cfg. All other files will be interpreted as static inventories. Subdirectories group_vars and host_vars will be treated in a normal way.


If you need to grant more granular permissions, this is an example for a single static inventory and single job template. This can be extended as required for additional job templates and/or inventories.


The most important part of any network automation solution is a reliable inventory. In large and complex network environments, a central DCIM like Netbox or Nautobot with dynamically generated inventories seems to be the gold standard.


Ansible tracks configuration resources by preserving lists, calledinventory lists, as simple files (also sometimes called a hostfile).These inventory lists can be static or dynamic. Dynamic lists canautomatically update when inventory resources are added, deleted, ormoved.


One possible solution is Ansible [1]. It is a widely-used and accepted automation tool, which is part of the Red Hat distribution, and it provides integration with several Red Hat tools. It uses OpenSSH for transport. Ansible needs an inventory file that contains all managed hosts. This file can be either static or dynamically generated. We recommend applying an Ansible playbook for SSSD installation and configuration, and using Ansible ad-hoc commands for troubleshooting any possible issues. 2ff7e9595c


0 views0 comments

Recent Posts

See All

Commentaires


bottom of page