provisioning multiple ec2 instances to multiple subnets and dealing with unmatching numbers

Hello,

I am building a provisioning system that will be receiving parameters from a jenkins job.

  1. I want to have a single playbook from where I can create new ec2 instances for both ec2-classic and vpc, though I can live with 2 different playbook in case it won’t work otherwise.

  2. I want for example, to launch 6 instances spread across 3 subnet-ids given, within 1 run of playbook.

  3. I want for example, to launch 3 instances from 6 subnet-ids given, within 1 run of playbook.

  4. when I use “with_items” and pass an array of say, 10 subnets, then 10 instances will be created. I wantcto be able to decide how many instances eventually run

  5. when using " {{ var | random }} → I was able to retrieve a subnet/zone randomly, but it would create 10 instances with the same subnet (which was picked randomly but didn’t change between the iterations).

  6. I didn’t really get into “count_tag” and “exact_count” because from what I read these parameters decide how many instances of the decided tag will run, eliminating other (and I don’t want to allow any kind of instance termination in this scope).

I thought of using the “until” option but wasn’t able to find any way to know how many instances have been created, like some value that gets incremented every run.

a valid “until” statement can be: "ec2.instances length = desired_number_of_instances

your help will be highly appreciated.