Infering the ssh private key file based on current provider

We’ve got a Vagrantfile that needs to work for several providers. Within this file, we do provision using ansible, and one provisioning step is done using the synchronize module that uses rsync internally.

When using this module directly from this setup, users need to enter the password of the vagrant user during vagrant provision because rsync will ask for it.

We can provide an ssh identity file to the sychronize module using the private_key parameter as documented here: ansible.posix.synchronize module – A wrapper around rsync to make common tasks in your playbooks quick and easy — Ansible Documentation

And of course, I can specify this parameter from the Vagrantfile by setting a variable using ansible.extra_vars. For a machine named testvm and when using the VirtualBox provider, I can set this to the relative path "../.vagrant/machines/testvm/virtualbox/private_key". Unfortunately, for the VmWare Desktop or for Parallels, the path will be different.

Any idea how I can set infer the active provider (or ideally the path to the ssh private key directly) from the Vagrantfile?

Please also have a look at Get current provider in Vagrantfile · Issue #1867 · hashicorp/vagrant · GitHub.