From the course: Ansible for Automation Essential Training: Advanced Playbooks, Roles, and Diverse Hosts

Unlock this course with a free trial

Join today to access over 24,900 courses taught by industry experts.

Nesting loops

Nesting loops

- [Instructor] I have a computer science background with a good bit of programming experience, but don't get it wrong, I am no programmer, but I can play one on TV on occasion. With this in mind, I'll join the fray and say that Ansible is not a programming language. And if you insist that it is, I would say it's not a great one. What I mean is that sometimes, complex processing tasks can be a little hairy. Case in point, how does an admin perform nested loops? This is a concept that I semi-regularly employ where I'm looping through a list of data and I need to perform an additional loop over that data. In a programming language, it is a simple matter, but in Ansible it requires a little extra work. The best way to accomplish a nested loop is via a task file. It's often easier to show rather than to tell. Have a look at my playbook I've named nested-loop.yml. I'll start with the first task, which uses the shell module to grab a list of all the users on the system and saves that list to…

Contents