`ansible_host` with `delegate_to` points to `inventory_hostname`, not delegated host

Actually the situation with connection variables like ansible_host, ansible_connection etc. in combination with delegate_to: D is a bit more nuanced. Let’s take ansible_host as example:

  • ansible_host from D is always used to establish a connection.
  • For templating, if inventory_hostname has ansible_host explicitely declared (e.g. via host_vars), then ansible_host is used from here.
  • Otherwise ansible_host is taken from D.
  • Other variable types are not taken at all from D.

That leads to the curiosity, that when resolving ansible_host via DNS, my playbook works, but when declaring ansible_host explicitely in host_vars, it fails.

As connection variables are already handled in a specialized way for delegate_to directive and are always taken from delegated host context for connection establishment, why not adhere to same principles the other way round and always return delegate host values for ansible_host & Co. during template interpolation?

Is this purely based on historical decisions? I might be missing something - please correct me otherwise, but from bare user perspective current design chocie seems to be the most inconsistent and least intuitive.

See also Variables not consumed from delegated host - #3 by kristianheljas .