connection=autolocal

Is it possible to define host in inventory in a way that will use local connection is the name resolves to local address?

Is it worth to add connection=autolocal option?

We would not be taking a pull request for a new connection option.

However, if you want to make sure the localhost if in the host file is always local:

localhost ansible_connection=local

(Further, Ansible now has “implicit localhost” capabilities so localhost doesn’t have to be in inventory in most cases, and if not there and referenced, it is assumed to be a local connection)

No. I want to make connection to host "yetanothernode" local only if I execute
playbook on this host itself. If I execute playbook for this host from other
machine, I want it to connect to "yetanothernode" by SSH as usual.

The problem can also be generalized for nodes inside subnetwork, where
node see each other by local IPs (or local protocol) and should connect
using custom connection if some condition is met. If condition is not met,
we assume to be outside the network and fall down to standard connection
type.

Hmmm, this would require some games to figure out if the hostname in the playbook matched what the local machine was saying it was.

I’m inclined to say no to this, and it can be greatly simplified by even allowing the machine to be managed locally over SSH if you want - - if localhost is not special.

Generally most people are designing a node for management purposes rather than running Ansible from one of the nodes in a cluster, etc, so I can’t see this being a very common use case.

Perhaps I’m misunderstanding though.