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 #8949

Open
pemsith opened this issue Jan 5, 2025 · 0 comments
Open

Inefficient loops negatively impacts performance #8949

pemsith opened this issue Jan 5, 2025 · 0 comments

Comments

@pemsith
Copy link

pemsith commented Jan 5, 2025

Describe the bug
The playbook contains multiple instances of inefficient loops using the command module. These tasks individually execute commands for each item in the loop, resulting in significantly longer execution times due to repeated invocations of the command module. This inefficiency can degrade performance, especially in scenarios with larger datasets or repetitive tasks.

Examples of inefficient tasks

Cleanup old hosts
This task loops over individual hosts and deletes them one by one using the command module:

yaml
Copy code

  • name: Cleanup old hosts
    command: "hammer host delete --name {{ item }}"
    loop:
    • satellite.bdj7s.red.osp.opentlc.com
    • aqua.example.com
    • skylab.example.com
      Create Master and Worker subnets for the ARO4 cluster
      This task creates subnets by invoking the az CLI multiple times, instead of handling the process more efficiently:

yaml
Copy code

  • name: Create Master and Worker subnets for the ARO4 cluster
    command: "{{ item }}"
    loop:
      az network vnet subnet create
      --resource-group {{ az_resource_group }}
      --vnet-name "{{ project_tag }}-vnet"
      --name "{{ project_tag }}-master"
      --address-prefixes "10.{{ range (1, 127) | random }}.{{ range(1, 255) | random }}.0/24"
      --service-endpoints Microsoft.ContainerRegistry
      az network vnet subnet create
      --resource-group {{ az_resource_group }}
      --vnet-name "{{ project_tag }}-vnet"
      --name "{{ project_tag }}-worker"
      --address-prefixes "10.{{ range (1, 127) | random }}.{{ range(1, 255) | random }}.0/24"
      --service-endpoints Microsoft.ContainerRegistry

Expected behavior
Both tasks should be optimized to minimize the number of command invocations. This can be achieved by combining commands into a single execution or using a more efficient module/script to handle multiple items at once.

Optimized Examples

Cleanup old hosts
Combine the host deletion commands into a single task:

yaml
Copy code

  • name: Cleanup old hosts (optimized)
    shell: |
    hammer host delete --name satellite.bdj7s.red.osp.opentlc.com
    aqua.example.com
    skylab.example.com
    Create Master and Worker subnets for the ARO4 cluster
    Use a single script or combine the commands in a way that reduces overhead:

yaml
Copy code

  • name: Create Master and Worker subnets (optimized)
    shell: |
    az network vnet subnet create --resource-group {{ az_resource_group }}
    --vnet-name "{{ project_tag }}-vnet"
    --name "{{ project_tag }}-master"
    --address-prefixes "10.{{ range (1, 127) | random }}.{{ range(1, 255) | random }}.0/24"
    --service-endpoints Microsoft.ContainerRegistry
    az network vnet subnet create --resource-group {{ az_resource_group }}
    --vnet-name "{{ project_tag }}-vnet"
    --name "{{ project_tag }}-worker"
    --address-prefixes "10.{{ range (1, 127) | random }}.{{ range(1, 255) | random }}.0/24"
    --service-endpoints Microsoft.ContainerRegistry
    By reducing the number of loops and separate command invocations, these changes will improve performance and maintainability.

Verions:

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

No branches or pull requests

1 participant