How would I iterate through containers in the variable syntax?

I am looking to get the internalip value from the containers list when using <%= instance.containers %> in bash script.

Don’t have an answer on dynamically iterating.

However, when using a fixed set of VMs in an instance I can use:

echo “<%=instance.containers[0].hostname%>: <%=instance.containers[0].internalIp%>: <%=instance.containers[0].externalIp%>”
echo “<%=instance.containers[1].hostname%>: <%=instance.containers[1].internalIp%>: <%=instance.containers[1].externalIp%>”

It works for me because I know exactly how many VMs are in each instance.

I could query the amount of VMs in an instance by using:

<%=instance.containers.size%>

However, when iterating it fails for me:

i=0
while [ $i -ne <%=instance.containers.size%> ]
do
i=$(($i+1))
echo “<%=instance.containers[$i].hostname%>: <%=instance.containers[$i].internalIp%>: <%=instance.containers[$i].externalIp%>”
done

When I comment out the line echo "<%=instance.containers[$i].hostname%>, it still fails. Making me wonder if the variable replacement gets done before invoking the script. I think there’s some Groovy behind it.

Variable replacement is done prior to script execution. It depends how you want to utilise the list of internal IPs. You could use the collect closure in groovy to return a list of internal ips for all containers:

echo "<%= instance.containers.collect{it.internalIp} %>"
1 Like