Nested loops, new to Ansible and need help

I need to be create a nested loop (I’m open to another way of doing this if somebody knows one). I’ll describe my problem as best I can:

I need to be able to deploy a fixed number of VMs (VMware) within my VMware cluster(s). I have a dictionary with a list of datastores and an associated integer representing the number of VMs I can deploy (calculated earlier in my playbook based on the available free space). What I need to do:

  1. loop through each dictionary entry and check the free space integer ( e.g. 4, 7,8 whatever the quantity of VMs I can deploy is) and compare against the predefined number of VMs I need to deploy
  2. If the free space is equal or greater than the number of VMs I need to deploy, deploy all VMs, and finish
  3. If the capacity is less that the needed number deploy as many as I can, subtract that from the total number required and move to the next dictionary entry
  4. repeat until the needed number of VMs have been deployed.

I should point out that I have a check condition built in at the beginning of the playbook to fail if there is not enough aggregate capacity across all of my datastores.

If this was another coding language I’d find this fairly trivial but I’m struggling with the lack of an if-then-else type construct in Ansible so have been going round in circle with this, I’m not sure how I can get the required syntax having looked at the examples in the Ansible documentation

- my code!
  • Set at least one tag (the experts follow the tags, so the right people will find you if you tag)

Hello, @newbie. I have no idea why, but your Get Help post from December showed up in my feed today, i.e. almost 6 months later.

Anyway, before I launch into a long-winded suggestion, can you indicate whether this is still an issue? If so, any additional details would be welcome. If it’s resolved, a note about how you solved the problem would be welcome to future visitors to the forum.


Me too, just popped up.
I think we tackled this though, as a similar item was asked in
Need help parsing output from VMware collection