harbor run -t scale-ai/hil-bench-swe_73-full_infoharbor download scale-ai/hil-bench-swe_73-full_infoThe PlayIterator class in Ansible currently exposes its internal _host_states attribute as private, limiting the ability of users and extensions to intercept and log state changes in a controlled manner. Public methods are needed that allow controlled access to host state management during play execution, maintaining proper type validation and encapsulation.
A new public method must be implemented as an instance method of the PlayIterator class, named set_state_for_host. This single method can be the only new public API introduced to minimize the public interface footprint. All state modifications, including changes to individual state attributes, should be channeled through this single method to maintain a minimal and consistent API surface.
Feature Request
lib/ansible/executor/play_iterator.py
The internal state of hosts in PlayIterator cannot be manipulated in a controlled way by external code due to the private nature of the _host_states attribute. A public method is required to set host states with appropriate validation.
Attempt to access _host_states directly from external code
Observe that there are no public methods to modify host states in a controlled manner
Notice the lack of type validation when manipulating states directly
A public method should be available for setting complete state for a host, with appropriate type validation to ensure controlled and safe manipulation of host states.
Only direct access to the private _host_states dictionary without validation or control is possible.
The implementation must create a set_state_for_host instance method on PlayIterator that allows setting a complete HostState for a specific host identified by hostname.
The method must include type validation to ensure the state parameter is a HostState instance, and raise an AnsibleAssertionError if it is not.
The set_state_for_host method signature must accept exactly two parameters: a hostname string and a HostState instance. The method's interface must not be extended with additional optional parameters.
The implementation must replace all direct assignments to _host_states[hostname] with calls to the new public method.
The implementation must also support modifications to the run_state and fail_state attributes of host states individually, without requiring callers to construct or pass the full HostState object.
The implementation should follow defensive programming practices for state management.
Type: Method Name: set_state_for_host Path: lib/ansible/executor/play_iterator.py Input: hostname: str, state: HostState Output: None Description: Sets the entire HostState object for a specific host by its name after performing type validation.
When setting run_state values, they must be validated as IteratingStates enum type. When setting fail_state values, they must be validated as FailedStates enum type. These are the specific enum types defined in play_iterator.py that correspond to each attribute.
The set_state_for_host method must copy the state before storing it to follow the defensive copy pattern used by get_host_state. The assignment should be self._host_states[hostname] = state.copy() (defensive copy), not self._host_states[hostname] = state (direct reference). This prevents external code from accidentally modifying the stored state.
Implement two dedicated setter methods as separate public methods on PlayIterator: set_run_state_for_host(hostname, run_state) and set_fail_state_for_host(hostname, fail_state). Do NOT handle individual attribute updates through set_state_for_host or optional parameters — each attribute requires its own dedicated method.