Swiftorial Logo
Home
Swift Lessons
Matchups
CodeSnaps
Tutorials
Career
Resources

Loops in Playbooks

Introduction to Loops

In CrewAI playbooks, loops allow you to iterate over a set of items and perform actions on each item. Loops are essential for automating repetitive tasks and handling multiple items efficiently. This tutorial will cover the basics of using loops in playbooks, including examples and explanations.

Basic Loop Structure

The basic loop structure in CrewAI playbooks uses the loop keyword. Here's a simple example:

- name: Print numbers from 1 to 5
  loop:
    - 1
    - 2
    - 3
    - 4
    - 5
  task:
    - name: Print the number
      action: echo "{{ item }}"
                

In this example, the loop iterates over the list of numbers from 1 to 5 and prints each number using the echo action.

Looping Over a List

You can use loops to iterate over any list of items. For instance, let's loop over a list of server names and perform an action on each server:

- name: Restart servers
  loop:
    - server1
    - server2
    - server3
  task:
    - name: Restart the server
      action: restart_server "{{ item }}"
                

This example will restart each server in the list by calling the restart_server action with the server name as an argument.

Looping with a Dictionary

Loops can also iterate over dictionaries. Here, we'll loop over a dictionary of users and their roles:

- name: Assign roles to users
  loop:
    user1: admin
    user2: editor
    user3: viewer
  task:
    - name: Assign role
      action: assign_role "{{ item.key }}" "{{ item.value }}"
                

This example will assign roles to each user by calling the assign_role action with the user's name and role as arguments.

Nested Loops

You can nest loops within other loops to handle more complex iterations. For example, let's loop over a list of server groups, and for each group, loop over the servers:

- name: Restart server groups
  loop:
    group1:
      - server1
      - server2
    group2:
      - server3
      - server4
  task:
    - name: Restart each server in the group
      loop: "{{ item }}"
      action: restart_server "{{ item }}"
                

This example will restart each server in each group by iterating over the servers within each group.

Loop Control and Breaks

Sometimes, you might need to control the flow of the loop, such as breaking out of the loop based on a condition. Here's an example of breaking out of a loop when a certain condition is met:

- name: Check servers status
  loop:
    - server1
    - server2
    - server3
  task:
    - name: Check server status
      action: check_status "{{ item }}"
      when: "item == 'server2'"
      register: result

    - name: Break if server is down
      action: break
      when: "result == 'down'"
                

This example checks the status of each server and breaks out of the loop if any server is down.

Conclusion

Loops in CrewAI playbooks are powerful tools for automating repetitive tasks and handling multiple items efficiently. By understanding and using loops, you can create more dynamic and flexible playbooks. We hope this tutorial has given you a comprehensive understanding of loops and how to use them in your playbooks.