Ascender Pro videos

Ansible list slicing and extended loop controls

Greg Sowell shares two Ansible looping tricks he found while digging for content: list slicing and extended loop controls. Working from a simple playbook with a five-item list, he shows how the bracket notation in a loop accepts start, stop, and step values separated by colons. Starting at index 1 skips a first element that is often a count rather than data, and a stop value of -1 drops the last element, both without any extra tasks.

He then enables loop_control with extended set to true, which unlocks variables such as ansible_loop.first, last, index, index0, length, nextitem, previtem, revindex, and allitems. A when conditional on ansible_loop.first reproduces the skip-first behavior, and he compares the output of debug's msg and var parameters to show how much extra information the extended variables carry. For simply counting items, he points out that the length filter does the same job as ansible_loop.length in fewer lines.

The demo is aimed at Ansible and Ascender users who iterate over dynamically retrieved lists and want more control over where loops start and stop. The demo playbook is linked in the description.

Key takeaways

  • Ansible loops accept slice notation in brackets: start, stop, and step values separated by colons, with indexes starting at zero.
  • Starting a slice at 1 skips a first element, which is useful when a returned list begins with a count instead of data.
  • A negative stop such as -1 trims from the end of the list, so the last entry is left out without extra logic.
  • Setting loop_control extended to true exposes ansible_loop variables including first, last, index, index0, length, revindex, nextitem, and previtem.
  • debug with var prints all extended loop data, while msg keeps output to exactly what you specify.
  • The length filter reports list size in fewer lines than enabling extended loop controls just for ansible_loop.length.

Questions this video answers

How do I skip the first item in an Ansible loop?

Use list slicing inside the loop reference, for example my_list[1:], which starts at index 1 and runs to the end. Alternatively enable loop_control extended and add a when condition of not ansible_loop.first, which skips the first iteration while still reporting it as skipped in the output.

What are Ansible extended loop controls?

Adding loop_control with extended set to true makes an ansible_loop variable available inside the loop. It includes first and last booleans, index and index0 counters, revindex values counting down from the end, the list length, the next and previous items, and allitems containing the whole list.

How does list slicing work in Ansible?

Slicing uses bracket notation with up to three colon-separated numbers: where to start, where to stop, and how many items to step by. Leaving a value blank uses the default, so [1:] starts at the second element and [:-1] stops one short of the end, while a step of 2 would take every other item.

About this video

Greg Sowell recently went down a rabbit hole and discovered list slicing...which allows you to start and stop at specific spots in your lists while looping.

That then led him to learn that when looping in Ansible you can enable extended loop controls that give you a bunch of extra features!

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 so today I'm going to be showing you a little bit that I learned about list slicing I was kind of trolling the internet for video fodder and I went down a rabbit hole to another Rabbit Hole to yet a third Rabbit Hole probably and uh I found something called list slicing and then from that I found out that loot control has extended options I'm going to show you those really quick and kind of how they react and and how you can maybe use them in a couple of different ways so often times I'm

going to be iterating over a list in my automations I I would say that's super common and it's not usually a hard-coded list most often it is one that I'm going to go and I'm going to retrieve somewhere I'm going to you I'm going to run some task that's going to go and pull some information create a list out of it and I'm going to iterate over it to perform some kind of operation and so you can think of a list kind of like an array essentially it's a variable that has a whole bunch of stuff that I want to you know operate against in

some way right so I want to Loop over that list right start at thing one then go to two then three and then perform these actions on it and ansible makes it really easy to do that with the loop operation uh it used to be kind of like with items and now we use Loop although with items still works um there used to be a lot of actual with things things like withd flattened and other with other things like that so most of those have kind of been Consolidated into Loop so if you are looking at some old playbooks and you ever run across those

things just realize those are the old ways they actually implemented some looping but list licing uh as demonstrated here so in this very first task that you see here well actually let's start kind of at the top you see in the VAR section I've created a VAR called my list and it consists of a b CDE e so just kind of some simple dummy data that you can iterate over that we can see what's going on here if I take a look at the very first list I'm really just doing debug on all these where I'm just spitting out information to screen um but here

View full transcriptHide full transcript

I'm using the loop option and inside of there I have a list right my list I'm referencing it but you can see I'm doing the brackets so I'm actually specifying kind of iteration information inside so often times whenever you are uh operating and you see the kind of the bracket and you have a number in there you're really designating which specific thing inside that list I put in there so if I just did uh bracket 4 bracket that would be e because it technically starts at zero right so in um a lot of programming languages the index starts at zero so the very first item

in there is zero the very next item is going to be one and then two and then three right so it kind of moves down there and I know for Lay people that's a little weird those that have done some computer science in the past that this feels familiar right so it's not so weird um but uh so generally you're just going to be specifying kind of an individual item with a number here uh we have uh options inside of there so instead of just specifying a number I can actually do number colon number colon number so the very first number is where I'm going

to start colon the next number is where I want to stop colon after that that it's how many numbers I'm going to skip uh as I'm moving through so by default it's one which means I'll go from one to two to three to four if it was say set to two I would go uh 0 2 4 6 8 right it'll count by two if I had the number set as three on the index and you're kind of that that third column there uh it would it would count by threes um I can't think of too many instances where I would want to use that

I'm sure it exists for a reason uh so by default I usually just leave that here but in this uh example right here say for your uh say for example I did want to um I said example too many times there now say for example I did want to uh skip the very first item which happens sometimes right so sometimes whenever you return information it'll actually the very first entry will be kind of uh say for example uh the count of how many things list in this array so say it's you know like for example here say zero would be uh five because there's like

technically five things in there and then it would start at one for a and then B right so if you're pulling information it returns that way and I want to skip that very first entry I could do it like this so I can say start at one and then colon nothing that means just continue on all the way to the end there's no finish um right just which is all the way to the end and also I don't set an index count so really we're just going to start at one and we'll move our way through so if I take a look at my example

here and I skip over to my output so this should be my slicing output you could see right here we did B CDE e so we skipped a right so it said essentially start at uh iteration uh one right because really we start at zero so start at number one and then move on the way down through there um so there have been instances again where you want to skip the very first entry this is a really succinct way of doing that right if you kind of look at it there's not a whole lot to it there really I'm just very compactly grabbing that information

now say for example and this has been real

life I've had to do this before if you look at my second example it looks almost the same except the very first start column I leave it blank so I I want it to start at the very beginning right like item zero index zero start there um and in the second column I wanted to to not go to the very last one I want it to be one before it and so I actually did minus one I couldn't find any documentation but I tried it out because this actually works in a lot of other programming languages and sure enough it'll take the very last entry

which would have been four and just subtract one so three and so it's essentially saying stop at once so if I wanted to skip the very last two I could say minus two right so it'll take that very last index and Boop move over a couple of spots and if we take a look at the output right right here you should see it showing uh a b c d we're leaving off e right so that very last entry isn't getting put in there so again really quick ways of um skipping initial entries skipping in entries I could put a static number in the beginning static

number at the end and we'll always read kind of that chunk of information in there so I don't know that it's always going to be useful for you but I thought it was actually pretty cool pretty pretty clever way of doing that now I know there are some other ways of accomplishing the exact same stuff so I thought I would show you the example here uh in this task I'm using a conditional in here um I'm doing a debug again I'm looping over my list only I'm enabling uh loop control extended so there are some additional extended options which I until I was kind of

looking through this stuff didn't realize existed but you had to turn them on for them to be available right and so you add the loop control option down at the bottom and then extended true that cranks that stuff on and now it's available to you so anore Loop you see in the conditional section so when the anable loop do first essentially it will tell you if this is the very first time you're running this Loop do first will equal to True uh if it's not it'll equal to false and so here I'm just saying when it's not the very first run go ahead and spit

out the information right spit it out to screen uh so essentially it's operating the exact same way as the first one up there a couple of more options of course there are extra things that are available to you in the uh extended options I'm going to show you those here in a minute but let's pop back to the output and you should see uh so that was what it looked like when we ran it the very first time right with the list slicing so whenever we are using the output uh with the extended stuff right here you can see uh we're liing through it says

skipping for item a because remember we did a conditional anytime a conditional the condition isn't met it just says skipping right doesn't do okay or change skipping is what you're expecting and then you can see B CDE e so on the first ones there was no skipping option right because it essentially removed that from the list of things we were going to operate over here it's still going to operate over the entire list except doesn't meet the condition so it skips so it performed exactly what we wanted it to do um just in a different way so taking a look here I was using the

message option right on the uh the debug uh module and so the debug module has a couple of uh parameters you can put in there you can do message or you can do VAR message allows you to craft a message have it show up exactly as you want I noticed and I often use especially when I'm like debugging information I'm just want to spit out the contents of a variable so I can see what's going on if you use VAR it actually spits out a whole bunch of extra stuff it shows you all the extended uh options as well like all those show up in

the message and I didn't necessarily want that because it's not as clean um but I did show it to you on this second one right so it's configured identically only it's using the VAR option and when you use the VAR option it spits a lot more out to screen so let me move down here so this is the loop control with the VAR option you can see down at the very bottom it's performing the exact same thing remember we're we're skipping the first thing uh in the list you can see the skipping section here so whenever we actually are going to show it it's going

to uh just be Item B so if you look at the very bottom of that output you can see Item B which is the very first thing in our iteration that we're actually going to operate against it shows up there but above it you can see all of the additional stuff that it collecting here so all items there's a uh analore loop. all items and it'll show you everything that's in there comma separated I guess that could be useful in some respects if you wanted to uh to use that only imagine that all items is 50 entries or 100 entries this outputs can get a

little bit messy right not that that necessarily matters because if I'm building a message and I'm spitting it out specifically or say I'm building a report and I'm just dumping out the specific item contents that'll go there it's just the VAR is showing all this stuff here but then we also in those extended Loop options we have uh anore loop. right that's again the true false condition that shows you uh is this the first one so now notice you have index and index zero so remember how I told you that indexes generally start at zero and our Loops do that here in anible um index

zero is going to be in that format and so index is how many times have we made our way through the list right are we on the the first one or we on the second time or we on the third time right it kind of gives you that indication so index zero is uh assuming that the index when it very first runs starts at zero and then goes one then two that's going to show you the output there now if you wanted the index to be um the very first iteration is number one the very second iteration is number two then that's just standard index

I know that's a little counterintuitive since the default is index zero you would think that index would be the default but they've kind of mix-matched it here um that is fairly useful because there have been multiple instances where I do a conditional I'm looking for some piece of information I need to know which uh order in the list so in my conditional I would say hey when you know the contents equals whatever and then I would check the analore loop.

index or index zero whichever is pertinent to what I'm trying to do here uh so if I did last that's going to be telling me is this the very last one in the uh the list right true false length it's going to show me the overall length of uh this uh list right so there's five things in there you see it says uh five tells you what the next item is what the previous item is and then uh reverse index uh is going to be from the end how far or rather how close are we to the very end right so it's kind of um

doing the opposite direction right the regular index counts up the reverse index is kind of counting down how close are we to the end and so they have the index zero version as well as the regular index so extra information that can be useful I don't know that you're always going to be able to use this stuff but uh if you have run into a situation where you might need to do some extra processing or find that information guess what you've got an option for it there now keeping that in mind I was thinking well you know the length of things uh can be used pretty commonly and so I have one here that's just spitting out the uh the length options you can see anore loop.

length but below it I wanted to show you another way that you can check a list so while the loop control extended stuff is interesting it does kind of add some extra lines you have to add in there and I am pretty lazy when it comes to this stuff so in the very bottom one you can see I can do the exact same thing um with a couple of lines less right there's a fewer lines and so I'm using the length plugin so I'm taking my list pipe that means I'm moving this to a plugin and so just tell me the length of this list

right here and so it should theoretically accomplish the same thing so if I skip to the very bottom here I can see uh right here we've got a message that's showing uh the uh what was it Loop uncore no analore Loop right yeah analore loop. link is showing that right here oh let me scroll back down uh as five and then the other one is using the length plugin to uh display that the uh the list is actually five so you get the same information so I don't know that you're going to be able to use this super commonly but I thought it was like

kind of a cool trick cool tip I actually found and learned several things which I love doing um honestly as soon as I learn something it's going to fall out of my head so this documentation is really a way for me to kind of come back and look at it so I hope you enjoy this if you find it useful in any way um I would be curious to hear how you actually would use it um if you would tweak or tune this modify it let me know about that I love feedback I love hearing uh what you guys are interested in guys and gals

and everybody in between if you would love to see a different video I am always looking for fodder give me materials to to make this stuff so tell me what you're looking to do uh if there is nothing else uh thanks for watching happy automating happy slicing 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