Ansible has issues with "run_once" and "when" in the same task. If the "when" only evaluates to true for some hosts, it's basically undefined wether the task will run or be skipped based on whatever host happens to be the first one to be evaluated. If the first one is skipped, the task won't even run once if all others would evaluate as true.
Example:
- name: Create temporary directory to download agent on ansible host
when: package.pkgversion != agent_installed_version
register: tempdir_ansiblehost
run_once: true
delegate_to: localhost
check_mode: no
tempfile:
state: directory
#ansible