Ansible loops
Ansible provides a number of loops in the playbook, such as standard loops, looping over files, sub-elements, do-until, and many more. In this section, we will look at two of the most commonly used loop forms: standard loops and looping over hash values.
Standard loops
Standard loops in playbooks are often used to easily perform similar tasks multiple times. The syntax for standard loops is very easy: the {{ item }} variable is the placeholder looping over the with_items
list. In our next example, chapter5_4.yml
, we will loop over the items in the with_items
list with the echo command from our localhost.
$ cat chapter5_4.yml
---
- name: Echo Loop Items
hosts: "localhost"
gather_facts: false
tasks:
- name: echo loop items
command: echo "{{ item }}"
with_items:
- 'r1'
- 'r2'
- 'r3'
- 'r4'
- 'r5&apos...