如何删除带有循环或变量的虚拟机列表 - ANSIBLE 和 VMWare

如何删除带有循环或变量的虚拟机列表 - ANSIBLE 和 VMWare

我需要一个剧本来使用 ANSIBLE 从 VMWare 中删除一些虚拟机

我的剧本类似于:

  - name: Delete VMs from VMware 
    vmware_guest:
      hostname: "{{secret.vcenter}}"
      username: "{{secret.username}}"
      password: "{{secret.password}}"
      validate_certs: False
      datacenter: DC
      state: absent
      name: "{{ item.name }}"
    with_items:
      - { name: VM_NAME_01_GROUP01 }
      - { name: VM_NAME_02_GROUP01 }
      - { name: VM_NAME_03_GROUP01 }
      - { name: VM_NAME_04_GROUP01 }

它运行良好,问题是我有 18 个组,我不想编写 4x18=78 行来执行此操作

其他机器名称:

[...]
VM_NAME_01_GROUP04
VM_NAME_02_GROUP04
VM_NAME_03_GROUP04
VM_NAME_04_GROUP04
[...]
VM_NAME_01_GROUP18
VM_NAME_02_GROUP18
VM_NAME_03_GROUP18
VM_NAME_04_GROUP18

我需要一个循环之王...

谢谢指教。

答案1

你是否在 bash 中寻找类似的东西:

for ((group = 1; group < 19; ++group)); do
    for ((name = 1; name < 5; ++name)); do
        printf "VM_NAME_%02d_GROUP%02d\n" "${name}" "${group}"
    done
done

输出是:

VM_NAME_01_GROUP01
VM_NAME_02_GROUP01
VM_NAME_03_GROUP01
VM_NAME_04_GROUP01
VM_NAME_01_GROUP02
...
VM_NAME_04_GROUP18

相关内容