-
Notifications
You must be signed in to change notification settings - Fork 1k
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
Comments
hello @pemsith |
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. |
This issue has been automatically closed due to inactivity. Please re-open if this still requires investigation. |
Hello sorry i was late in providing a PR. can you reopen so that I can send a PR? I dont have the access to reopen |
created a pull request - #7664 |
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. |
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
command: "{{ container_binary }} rmi {{ item }}"
loop:
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:
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
The text was updated successfully, but these errors were encountered: