Ascender Pro videos

Constructed inventories in Ascender and AWX

Greg Sowell walks through constructed inventories, a newer feature in Ascender and AWX that builds a new inventory from one or more existing ones using filters. He starts with two source inventories whose hosts carry variables such as status and location, then contrasts constructed inventories with smart inventories, which rely on a fiddly smart host filter and, crucially, drop all group information.

Creating a constructed inventory means picking the input inventories, then filling in a source vars block that names the constructed plugin, sets strict to true for clearer failures, and defines groups from host variables using Jinja expressions, with a default filter to handle missing values. The documented approach creates separate groups such as is_active and in_dallas and combines them with an ampersand in the limit field; Greg prefers a single constructed group with all conditions and a limit that always points at it.

After syncing, the resulting inventory contains the matching hosts along with every group and group variable inherited from the sources, including connection settings such as WinRM. A final example uses the compose section to build a custom variable from ansible_host, handy when a source inventory delivers an IP under an unexpected name.

Key takeaways

  • Constructed inventories build a new inventory from multiple input inventories, and unlike smart inventories they carry over groups and group variables.
  • The source vars block declares plugin constructed and strict true, so matching errors fail the sync instead of silently dropping hosts.
  • Groups are defined from host variables with Jinja expressions, and the default filter supplies a value when a variable is missing.
  • The documented method creates separate groups such as is_active and in_dallas and combines them with an ampersand in the limit field.
  • Greg's preferred pattern puts every condition into one constructed group and always sets the limit to that group for reusable configuration.
  • The compose section builds custom variables, such as special_ip from ansible_host, useful when source data arrives under a different variable name.

Questions this video answers

What is the difference between a smart inventory and a constructed inventory in AWX or Ascender?

Smart inventories filter hosts with a single smart host filter that is awkward to build, and they do not bring over groups. Constructed inventories take one or more input inventories, let you define groups and filters in a source vars block plus a limit, and import all existing groups and group variables from the sources.

How do you filter hosts into a constructed inventory?

In the source vars block, set plugin to constructed and strict to true, then define groups with Jinja conditions on host variables such as status and location. Use the limit field to choose which groups end up in the inventory, either combining groups with an ampersand or pointing at a single group that holds all your conditions.

Can a constructed inventory create new host variables?

Yes. Add a compose section to the source vars and define a variable name with an expression, for example building special_ip from ansible_host with a default. This is useful when an inventory source provides an IP address under an unexpected variable and you need it as ansible_host or another name.

About this video

Greg Sowell walks through a new feature to Ascender/AWX, Constructed Inventories. Take multiple existing inventories and build new ones with advanced filtering...oh yeah, and it brings in existing 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 welcome to another ciq demo today I'm going to be showing you how to use constructed inventories inside of Ascender so this is actually a really cool new feature for inventories that honestly has been needed for quite a little while and so let me kind of break down how the documentation says you should do it how I kind of prefer to do it it's a comparison to Smart inventories right so constructed versus smart and then maybe a couple of interesting little tidbits in there so let's start by taking a look at a couple of existing inventories I have

right so constructing smart inventories really what they do is they take existing inventories you have and they'll create new inventories from it right so you can kind of extract information out and build new inventories which can be exceptionally useful in various scenarios and I've already talked to some customers that are very successfully using these so in this example I have the dynamic custom inventory right here and I'll go to the host section you can see I have uh one through 10 so host one.

example.com through 10 and it pulls all kinds of information you can see over here on the Related Group section I have some that are Linux well what I'm having them do is automatically sort into groups right so the Related Group section those are going to be groups that they were automatically added to uh as they were imported and next you'll see another group for the the data center they are actually a member of so you have the Virginia data center one or you have the Dallas data center one now if I go into this host individually I can see that I've got a whole bunch

of additional information in here right a bunch of variable IED stuff that exists like uh uh custom variables for these some are magic variables so that anore host is a magic variable right one that's known to anible has a specific purpose the rest are just ones that I've created right so you can also see under here I've got location uh Dallas dc1 so it's actually got a location variable for each one of the hosts in this inventory that designate where it's from I've also got a status here whether it's active or inactive so coming up I'm going to be using status and location in these

View full transcriptHide full transcript

inventories when filtering these things out I've also got a second inventory here that's just my generic host inventory because I wanted to just show you how you can actually pull from multiple uh various inventories multiple inventory sources filter those into a constructed inventory so here it's host 44. example.com you can see it's got a bunch of varialed information it's also got the status and it's got the location so I mentioned smart inventories earlier and so what are smart inventories here's an example of a Smart inventory if I go to the details section I like to click edit what it allows you to do is create

smart host filters so constructed actually operates completely differently it uses uh two fields uh instead of just one so it actually has a limit and then I think a custom variable section I believe is what they refer to it as but in here you create a smart host filter and you can do some filtering it is I'm not going to mince words here it's a pain in the butt to actually do um whenever you're creating your filter you like choose the various pieces the and the ores uh and you try and narrow it down there and then once you get that completed you click select

and it will put it in there and then once you save it it will actually show you the total number of hosts and that's great and the hosts are there and available and you can see that it brings over their variables which is nice the one thing it doesn't do well one of the things it doesn't do is if you look up here there is no groups tabs so all that group information that you tend to uh rely on inside of your standard inventories aren't available in smart inventories right so that completely goes out the window so we're not going to use a Smart inventory

instead we're going to do a constructed inventory so I'm going to go into my inventory section here I'm going to click add and constructed inventory from the list next I'm going to give it a name I'm going to call it constructed demo and in here I have to put input inventory so which inventories do I actually want to pull from I'm going to be pulling from the dynamic custom and the generic inventory you can see it automatically pops them up into the list there I'm going to say select and then I'm going to move down I have a little bit of document ation I can expand here but we're too lazy to read all

that because this is a video demo so I'm going to just be talking about it really quick we have the limit option and the source VAR section so in the source VAR section I have a chunk of data that I'm going to copy and paste in there and then I'll tell you all about it so first and foremost you have plug-in constructed since this is a constructed inventory I have to tell it hey use the constructed inventory plugin right there strict can either be true or false when it's true if there's an error in my matching or in my groups or anything like that it'll

actually fail so it won't uh it won't parse and pull the new host it'll actually give a failed message so it's kind of good for I would say troubleshooting that way you don't have unexpected results in there if I put strict as false and there's an error it just kind of quietly ignores it and we'll go ahead and add uh host to the inventory is best it can so it it does what it can but it's not going to give you any warning so I would say go ahead and probably start with the truth uh on the strict method just for troubleshooting purposes in case

there's unexpected results you'll see why right you're going to get a failed message in there so under the group section here I have two options so I am actually creating these groups so whenever I do is active colon and then any information I'm actually creating that group and I'm doing it based on what I have under here so if you recall in our variables we have let me expand that a little bit I have status active and location Dallas dc1 for this host right so I'm going to be first filtering on status so if I pop into here I've got status colon default so what

I'm really doing is I'm doing a Ginga operation where I'm piping that and I'm saying default so if the variable doesn't exist right then go ahead and make its default setting inactive and so essentially you can ignore that uh that pipe default portion to really get the message that I'm looking for which is status equal active I want to find things that are active so if there's nothing there like that variable doesn't exist instead of aor out and failing so really this is kind of error handling is what I'm baking in here with this so if there is nothing there it'll just show up as

inactive equals Stat or equals active and so it'll fail it'll fail in that response right so it won't match there um and then uh if it already has a definition and it is set to inactive it won't matter match there either right because inactive doesn't equal active but if by chance it does say active then it will match and it will get added to that group you can say the uh the same sort of thing is happening for uh the Dallas one right here where I'm looking at the location so I'm just creating a group named inore Dallas and I'm saying if your location equals

Dallas dc1 go ahead and add it to that group as well so this is the method that they're actually recommending in the documentation this works so by default what I'm doing is I'm adding it uh the hosts that are uh active they're getting act uh added to this is active group and then the Dallas hosts are getting added to that so now I have two groups that they've gotten added to um but it's not a combination of the two here right I'm just saying hey if you happen to be active so this is going to pick up all the Virginia uh assets as well right

stuff that's in the Virginia data center that happens to be active that'll get added into that group well as also down in the uh in Dallas one it's going to be adding inactive ones so technically it's not exactly in the format we want right it's created these two groups so now we have to kind of stitch those together and we're going to do it in the limit section so think of the limit section kind of like a limit in a regular job template where I'm just defining what hosts I want to uh kind of limit down and operate against so in here I'm using the

Amper sand characters for these two groups so if it is active and also in uh in Dallas so if it happens to be in both of those groups then go ahead and add it to the inventory so I'm going to click save here and it doesn't synchronize by default so I'm going to go ahead and click sync and so we should watch this total number of hosts as the job runs and it should update momentarily there you go so now we have our for host added so it really did it created a a big group of everybody active big group of everybody in Dallas and

then it cross referenced the two with the limit option and said hey if you happen to be in both of those groups and it pulled that subset of information out so that's good and it works fine here's another example that does the exact same thing so in here I'm going to expand this a little bit so that you could see my uh group section here so I'm using the plugin constructed strict true again only in groups I'm creating a group named constructed and I'm saying hey status equals active and right you see that that operator there and the location is also in Dallas so the

only hosts that are even making it through uh through my filtering here are going to be the ones that meet all of my criteria they're going to go into constructed done and then uh scroll down and hit edit it's so much easier for me to look at this in the edit menu you can see that the uh limit option here I've actually got it set to the con constructed group so what this means is I can kind of you reuse this sort of thinking for all of my constructed inventory so I could really have my uh conditional in there of like all the things I

need add them to the Constructor group and then always have the limit set to construct it that way it will just add that one group so it kind of knocks down the number of things I have to kind of keep track of these things aren't as much snowflakes right uh a lot of the um a lot of this uh configuration is just copy and paste I can always have the limit there set to constructed um and I can always have the group that it's creating be just the constructed group again this is kind of my preference on how to do it it's not necessarily something

that um there's no wrong or right to this I would say necessarily this is just how I like to do it to me it makes a little bit more sense to my brain um than having erroneous information like come in here and then I have to use the limit to filter it down I'm sure there's a situation where it makes sense to do that if potentially you wanted to uh do some troubleshooting you could adjust the limit on the old one and be able to see information from both those Divergent groups maybe that would help in that respect but for me this just keeps a

little bit more straightforward and simple and again it's going to return the exact same information so it's not like there's a difference in those two it's just two ways of getting the exact same info but I'm going to pop into the host section you can see it's captured the uh four from the various areas you can see my Lo location here is Dallas and my status happens to be active as well right so it meets that criteria and it's got all variabil ased information along with it but something of Interest here is if I go to the group section all of the groups that existed

from those individual inventories now get imported in with that data right so I can actually click on Linux and go to hosts and I can actually see the uh members that were in those Linux host groups right so all that group information gets brought over and it's not just the group information it's also any variabl ased info that was associated with that group in the other inventory right so theoretically on the Windows group I probably have Windows connection information in there right like I'm setting up winrm uh variable IED information to be able to kind of connect in and so all that info gets pulled

in with it right so all the important bits that I'd put in those groups with uh with uh the variables and stuff like that that's getting pulled into so not just the individual host with their variables but also the uh variab IED info inside the group so this is one of the powerful things of constructed inventories it's kind of all of the best parts of a Smart inventory only it's more granular it's like so much easier for me to build the filters in here to get the information I want but now I also get groups as well so let's look at some other interesting information

on this one one I'm going hit the edit button cuz again I just like to look at it a little bit easier this way you can see I am uh picking the exact same inventories as my inventory sources you can see in here I'm using the Linux group as a limit so only stuff that happens to be in the Linux group from either one of those inventories in here you can see I'm calling plug-in constructed but nothing else in there I'm not creating any special groups I'm not doing any filtering I'm really just saying hey if it happens to be in this one group just

pull it over here so again it's like a nice kind of easy way to pull that extra information in and filter things out so you can see I have hosts in here I'm going to have a lot more hosts because I'm just looking for stuff that happen to to live in the uh the Linux group and then again groups it's pulling all of the grouped information again right so again all the power of pulling in those groups with its configuration and I thought I would show you one extra little thing on this demo 4 and is how you can actually build some variables so not

only can you build groups but I can build custom variables as well so if I add a composed section like you can see right down here I created some just random custom variable named it special IP and I'm pulling the analore host portion and I'm sticking that in as the IP address you can also see that I'm adding a default in there so if there isn't an un uh anore host specified I'm putting a piece of default information in there now I put this specifically in here because I have pulled from random inventory sources before where I was getting uh kind of gibberish information out

but somewhere in there it actually gave the IP address now this host wasn't fully qualified so I couldn't reach it and I needed to create an analore host so this really in this composed section I could make it analore host colon and then whatever variable IED field brings over that IP address like if it's just something like hostor IP or um ec2 IP I mean you get the idea right like if you have a piece of information that you need it to be in a different variable name maybe a magic variable name it's a really easy way to kind of build it in the composed

section so you can see I'm making special IP for anore host let me look in the host section here let me grab one of my hosts out expand its variables and you can see right here special IP it's created that variable right just using the uh IP address that was in anore host so another kind of cool little feature uh that you can add into your uh constructed inventories hopefully this uh inspires you to do some interesting stuff with constructed inventories if you have any questions or comments I would love to see them if you would like any help of this I'd love for you

to reach out to us we love doing Ascender support out there migration support on installation configuration all that good stuff and I love talking to customers so happy automating happy uh inventory constructing 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.

Talk to an Expert