Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Inefficient loops negatively impacts performance #7631

Closed
pemsith opened this issue Jan 7, 2025 · 3 comments
Closed

Inefficient loops negatively impacts performance #7631

pemsith opened this issue Jan 7, 2025 · 3 comments
Labels

Comments

@pemsith
Copy link

pemsith commented Jan 7, 2025

Describe the bug
The playbook uses an inefficient loop with the command module to remove container images individually. Each iteration of the loop invokes the command module, leading to unnecessary overhead and longer execution times, particularly when managing multiple container images.

Example of the inefficient task in infrastructure-playbooks/purge-dashboard.yml

  • name: remove ceph dashboard container images
    command: "{{ container_binary }} rmi {{ item }}"
    loop:
    • "{{ alertmanager_container_image }}"
    • "{{ prometheus_container_image }}"
    • "{{ grafana_container_image }}"

Expected behavior
The task should handle the removal of container images in a more efficient way by combining the commands or using a more appropriate module or approach to process multiple images in a single execution.

Potential Optimization

Combine Commands: Remove all images in a single invocation:

  • name: remove ceph dashboard container images (optimized)
    shell: "{{ container_binary }} rmi {{ alertmanager_container_image }} {{ prometheus_container_image }} {{ grafana_container_image }}"
    Use a Script: Create a small script to process the list of images and execute it in one task:

  • name: remove ceph dashboard container images using a script
    shell: |
    {% for image in [alertmanager_container_image, prometheus_container_image, grafana_container_image] %}
    {{ container_binary }} rmi {{ image }}
    {% endfor %}
    Observation
    This inefficient loop may cause noticeable delays in environments with a larger number of images. Optimizing the task by reducing the number of command invocations will significantly improve performance and reduce execution time.

Recommendation

Refactor the task to minimize the number of invocations of the command module.
Consider combining commands or using a custom script to process the images in bulk.
Optimizing this task will enhance the efficiency and scalability of the playbook, especially in scenarios with a higher number of container images to manage.

Environment:

  • OS - CentOS stream 9
  • Ansible version (e.g. ansible-playbook --version):
    ansible [core 2.15.8]
    config file = None
    configured module search path = ['/.ansible/plugins/modules', '/usr/share/ansible/plugins/modules']
    ansible python module location = /.local/lib/python3.9/site-packages/ansible
    ansible collection location = /.ansible/collections:/usr/share/ansible/collections
    executable location = /.local/bin/ansible
    python version = 3.9.18 (main, Jan 4 2024, 00:00:00) [GCC 11.4.1 20231218 (Red Hat 11.4.1-3)] (/usr/bin/python3)
    jinja version = 3.1.3
    libyaml = True
@guits
Copy link
Collaborator

guits commented Jan 8, 2025

hello @pemsith
thanks for your feedback. Would you mind sending a PR ? 😁

Copy link

This issue has been automatically marked as stale because it has not had recent activity. It will be closed in a week if no further activity occurs. Thank you for your contributions.

Copy link

This issue has been automatically closed due to inactivity. Please re-open if this still requires investigation.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants