play_hosts with serial includes only the hosts of the current batch

When running a playbook with serial the play_hosts variable includes the hosts just of the current batch. I think this is unexpected and can lead to various issues, especially with the cases where play_hosts is generally used (from docs: “This may be useful for filling out templates with multiple hostnames or for injecting the list into the rules for a load balancer”). Is this classified as a bug or known feature?

It seems this is related to the fact that run_once tasks in a serial play are run once per host batch.

I had situations when this proved useful and situations when this proved annoying :slight_smile: i’d love a finer control on this.

Yeah, I did not account for 'serial' when i added this, but we want to
add 2 vars to reflect with and without serial behaviors.

Thanks for the answer Brian,
2 vars sounds like a good solution. Do you want me to open a GitHub issue for that? Would you accept a PR for that?

I was thinking a bit about naming the variables and here are some thoughts:
I see play_hosts is being replaced by ansible_play_hosts but ansible_play_hosts is not documented and (hopefully) not widely used. So what do you think about these names:
ansible_play_hosts - all hosts in the play;
*ansible_play_batch_hosts (*or ansible_serial_batch_hosts or ansible_batch_hosts) - all hosts in the current play batch.

Additionally, what do you think about the run_once issue - together with serial run_once runs once for each batch which again is unexpected? In the general case this is not a critical issue as executing run_once multiple times should work fine if your task is truly idempotent. In the same time, it is good to be able to run a task once really per play; not per batch. You never know how Ansible can be (mis)used :wink:

run_once is per batch and was always intended that way, as per naming
the vars, I believe there is already some code doing that, need to x2
check.

Did these variables ever get implemented? My googling for ansible_play_hosts found almost nothing.

ansible_play_hosts is in the code base, but we never assign it, so not
yet implemented, though the intention was there.