I figured out what was going on. The return from hosts var was a single string with comma separated hostnames host1, host2, I had to convert it to List using the comma as delimiter. The following post How to convert comma-separated String to List? helped me with that task.
Basically the last section now reads