Vagrant 在 Vagrantfile 中找不到主机 OS 上的文件

Vagrant cannot find files on Host OS in Vagrantfile

我试图在 Vagrantfile 中引用主机 OS 上的文件并设置环境变量。
但是,Vagrant 似乎找不到该文件。

当引用与 Vagrantfile 位于同一目录中的文件时,一切正常。

1. Vagrantfile中Vagrant可以访问的文件是否仅限于VM目录?

2。是否可以在 Vagrantfile 中引用 VM 目录(test1、test2、test3 ...)之外的文件? (例如 /Users/hoge/vagrant/key.json)

我的项目和Vagrantfile如下

/Users/hoge
  /vagrant
    - key.json

    /test1
      - Vagrantfile
      - provision.sh
      - /workspace

    /test2
      ...

    /test3
      ...

# -*- mode: ruby -*-
# vi: set ft=ruby :

Vagrant.configure("2") do |config|

  # Every Vagrant development environment requires a box. You can search for
  # boxes at https://vagrantcloud.com/search.
  config.vm.box = "ubuntu/bionic64"

  # Directory ./workspace needs to be made in advance, or error occurs.
  config.vm.synced_folder "./workspace", "/home/vagrant/workspace"

  # Provider-specific configuration so you can fine-tune various
  # backing providers for Vagrant. These expose provider-specific options.
  # Example for VirtualBox:
  #
  config.vm.provider "virtualbox" do |vb|
    # Display the VirtualBox GUI when booting the machine
    vb.gui = false

    # Customize the number of cpus on the VM:
    vb.cpus = "2"
    
    # Customize the amount of memory on the VM:
    vb.memory = "2048"

    vb.customize ["modifyvm", :id, "--ioapic", "on"]
  end
  #
  # View the documentation for the provider you are using for more
  # information on available options.

  # Enable provisioning with a shell script. Additional provisioners such as
  # Ansible, Chef, Docker, Puppet and Salt are also available. Please see the
  # documentation for more information about their specific syntax and use.

  config.vm.provision "shell", path: "./provision.sh", env: {"CLIENT_SECRET" => "$(base64 /Users/hoge/vagrant/key.json)"}
end

主机OS:MacOS Big Sur 11.5.1
访客 OS:Ubuntu 18.04 LTS(ubuntu/bionic64)
流浪者 2.2.17
虚拟机 6.1.26

Provisioner 不在主机上执行,而是在 VM 上执行 运行s(这就是为什么它 运行s 当你在 Vagrantfile 所在的 vagrant 目录上有文件时,因为这个目录是自动共享的/vagrant 在虚拟机上。

如果您想将文件保留在原处,您应该添加一个 file provisioner 以将密钥文件复制到 VM 上,然后 运行 shell 配置程序