Ansible loop speed to the max

river

Imagen you want to use Ansible to set interface description based on CDP. The following example is super slow because each iteration in the loop, that satisfies the condition, is a separate task that Ansible has to execute.


- name: Set interface description.
  cisco.ios.ios_interfaces:
    config:
      - name: "{{ interface_description_item['key'] }}"
        description: "{{ interface_description_item['value'][0]['host'] }} via {{ interface_description_item['value'][0]['port'] }}"
    state: merged
  when: interface_description_item['value'][0]['platform'] is defined
  loop: "{{ ansible_facts['net_neighbors'] | dict2items }}"
  loop_control:
    loop_var: interface_description_item
    label: "{{ interface_description_item['key'] }}"

Modern modules, so-called resource modules, start with a “config” option that accepts multiple items from a list. The benefit of this is that all these items can be executed as one task by Ansible. Loops are still required to build the list, but this part of the job is moved out of the task and into a Jinja template. Rewriting the CDP example leaves us with a task like this.

- name: Set interface description.
  cisco.ios.ios_interfaces:
    config: "{{ lookup('ansible.builtin.template', 'interfaces_description.j2') | ansible.builtin.from_yaml }}"
    state: merged

The corresponding Jinja template might look like the following.

{% for interface_description_item in ansible_facts['net_neighbors'] | ansible.builtin.dict2items %}
{% if interface_description_item['value'][0]['platform'] is defined %}
- name: {{ interface_description_item['key'] }}
  description: {{ interface_description_item['value'][0]['host'] }} via {{ interface_description_item['value'][0]['port'] }}
{% endif %}
{% endfor %}

This will massively increase the time it takes to go through a loop. You can use the callback ansible.posix.profile_tasks to measure and print the time if you want to test this yourself.

$ cat ansible.cfg
[defaults]
callbacks_enabled = ansible.posix.profile_tasks

Posted

in

by

Tags:

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *