with_items x count

2,362 views
Skip to first unread message

sma...@gmail.com

unread,
May 29, 2014, 1:34:19 AM5/29/14
to ansible...@googlegroups.com
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 :)

James Cammarata

unread,
May 29, 2014, 10:13:16 AM5/29/14
to ansible...@googlegroups.com
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)"
}



--
You received this message because you are subscribed to the Google Groups "Ansible Project" group.
To unsubscribe from this group and stop receiving emails from it, send an email to ansible-proje...@googlegroups.com.
To post to this group, send email to ansible...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/ansible-project/0a5ac977-f227-47de-b8c1-f10951e3779b%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Adam Heath

unread,
May 29, 2014, 11:33:01 AM5/29/14
to ansible...@googlegroups.com
That only prints each droplet once, not count times. The original would
have given consul.1, consul.2, consul.3, etc.

On 05/29/2014 09:13 AM, James Cammarata wrote:
> 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
> debug: msg="{{ item.0.name <http://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 :)
>
> --
> You received this message because you are subscribed to the Google
> Groups "Ansible Project" group.
> To unsubscribe from this group and stop receiving emails from it,
> send an email to ansible-proje...@googlegroups.com
> <mailto:ansible-proje...@googlegroups.com>.
> To post to this group, send email to
> ansible...@googlegroups.com
> <mailto:ansible...@googlegroups.com>.
> <https://groups.google.com/d/msgid/ansible-project/0a5ac977-f227-47de-b8c1-f10951e3779b%40googlegroups.com?utm_medium=email&utm_source=footer>.
> For more options, visit https://groups.google.com/d/optout.
>
>
> --
> You received this message because you are subscribed to the Google
> Groups "Ansible Project" group.
> To unsubscribe from this group and stop receiving emails from it, send
> an email to ansible-proje...@googlegroups.com
> <mailto:ansible-proje...@googlegroups.com>.
> To post to this group, send email to ansible...@googlegroups.com
> <mailto:ansible...@googlegroups.com>.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/ansible-project/CAMFyvFh9uAadSkSb7LaFGEpDEjGC9F6wSg%3Dye5ZH09niPF7gAw%40mail.gmail.com
> <https://groups.google.com/d/msgid/ansible-project/CAMFyvFh9uAadSkSb7LaFGEpDEjGC9F6wSg%3Dye5ZH09niPF7gAw%40mail.gmail.com?utm_medium=email&utm_source=footer>.

Adam Morris

unread,
May 29, 2014, 12:29:42 PM5/29/14
to ansible...@googlegroups.com
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?)





Adam Morris

unread,
May 29, 2014, 12:47:28 PM5/29/14
to ansible...@googlegroups.com


On Thursday, May 29, 2014 9:29:42 AM UTC-7, Adam Morris wrote:
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?)


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

Michael DeHaan

unread,
May 31, 2014, 12:30:25 PM5/31/14
to ansible...@googlegroups.com
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.






--
You received this message because you are subscribed to the Google Groups "Ansible Project" group.
To unsubscribe from this group and stop receiving emails from it, send an email to ansible-proje...@googlegroups.com.
To post to this group, send email to ansible...@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages