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.
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