I use Ansible to configure some servers, one of which is only accessible locally and via the Tailscale VPN. This means I have two hostnames for the same box, where none, one, or both hostnames are always reachable.
Is there a way to instruct Ansible that some hostnames belong to the same physical server so that the playbook is not being run for each hostname and therefore for the same server multiple times?
For example, if I am in the network, and the tailscale client is running on my machine and the server, I want Ansible to only use one of those connections. If the Tailscale VPN is disconnected or I am not in the local network, I want Ansible to fall back to the reachable hostname.
You can have multiple inventory files and manually switch between the two. Put "everything else" in, say, hosts.yaml, and then for your VPN connected host, have two files:
In host_is_local.yaml
:
all:
hosts:
myvpnhost:
ansible_host: localhost
In host_is_vpn.yaml
:
all:
hosts:
myhost:
ansible_host=my.vpn.address
Now when you run your playbooks, you can select how that host is addressed by selecting the appropriate inventory file:
ansible-playbook -i hosts.yaml -i host_is_local.yaml ...
Or:
ansible-playbook -i hosts.yaml -i host_is_vpn.yaml ...