Ansible’s inventory plays a crucial role in defining the infrastructure you manage. It holds information about your hosts, such as their names, variables, and IP addresses. Traditionally, this information was stored in static inventory files. But what if your infrastructure is constantly changing? Dynamic inventory offers a powerful solution.
Dynamic inventory eliminates the need for manual updates by fetching host information from various sources in real time. This ensures your Ansible playbooks always target the correct hosts, regardless of infrastructure changes.
Ansible Controller leverages two key concepts for dynamic inventory:
Inventory Plugins Rewritten
Previously, inventory plugins required separate configuration files. However, in the Ansible automation controller, you can directly provide the new-style inventory plugin configuration through the source_vars
field for supported sources. This simplifies the process for managing inventory plugins associated with:
Here’s a step-by-step guide on creating dynamic inventory using the “Sourced from Project” option in Ansible Controller:
1. Prepare Inventory File:
ini
or YAML
file, for example, demohosts.ini
, containing your host definitions. You can find a sample inventory file here.[demohosts]
node1sample
node2sample
node10sample mycutom_var=testing port=8443
[demodb]
dbsample1
dbsample2
dbsample3
dbsample4
[network]
cisco101 mycutom_var=prod
cisco102 env=dev
[web]
webserver[101:102] http_app=nginx
webserver[201:202]
[web:vars]
web_http_port = 8080
[demodb:vars]
db_port = 5432
2. Create a Project with Repository:
It’s recommended to store your inventory file in a separate Git repository for version control and as a single source of truth. Create a project within Ansible Controller that links to this repository.
3. Create/Configure Inventory:
Navigate to the Inventories section and create a new inventory or use an existing one. Adjust configurations as needed, but primarily focus on the inventory name.
4. Add Inventory Source:
demohosts.ini
).5. Sync and Verify:
demohosts.ini
file.You can also check the variables you set for hosts or groups as follows.
We have node10sample mycutom_var=testing port=8443
in our inventory file (demohosts.ini
) and see the same synced on the automation controller.
Updating Your Inventory:
demohosts.ini
) in the source (Git) and perform the following actions:
Automation Options:
Ansible Controller offers automation features to streamline updates:
By implementing dynamic inventory using “Sourced from Project,” you gain a flexible and automated approach to managing your Ansible infrastructure. Remember to adapt the configurations and best practices to suit your specific needs.
References:
Disclaimer:
The views expressed and the content shared in all published articles on this website are solely those of the respective authors, and they do not necessarily reflect the views of the author’s employer or the techbeatly platform. We strive to ensure the accuracy and validity of the content published on our website. However, we cannot guarantee the absolute correctness or completeness of the information provided. It is the responsibility of the readers and users of this website to verify the accuracy and appropriateness of any information or opinions expressed within the articles. If you come across any content that you believe to be incorrect or invalid, please contact us immediately so that we can address the issue promptly.
Gineesh Madapparambath
Gineesh Madapparambath is the founder of techbeatly and he is the co-author of The Kubernetes Bible, Second Edition. and the author of 𝗔𝗻𝘀𝗶𝗯𝗹𝗲 𝗳𝗼𝗿 𝗥𝗲𝗮𝗹-𝗟𝗶𝗳𝗲 𝗔𝘂𝘁𝗼𝗺𝗮𝘁𝗶𝗼𝗻.
He has worked as a Systems Engineer, Automation Specialist, and content author. His primary focus is on Ansible Automation, Containerisation (OpenShift & Kubernetes), and Infrastructure as Code (Terraform).
(aka Gini Gangadharan - iamgini.com)
This site uses Akismet to reduce spam. Learn how your comment data is processed.
Leave a Reply