Custom dynamic inventory script in Ascender
Most organizations already keep host data in a CMDB or similar system. When there is no off-the-shelf inventory plugin for it, a custom dynamic inventory script can pull that data into Ascender. Greg Sowell shows the full loop: a Python script kept in a public Git repository, an Ascender project that pulls it in, an empty inventory, and a source of type sourced from a project that points at custom_inventory.py. A sync then populates five hosts complete with IP address, status, OS and owner as host variables.
The video explains the source options that matter: overwrite replaces manually added hosts and variables with what the script returns, overwrite variables does the same per host, and update on launch runs the script before any job that uses the inventory so hosts are always fresh. The script itself is broken down line by line: it builds the inventory structure, loops through hosts, sets host vars, and creates groups keyed on the OS field so Windows and Linux hosts inherit group-level connection settings automatically.
For contrast, Greg pastes the same JSON payload into ChatGPT and gets a workable first draft that groups by active and inactive status, then explains why he prefers his own template. Any reachable data source, API or file, can feed an inventory, and grouping by owner, location or OS is a small change.
Key takeaways
- A custom dynamic inventory script lives in Git, is pulled in through an Ascender project, and is attached to an inventory under Sources.
- Overwrite wipes manually added hosts and variables on every sync, so understand what will be replaced before enabling it.
- Update on launch runs the inventory script before any job using that inventory, ensuring the freshest host list.
- The demo script adds every host to all, attaches all returned fields as host vars, and creates a group per OS value.
- Grouping hosts by OS lets Windows hosts inherit WinRM connection settings from the group as they are dynamically added.
- ChatGPT produced a usable first-pass script from the JSON payload, though Greg prefers his own reusable template.
Questions this video answers
How do you add a custom dynamic inventory script to Ascender?
Commit the script to a Git repository and create an Ascender project for it. Create an inventory, open its Sources tab, add a source with type sourced from a project, pick the project and the script file, then save and sync. Job output for the sync appears in the jobs menu for troubleshooting.
What do the overwrite and update on launch options do on an inventory source?
Overwrite replaces the inventory contents with whatever the script returns on each sync, removing manually added hosts and variables. Overwrite variables does the same for per-host variables. Update on launch makes Ascender run the script before any automation that uses the inventory so hosts are current.
Does the inventory script have to call an API?
No. The demo parses a JSON file in the repository, but the source can be an API, a file another system produces, or anything the script can reach. As long as the data can be read and parsed, the script can turn it into hosts, host vars and groups.
This video is part of the Ascender Pro playlist. Browse every CIQ video by product and topic.
Transcript
hey everybody I'm Greg Sowell and I'm here with another ciq demo so today I'm going to be showing you how to do custom Dynamic inventory sources inside of Ascender so what exactly does that mean you probably have some cmdb right configuration management database some system somewhere that stores all of your information about your host whether it be your networking stuff or your uh server infrastructure or security whatever happens to be you probably have some system out there that's maintaining that and if there's not something off the shelf there is a way for you to build a custom Dynamic inventory script to pull that
stuff in so let me show you mine really quick here is mine and it is written in Python and I'm going to actually go over this in a little bit more detail momentarily but first I want to show you once you actually have it created how simple it is to get inside of Ascender and start actually pulling those hosts in so first you add your uh Python scripts to a get repository right some kind of SCM you add a project for it so in my Ascender I named it Dynamic custom inventory demo and and my script is going to be in a public G
repository so you'll have access to all of this as well so if you want to kind of uh pick uh up from mine and kind of tweak and tune uh feel free to do that once you have your project created that's going to pull in that python script into Ascender we create an inventory here I have one called dust custom rather Dynamic custom inventory demo so I'm going to pop into that as you can see I don't have any hosts I don't have any groups right it's just all I did was create it I clicked add and then gave it a name and click
save uh nothing crazy there so it's pretty much empty right now well how do I actually attach a dynamic inventory script to this inventory i do it via the sources tab right here so in sources I click add I give it a name I'm going to call it uh Json parser because that's what I'm doing I'm returning a Json payload and I'm going to be parsing it so choose a source sourced from a project I'm going to choose the project click the little next button there got my Dynamic custom inventory script remember project points to my get repository once I do that I'm going to choose my inventory file and it's custom inventory.
View full transcriptHide full transcript
py after that I can click save and I'm done now while it's saving I'm going to go ahead and uh click edit because I wanted to talk about a couple of options you actually have in here so overwrite this is basically every time it synchronizes where it's going to run the script and pull stuff in it's basically going to wipe out what you have in there and replace it with what is returned so if you have some host that you've manually added in there or any variables or things like that those are all going to get blown away and the contents of whatever is returned
from the script is actually going to get put in place right so if you're going to check that be sure you know what you're kind of doing there and remember you have the check marks right here override variables this is just going to for the individual host it will wipe any variables that are in there and replace them uh via what's pulled from pulled from that script update on launch this one can actually be pretty useful too what this tells a sinder to do is anytime any piece of automation is launched that's using this inventory pause first and fire this Dynamic inventory script right so
it'll go and it'll pull and make sure you've got all those freshest versions of those hosts that's actually pretty useful in most regards so once you have that in there you can click sync and you can see its job status is running and it actually pops into the job menu so while you're developing this if you run into any error or you need to kind of see what's going on you can actually click on running right here it'll take you to the job menu and you can see the output of the run so if you do run into errors they're going to show up right
here but it looks like it successfully synced and I can tell you rather I can show you this is the Json that it's pulling from I've got five hosts in here this Json payload you can see they have a host name IP addresses status OS owner it's got all kinds of various information for all five of these hosts so let me see if it successfully pulled that in I can check by going into inventory go back into my Dynamic custom inventory inventory uh now let's take a look at the host tab all right all five of my host actually got pulled in and that's great
and I can click on them and I can see all this additional information right all those extra variables were kind of pulled in which I think is really useful but if I take a look at just the host tab right here I can see related groups I actually augmented my script so that it would dynamically add my host into groups based on what OS was set for them there so you can imagine that can be extremely useful if I'm doing it for Server infrastructure right I know what's Linux or Windows or I could say maybe Rocky or yuntu or whatever it happens to be in
the networking world I could say oh this thing is um Cisco iOS or Cisco iOS XR right or this is Arista right so you can really start kind of channeling those things into different groups well those groups could have varying connection information so for Windows host you're going to use winrm you're going to have uh not a whole host but a handful of settings specific to those windows hosts but you could add it to the group so that way as hosts dynamically get pulled into your inventory they're going to inherit those specific settings right makes things very convenient now let's break down this script just
a little bit more so this really and truly in your environment is going to be something that's pulled I just kind of artificially made up some dummy data this isn't actually pulling from a cmdb you can see I'm keeping it here in my git repository so I'm actually parsing it from file I'm not actually connecting to a a source and pulling it it just makes my testing in demo just a little bit cleaner in your environment again you're probably going to be pulling from an API or something like that but it doesn't necessarily have to be an API you're connecting to as long as you
can get to that data right as long as you can pull it you can access it in some format whether you're dynamically connecting and pulling it right now or some system is pulling it and sticking into some kind of file format as long as you can get to that you can parse it with your script and you can pull it into your inventory so I've got simple list of hosts living under the host variable I've got all these hosts um with all their Associated information so let me pull into my script now I'm not going to pretend to be a python developer I'm not going
to pretend like I even put that much effort in here what I actually did was I went into chat GPT and I asked it to parse uh this information for me and I'll show you exactly what I did I copied all the information so I have this dummy Json payload copy it I'm going to go into chat GPT and I will say create a custom dynamic IC inventory scripts for anable to process following Json payload um how about as if it's being returned from an API in reality I'm just doing it via file I'm going to paste in that Json payload now as you can
see it didn't nail it right off uh right off the blocks it went ahead and stuck that Json payload in here uh in this section and I can uh ask it to um to clarify this a little bit or you know give me kind of a standard hey pull this information in but it should still be able to digest all this so it's setting up my inventory section which is cre uh you can see it's decided to do active and inactive host I can modify that a little bit if I want to but here's the core of it right it's actually looping through the host
data right that host variable and it's saving it as host it's grabbing all that information it's adding it to the various inventory pieces and it is sticking them into active or static sections so or uh active or inactive I should say now mine does something very similarly right so I could keep throwing stuff at chat GPT and asking it to rewrite it for me um but I've actually got a pretty decent template that I like uh that I'm building from so right here again the intro sections are all pretty much going to look the same for these I'm setting up my anible inventory right just
kind of that data structure for that variable right there now I'm creating um a variable called OS group so that is a dictionary that I'm initializing because I'm actually going to be grouping things by OS group right like OS type so I'm setting a variable kind of null one or not null but just an empty variable for that next I'm going to be looping through the host and I'm grabbing all of the information and I'm just kind of sticking it arbitrary value or variables um next I am adding uh all of the host to the all group and then I'm setting up the host bars
and so remember a host bar section is whenever you're in an inventory like right here and I click on an individual host all these variables down here these are the host bars so I'm really just setting up kind of what variables are going to get attached to every individual host as it gets saved into the inventory so I'm setting all that up adding that in and then right here I'm actually looping through and I'm processing for my groups so right here I say hey if the OS that's in the um the OS variable section here if it doesn't already exist in my dictionary go ahead
and add it really quick that's what it's doing right there nothing too crazy with that next it's saying okay for this individual host go ahead and stick it in a group of that OS so Linux or Windows and my example after that it just basically saves and then closes everything out and that's it right so I could be pulling in thousands of hosts I could having them uh dynamically group based on their inventory type if I wanted to or rather OS type I guess in this example um I could also remember I could change any of this information cuz really I'm just grouping based on
one of the variables that's returned so if I take a look right here I have OS I could also be grouping based on owner or I could be grouping based on status or if this was a Cisco device I could be grouping based on OS type right or location I could do group location based on what data center this stuff happens to be what geographic region any of that information right is fair game for grouping as well as sticking into the host far section and I'm a big fan of putting more information in the host of ours because you never know when you might want
to use it right it's it's not usually a problem to have more information oftentimes you run into problems when you have less information so this is just a kind of quick and dirty example of how you would create a custom Dynamic inventory Source if you have any questions or comments I would love to hear them how you would tweak this for your environment how you would tune it uh maybe how you've done interesting things like this in your environment and maybe from some unexpected sources I would love to hear about that too um if nothing else happy automating happy inventorying happy Dynamic scripting and we'll
see you next time bye
Built for scale. Chosen by the world’s best.
2.75M+
Rocky Linux instances
Being used world wide
90%
Of fortune 100 companies
Use CIQ supported technologies
250k
Avg. monthly downloads
Rocky Linux
9
Enterprise products
Spanning the kernel to the orchestrator
Have questions about your infrastructure?
Talk to a CIQ engineer about Rocky Linux, HPC, and AI infrastructure.
