Understanding the Ansible with_items Directive

300-435 By islever Editorial Last updated

Short answer

A Iterates over list to execute task multiple times by applying the Ansible with_items loop directive.

Question

What is Ansible 'with_items' directive?
  1. Iterates over list to execute task multiple times Correct Answer
  2. Registers variable
  3. Sets fact
  4. Tags task

Key Takeaways

The Ansible with_items directive is a loop construct that iterates over a list to execute a task multiple times. Option A correctly identifies this behavior, distinguishing it from registering variables, setting facts, or tagging tasks. It implicitly defines the item variable for each iteration.

Expert Analysis

with_items runs a task for each item in a list.

The with_items directive in an Ansible playbook operates as a loop mechanism that feeds individual elements of a predefined list into a task. Option A correctly identifies this behavior, stating that it iterates over a list to execute the task multiple times. When a task includes with_items, the Ansible engine processes the associated list and runs the task once for every item in that collection, substituting the {{ item }} variable with the current element's value during each iteration.

Option B suggests that the directive registers a variable. Registering a variable in Ansible is achieved using the register keyword, which captures the output and results of a task so that subsequent tasks can evaluate its state, such as checking standard output, error messages, or return codes. The with_items directive does not capture task output; it only provides input for task execution.

Option C claims the directive sets a fact. Setting a fact is accomplished via the set_fact module, which allows a playbook to define new host-specific variables dynamically during execution. While with_items can be combined with set_fact to iterate over multiple fact assignments, the loop directive itself does not perform the fact-setting function.

Option D states that the directive tags a task. Tags in Ansible are applied using the tags keyword at the task or play level, enabling users to selectively run specific portions of a playbook via the --tags or --skip-tags command-line flags. The with_items directive has no relation to playbook tagging or execution filtering.

A key mechanism underlying with_items is its integration with Ansible's default looping structure, where the loop variable item is implicitly defined. For example, if a playbook needs to configure multiple VLAN interfaces on a Cisco IOS XE device, a list of VLAN IDs can be passed to a network module using with_items: "{{ vlans }}". During execution, the module receives the configuration command for the first VLAN, completes that task iteration, and then proceeds to the next item, effectively expanding a single task block into multiple distinct module calls. This implicit variable injection is what allows a single task definition to scale across a dynamic or static list without duplicating code.

Furthermore, the with_items lookup plugin specifically flattens nested lists one level deep before iterating. If a playbook provides a list containing sublists, with_items will extract the individual elements of those sublists rather than treating the sublist as a single entity. Modern Ansible versions often recommend the generic loop keyword over with_items for new development, but with_items retains this specific flattening behavior, a subtle but important distinction when migrating legacy network automation playbooks or troubleshooting unexpected iteration results.

Official Reference

https://docs.ansible.com/ansible/latest/playbook_guide/playbooks_loops.html

This link opens the official vendor documentation in a new tab.

Exam Strategy

When evaluating Ansible directive questions, map the keyword to its core function: with_items implies looping, register implies capturing output, set_fact implies variable creation, and tags implies execution filtering. Look for the option that explicitly describes iteration or looping behavior for with_items, and eliminate options that describe unrelated module or keyword functions.

Practice All 300-435 Questions

Access 150 questions with complete answers and detailed explanations.

View Full 300-435 Practice Test →

← Back to 300-435 Study Guide