When managing infrastructure, there are times when a dynamic inventory is essential. Kubernetes is a perfect example of this where you may create multiple applications within a namespace but you will not be able to create a static inventory due to Kubernetes appending a systems-generated string to uniquely identify objects.
Recently, I decided to play with using a Kubernetes dynamic inventory to manage pods, but finding the details on how to use and apply it was a bit scarce. As such, I wanted to write a quick start guide on how you can create an Ansible Playbook to retrieve your pods within a namespace and generate a Kubernetes dynamic inventory.
This is much easier to do when you take advantage of the kubernetes.core.k8s_info
module.
In my example, I’m going to take advantage of using my existing ansible-automation-platform
namespace that has a list of pods to create my dynamic inventory. In your scenario, you’d apply this to any namespace you wish to capture a pod inventory from.
When creating your inventory, the first step is to register the pods found within a particular namespace. Here’s an example of a task creating an inventory within the ansible-automation-platform
namespace:
While this task would capture every pod within that namespace, you could further target specific pods using the label_selectors
as such:
This would target only the automation controller pods from within my ansible-automation-platform
namespace.
Once the pods are registered, take your list and use the add_host
module to dynamically add those pods to your inventory. One key piece from this add_host
module is that I’ve included the ansible_kubectl_namespace
var within the task. When adding pods to your host list, you need to provide their namespace, because without adding this variable it assumes the namespace is the default namespace.
NOTE: If you want to use kubernetes connection plugin parameters, you need to prefix ansible_
to them. In this example, I used the kubectl_namespace
and prefixed ansible to get ansible_kubectl_namespace
.
And there you have it! Now your pods have been added to your inventory dynamically in a group labeled “pods”.
Now with your dynamically built inventory, you can run tasks against this pod inventory. Since this inventory is built at run-time, you’d want to have a playbook with multi plays similar to this example:
NOTE: In the second play labeled "New Play using the pod inventory" there is a connection: kubernetes.core.kubectl
. This allows for the run of command on the pods and for that you need this connection plugin.
And there it is! One method of creating an inventory of your pods that you can then run tasks against.
What can I do next?
Whether you are beginning your automation journey or are a seasoned veteran, there are a variety of resources to enhance your automation knowledge:
- AnsibleFest 2022 - Come hang out with myself, and more importantly the automation mesh guru Craig Brandt, my friend and colleague who knows everything there is to know about automation mesh in Chicago October 18-19, 2022.
- Self-paced exercises - Check out our interactive, in-browser exercises to learn and dive into Ansible Automation Platform
- Subscribe to the Red Hat Ansible Automation Platform YouTube channel. Be sure to check out our web series, Automated Live hosted by Colin McNaughton.
- Follow Red Hat Ansible Automation Platform on Twitter and myself @Lopez - Do you have questions or an automation project you want to show off? Tweet at us!
About the author
Browse by channel
Automation
The latest on IT automation for tech, teams, and environments
Artificial intelligence
Updates on the platforms that free customers to run AI workloads anywhere
Open hybrid cloud
Explore how we build a more flexible future with hybrid cloud
Security
The latest on how we reduce risks across environments and technologies
Edge computing
Updates on the platforms that simplify operations at the edge
Infrastructure
The latest on the world’s leading enterprise Linux platform
Applications
Inside our solutions to the toughest application challenges
Original shows
Entertaining stories from the makers and leaders in enterprise tech
Products
- Red Hat Enterprise Linux
- Red Hat OpenShift
- Red Hat Ansible Automation Platform
- Cloud services
- See all products
Tools
- Training and certification
- My account
- Customer support
- Developer resources
- Find a partner
- Red Hat Ecosystem Catalog
- Red Hat value calculator
- Documentation
Try, buy, & sell
Communicate
About Red Hat
We’re the world’s leading provider of enterprise open source solutions—including Linux, cloud, container, and Kubernetes. We deliver hardened solutions that make it easier for enterprises to work across platforms and environments, from the core datacenter to the network edge.
Select a language
Red Hat legal and privacy links
- About Red Hat
- Jobs
- Events
- Locations
- Contact Red Hat
- Red Hat Blog
- Diversity, equity, and inclusion
- Cool Stuff Store
- Red Hat Summit