Skip to content

Inefficient loops negatively impacts performance #8949

Open
@pemsith

Description

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

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions