Ansible- Loop over sub items

2 min read 04-10-2024
Ansible- Loop over sub items


Looping Through Sub-Items in Ansible: Mastering Complex Data Structures

Ansible's power lies in its ability to automate tasks across multiple systems. But what happens when your data takes on a more complex structure? This is where the art of looping through sub-items comes into play.

Let's say you have a YAML file containing a list of servers, each with its own list of applications.

servers:
  - name: server1
    apps:
      - name: app1
        version: 1.0
      - name: app2
        version: 2.0
  - name: server2
    apps:
      - name: app3
        version: 3.0

Now, you want to use Ansible to perform actions on each application, for example, install updates.

The Power of Nested Loops in Ansible

Ansible's with_items loop is your friend for navigating complex data structures. Here's how you can iterate over both servers and their applications:

- hosts: all
  tasks:
  - name: Loop through servers
    debug:
      msg: "Server name: {{ item.name }}"
    with_items: "{{ servers }}"
    loop_control:
      loop_var: server
  - name: Loop through applications
    debug:
      msg: "Application name: {{ item.name }} - Version: {{ item.version }}"
    with_items: "{{ server.apps }}"
    loop_control:
      loop_var: app

This code snippet demonstrates two nested loops:

  1. Looping through servers: The first loop iterates through the servers list, storing each server's information in the server variable.
  2. Looping through applications: The second loop, nested within the first, iterates through the apps list within the current server.

This approach allows you to access individual application information using the app variable, making it easy to perform specific tasks on each application.

Key Concepts for Looping Mastery

  • loop_control: This directive is crucial for managing loop variables. In our example, loop_var defines the variable used to access the current item in the loop.
  • Nested loops: You can combine multiple with_items loops to traverse increasingly complex data structures.
  • register: Use the register keyword to store the results of your loop actions for future use.

Practical Example: Updating Applications

Imagine you need to upgrade the version of all applications on all servers to the latest version. You could use the following playbook:

- hosts: all
  tasks:
  - name: Install latest version
    package:
      name: "{{ item.name }}"
      state: latest
    with_items: "{{ server.apps }}"
    loop_control:
      loop_var: app
    loop:
      - name: server1
        apps:
          - name: app1
            version: 1.0
          - name: app2
            version: 2.0
      - name: server2
        apps:
          - name: app3
            version: 3.0

This playbook iterates through each server and updates the packages for all its applications to the latest available version.

Conclusion

Ansible's with_items loop is a versatile tool for handling complex data structures. By understanding nested loops and loop control, you can automate tasks with precision, ensuring efficient management of your infrastructure. Remember to always test your playbooks thoroughly before applying them to production environments.

Resources: