with_items x count

Hey just wondering if there is a more elegant way of doing this:


droplets:

  • name: consul
    count: [1,2,3,4,5]
    region: nyc2
    region_id: 4
  • name: influxdb
    count: [1,2,3]
    region: nyc2
    region_id: 4
  • name: elasticsearch
    count: [1,2,3,4]
    region: nyc2
    region_id: 4

vars_files:

  • vars/testing.yml
    tasks:
  • name: Print phone records
    debug: msg=“{{ item.0.name }}.{{ item.1 }} in {{ item.0.region }}({{ item.0.region_id }})”
    with_subelements:
  • droplets
  • count

this could get out of control if the counts got into the 100’s

would be nice if I could do this:

  • name: consul
    count: 5
    region: nyc2
    region_id: 4

and it looped on the count 5 times.

hoping you have a more elegant solution for me :slight_smile:

You can simplify that by just using with_items:

vars:
droplets:

  • name: consul
    count: 5
    region: nyc2
    region_id: 4
  • name: influxdb
    count: 3
    region: nyc2
    region_id: 4
  • name: elasticsearch
    count: 4
    region: nyc2
    region_id: 4
    tasks:
  • name: Print phone records
    debug: msg=“{{ item.name }} in {{ item.region }}({{ item.region_id }})”
    with_items: droplets

Results in output like this:

ok: [127.0.0.1] => (item={‘count’: 5, ‘region’: ‘nyc2’, ‘name’: ‘consul’, ‘region_id’: 4}) => {
“item”: {
“count”: 5,
“name”: “consul”,
“region”: “nyc2”,
“region_id”: 4
},
“msg”: “consul in nyc2(4)”
}

That only prints each droplet once, not count times. The original would have given consul.1, consul.2, consul.3, etc.

with_sequence: count=5 I’m not sure how that would work with with_items, but it’s worth a try… (with_nested used with sequences?)

http://docs.ansible.com/playbooks_loops.html#looping-over-integer-sequences

With_sequence could be used to loop over each step X times… But can’t be combined with with_items as far as I can tell.

Adam

It would be better and much more ansible-playbook-happy to enhance the digital ocean module to support exact_count like ec2 and Rackspace have, rather than having the playbook get a little gross like this.

Both implement them a little differently, and I’m not entirely sure what it might take, but probably not too difficult.