【问题标题】:Vagrant cannot find files on Host OS in VagrantfileVagrant 在 Vagrantfile 中找不到主机操作系统上的文件
【发布时间】:2021-08-31 21:35:09
【问题描述】:

我试图在 Vagrantfile 中引用主机操作系统上的文件并设置环境变量。
但是,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

主机操作系统:MacOS Big Sur 11.5.1
来宾操作系统:Ubuntu 18.04 LTS(ubuntu/bionic64)
流浪者 2.2.17
虚拟机 6.1.26

【问题讨论】:

    标签: macos ubuntu vagrant vagrantfile


    【解决方案1】:

    provisioner 不在主机上执行,而是在 VM 上运行(这就是为什么当您在 Vagrantfile 所在的 vagrant 目录中有文件时它会运行,因为该目录会自动与 VM 上的/vagrant 共享。

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

    【讨论】:

    • 抱歉回复晚了。我不知道文件配置器。一切都按预期使用它。我应该更仔细地阅读文档。谢谢你的建议
    猜你喜欢
    • 1970-01-01
    • 2016-09-15
    • 1970-01-01
    • 2022-11-22
    • 1970-01-01
    • 2019-05-25
    • 2020-10-08
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多