0

Is any reliable method available for including a conditional in a Vagrantfile, for use in Vagrant, to select method of folder syncing based on provider?

Presently, I have included the following line:

config.vm.synced_folder "proj", "/srv", type: "9p", accessmode: "mapped"

However, the following represent a few possible improvements:

  1. Use type "9p" only if the provider is libvirt, otherwise use some other, particular method.
  2. As above, but use whatever method is default for the provider, if the provider is not libvirt.
  3. As above, but test not whether the provider is libvirt, but simply whether support is available for the type "9p".
  4. Select a method globally, rather than as the type parameter, for all separate invocations of synced_folder.
  5. Apply also conditionally the value for accessmode, based on one of the tests given above.

Might anyone offer recipes for making the Vagrantfile more robust and portable, through any of the above suggestions that may be feasible, given the capabilities of the tools?

brainchild
  • 754
  • 1
  • 7
  • 21

1 Answers1

0

I'm no expert on vagrant or libvirt, but maybe this article Writing a general multi-provider Vagrantfile, in particular testing for (in your case) libvirt. Here's the relevant part of the sample config in the article that might point you in the right direction,

Vagrant.configure(2) do |config|
    config.vm.provider :libvirt do |libvirt|
      config.vm.box_url = "https://download.fedoraproject.org/pub/fedora/linux/releases/22/Cloud/x86_64/Images/Fedora-Cloud-Base-Vagrant-22-20150521.x86_64.vagrant-libvirt.box"
    end if Vagrant.has_plugin?('vagrant-libvirt')
end

I'm not sure why the conditional is after the end in that article, I think the following is equivalent and clearer,

Vagrant.configure(2) do |config|
  if Vagrant.has_plugin?("vagrant-libvirt")
    #...

else obviously also useful for the non-libvirt scenario.

If I'm misunderstanding the question or going off in totally the wrong direction please comment accordingly so I can delete or modify my answer.

Andrew Richards
  • 1,392
  • 11
  • 18
  • Thanks for the attempt. The structure of encapsulating the full calls in outer conditional statements creates a high level of code redundancy, while only satisfying to a minimal extent the various objectives. The intention of the question was to seek a solution less clumsy than the most obvious. – brainchild Jan 20 '22 at 03:30
  • Thanks @epl, sorry that hasn't helped much. I'll leave the answer up, at least for now since "most obvious" may help others. – Andrew Richards Jan 20 '22 at 11:31
  • I hope you didn't find my language insulting. The point was that I am seeking more specific and relevant details than those directly emerging from the documentation. – brainchild Jan 21 '22 at 04:45