combining with_nested: + with_together: (incl. loop count limit)

699 views
Skip to first unread message

Nusenu

unread,
Feb 18, 2015, 5:23:59 PM2/18/15
to ansible...@googlegroups.com
Hi,

(this email was previously send to the incorrect list [1])

I've got 3 sets of data:

foo: [ A, B, C, D, ...]
bar: [ 1, 2 ]
tra: [ a, b ]

foo should be combined with bar and tra in a nested loop, but bar and
tra should loop together.

something like (not actually working):

- debug: msg="{{ item[0] }} {{ item[1][0] }} {{ item[1][1] }}
with_netsted:
- foo
- item
with_together:
- bar
- tra

Expected output:

A1a
A2b
B1a
B2b
C1a
C2b
...

How would I actually combine with_nested: and with_together?

In addition to that I will need a loop termination once it reached a
certain amount of loop executions.
On a simple loop I would use 'with_indexed_items:' and check the index
against a given max value. How would that loop count limit be
implemented in the loop described above?

thanks!
Nusenu

[1] https://groups.google.com/d/msg/ansible-devel/aLrH_SC8HyY/FZPs7GanHysJ

Nusenu

unread,
Feb 19, 2015, 1:26:08 PM2/19/15
to ansible...@googlegroups.com
I got it working. I combined 'bar' and 'tra' in a dictionary and used
that in with_nested


Now I'm left with the question: How can I limit the amount of loops in
a with_nested loop?

Nusenu

unread,
Mar 16, 2015, 4:29:58 PM3/16/15
to ansible...@googlegroups.com
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA512

> How can I limit the amount of loops in a with_nested loop?

I still didn't find a solution for this. Maybe someone can suggest
alternative approaches if the use case is clear:

I've got two use cases where I would need an index counter within a
with_nested loop (or any other solution that gets the job done):

1) limit number of service instances based on available machine memory

basically something like:
create process instances
with_nested:
- set1
- set2
when: machine_memory / loopIndex+1 > minimalmemoryrequired


but there is no "loopIndex" that I could use..


2) assign unique TCP port to a service to prevent duplicate bindings


here is the ansible role where this would be useful for me:
https://github.com/nusenu/relayor/blob/master/tasks/configure.yml

thanks,
Nusenu
-----BEGIN PGP SIGNATURE-----

iQIcBAEBCgAGBQJVBz0vAAoJEFv7XvVCELh0Zi8P/iqwRHZe5l67BDiEUZNrRPWO
xhr0uM8iKUr00zMp2WTuG6PN9ifGR0ZdNcxeojTyqiZYnzJPHWxVi4VG4KR59gOi
FUimZmuNzcvBKHFLzm4vqiy/n28qYQacXDTacBamAtoguot1ecH4RK1gwHmtZItZ
QpPBq/08x30o9rf5GHE+yeLE6x62fXIcGYVoadjqghNReZxym68szI0Np1jjByKl
vkIZ/tf5/gFZ2hkV7aMNiH9b6cMR4a6hHhP1oCk4SZ/6ERxbOTuYJoX+NZzfqCf6
hJLrJPhreyfHAaF5wSG62yrP/NWY9Clxz+5451UTS/t6qjR3bUVBG9jR6R30A0yt
bvcu4wzpaNbcMIVfx8UQTvlpljzfh6oiJMhUUxeTL8PNAz8P27OptEIc4PvNE9zJ
DE30plbsM7YLJLZT6ASPGiAMDBW6sL48hKx6XC63vSQZC1tkDp8/xSjf4kjpXwsp
DZMjliVoDBIs4oRQNwTPvJORIaYGAsIPlHzgULCA26XgVvqcKkmL89gA/y6aV068
HugIRTUjKRq0urMdMyfdiuru96qKLxKhEB4q0iBmg69udI3hcgd8xx438syOgTV1
QZ79MHnhUMqA2H2Vh3tB1pJvlopGnLRiHSt/SRmwhlc/qRXND9SAmU+8lfzvCXcq
sjuUVyXYFrz5GAxHmrwE
=pgFr
-----END PGP SIGNATURE-----
Reply all
Reply to author
Forward
0 new messages