# with\_items x count

**URL:** <https://forum.ansible.com/t/with-items-x-count/15359>\
**Category:** Ansible Project\
**Created:** [May 29, 2014, 5:34am UTC](https://forum.ansible.com/t/with-items-x-count/15359 "2014-05-29T05:34:19Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![smajor](https://avatars.discourse-cdn.com/v4/letter/s/a4c791/32.png) [@smajor](https://forum.ansible.com/u/smajor)\
**Post date:** [May 29, 2014, 5:34am UTC](https://forum.ansible.com/t/with-items-x-count/15359/1 "2014-05-29T05:34:19Z")

</div>

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 🙂

---

<div class="post-metadata">

**Author:** ![James\_Cammarata](https://avatars.discourse-cdn.com/v4/letter/j/7c8e57/32.png) [@James\_Cammarata](https://forum.ansible.com/u/James_Cammarata)\
**Post date:** [May 29, 2014, 2:13pm UTC](https://forum.ansible.com/t/with-items-x-count/15359/2 "2014-05-29T14:13:10Z")

</div>

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](http://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)”  
}

---

<div class="post-metadata">

**Author:** ![adam2](https://avatars.discourse-cdn.com/v4/letter/a/ecae2f/32.png) [@adam2](https://forum.ansible.com/u/adam2)\
**Post date:** [May 29, 2014, 3:32pm UTC](https://forum.ansible.com/t/with-items-x-count/15359/3 "2014-05-29T15:32:50Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Adam\_Morris](https://avatars.discourse-cdn.com/v4/letter/a/ecae2f/32.png) [@Adam\_Morris](https://forum.ansible.com/u/Adam_Morris)\
**Post date:** [May 29, 2014, 4:29pm UTC](https://forum.ansible.com/t/with-items-x-count/15359/4 "2014-05-29T16:29:42Z")

</div>

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](http://docs.ansible.com/playbooks_loops.html#looping-over-integer-sequences)

---

<div class="post-metadata">

**Author:** ![Adam\_Morris](https://avatars.discourse-cdn.com/v4/letter/a/ecae2f/32.png) [@Adam\_Morris](https://forum.ansible.com/u/Adam_Morris)\
**Post date:** [May 29, 2014, 4:47pm UTC](https://forum.ansible.com/t/with-items-x-count/15359/5 "2014-05-29T16:47:28Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Michael\_DeHaan1](https://avatars.discourse-cdn.com/v4/letter/m/5e9695/32.png) [@Michael\_DeHaan1](https://forum.ansible.com/u/Michael_DeHaan1)\
**Post date:** [May 31, 2014, 4:30pm UTC](https://forum.ansible.com/t/with-items-x-count/15359/6 "2014-05-31T16:30:23Z")

</div>

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.
