In this tutorial, you’ll learn how to simplify your Ansible playbooks by using loops. Loops help you execute the same task multiple times by iterating over a list of items, making your playbooks more structured, maintainable, and efficient. A common scenario is creating multiple users on target systems without the need to duplicate tasks.Documentation Index
Fetch the complete documentation index at: https://notes.kodekloud.com/llms.txt
Use this file to discover all available pages before exploring further.
Basic Looping Example
Consider a traditional playbook that creates individual users by repeating nearly identical tasks:user module to be called repeatedly for each value in the list. Here’s how you can streamline the playbook:
loop directive iterates over the usernames. For each iteration, the current value is assigned to the variable item, which you reference using {{ item }}. This reduces repetition significantly and enhances readability.
Visualizing Loop Execution
To help you grasp how loops execute tasks, imagine the loop expanded manually:Looping Over an Array of Dictionaries
When your requirements include passing multiple parameters (like a username along with a user ID), you can iterate over a list of dictionaries. In each iteration, the dictionary’s keys (e.g.,name and uid) can be accessed for dynamic task creation:
The with_items Directive
Prior to the introduction of theloop directive, Ansible used with_items for iterating over lists. Although both methods produce the same result for simple use cases, it is recommended to use the newer loop directive to enhance readability and consistency in your playbooks.
For better clarity and future-proof playbooks, prefer using
loop over with_items.Other Looping Directives and Lookup Plugins
Beyond basic loops, Ansible offers several looping directives powered by lookup plugins. These plugins allow iteration over various data sources such as files, URLs, and even databases. Below are some examples:| Lookup Plugin | Use Case |
|---|---|
| with_dict | Looping over dictionary key/value pairs |
| with_etcd | Retrieving data from an etcd datastore |
| with_env | Iterating through environment variables |
| with_filetree | Looping through a directory tree |
| with_ini | Parsing INI files |
| with_inventory_hostnames | Accessing hostnames from an inventory file |
| with_k8s | Interacting with Kubernetes objects |
| with_openshift | Interacting with OpenShift |
| with_password | Generating or handling passwords |
| with_sequence | Creating a sequence of numbers |
| with_subelements | Iterating over nested lists |
Conclusion
Understanding and utilizing loops in Ansible is critical for creating efficient, scalable, and maintainable playbooks. By leveraging loops, whether simple arrays or complex dictionaries, you can significantly reduce code duplication and enhance the clarity of your automation scripts.Experiment with different looping constructs to find the best fit for your automation needs. For additional guidance, explore the Ansible Documentation.