Swiftorial Logo
Home
Swift Lessons
Matchups
CodeSnaps
Tutorials
Career
Resources

Loops in Playbooks - Ansible Tutorial

Introduction

Ansible is a powerful IT automation tool. It can automate repetitive tasks, deploy applications, and manage configurations. One of the key features of Ansible is the ability to use loops in playbooks. Loops allow you to repeat a task multiple times with different parameters. This tutorial will guide you through the various types of loops available in Ansible playbooks.

Basic Loop

The simplest form of a loop in Ansible is the loop keyword. This allows you to iterate over a list of items.

Example:

- name: Install multiple packages
  apt:
    name: "{{ item }}"
    state: present
  loop:
    - git
    - curl
    - vim
                

This playbook will install the packages git, curl, and vim on the target hosts.

Loop with Dictionaries

You can also loop over a list of dictionaries. This is useful when you need to repeat a task with multiple parameters.

Example:

- name: Create multiple users
  user:
    name: "{{ item.name }}"
    state: present
    groups: "{{ item.groups }}"
  loop:
    - { name: 'alice', groups: 'admin' }
    - { name: 'bob', groups: 'users' }
                

This playbook will create two users, Alice and Bob, with different group memberships.

Loop Control

Ansible provides several ways to control the loop execution. You can use the loop_control keyword to modify the loop behavior.

Example:

- name: Install multiple packages with index
  apt:
    name: "{{ item }}"
    state: present
  loop:
    - git
    - curl
    - vim
  loop_control:
    index_var: loop_index
                

In this example, the index_var is used to keep track of the current index in the loop.

Nested Loops

Sometimes, you may need to use nested loops to iterate over multiple lists simultaneously.

Example:

- name: Deploy applications to multiple hosts
  hosts: "{{ item.0 }}"
  tasks:
    - name: Deploy application
      copy:
        src: "{{ item.1.src }}"
        dest: "{{ item.1.dest }}"
  with_subelements:
    - "{{ applications }}"
    - "hosts"
                

This example demonstrates how to deploy multiple applications to multiple hosts using nested loops.

Registering Variables with Loops

You can register the output of each iteration to a variable, which can then be used later in the playbook.

Example:

- name: Check disk usage
  command: df -h
  register: disk_usage
  ignore_errors: yes
  loop:
    - /
    - /home
    - /var

- name: Print disk usage
  debug:
    var: disk_usage.results
                

This playbook checks the disk usage for multiple directories and registers the output which is then printed using the debug module.

Conclusion

Loops in Ansible playbooks provide powerful capabilities to automate repetitive tasks with ease. You can loop over simple lists, dictionaries, nested lists, and even control the loop execution behavior. Understanding and utilizing loops effectively can significantly enhance the efficiency of your playbooks.