help w/ add_host

In trying to wrangle my inventory to be more dynamic, I’ve come to find group_by and add_hosts. group_by seems to work well, but I’m having trouble understanding why add_host seems to only add a single host to a new group: it’s always the first host in the list.

When I use my recently created group names (from a group_by) in a new playbook within the same run, my test remote echo works as expected, but add_hosts is only called against a single host (always the first host in my inventory).

playbook and output here: https://gist.github.com/davidbirdsong/6668127

Using add_host inside a host loop is not intended.

It’s intended for local plays that are doing machine provisioning, to add machines to inventory not yet picked up by an inventory script.

Use group_by instead and you’ll get what you want.

I should also point out your “when” statement: