0

I need a playbook to delete some vm's from VMWare using ANSIBLE

My playbook is similar to:

  - 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 }

It works well, the problem is that I have 18 GROUPs and I wouldn't like to write 4x18=78 lines to do this

other machine names:

[...]
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

I need a king of loop...

Thanks in advice.

1 Answer 1

0

Are you looking for something like this in 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

The output is:

VM_NAME_01_GROUP01
VM_NAME_02_GROUP01
VM_NAME_03_GROUP01
VM_NAME_04_GROUP01
VM_NAME_01_GROUP02
...
VM_NAME_04_GROUP18

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .