我可以加载一个加载另一个vagrantfile的vagrantfile吗?

发布于 2025-02-11 17:48:29 字数 2928 浏览 1 评论 0 原文

我正在尝试在我的Vagrantfiles森林中建立继承结构。基本上,我想要这样的东西:

/base
  Vagrantfile
  /testvm
    Vagrantfile
    /nginx-test
      Vagrantfile

testvm/vagrantfile 会加载 base/vagrantfile nginx-test/vagrantfile 将加载 testvm/vagrantfile 。在我的测试案例中,基本 vagrantfile 指定一个框类型(例如 bento/ubuntu-bentu-18.04 ),并为VM和其他一些内容设置1GB内存。 testvm vagrantfile 加载并将框覆盖到 bento/ubuntu-22.04 并设置一个不同的主机名。 nginx检测 vagrantfile 加载最后一个文件并设置其自己的主机名加添加了另一个配置块以安装nginx。

我已经尝试过了,但是我得到了此 SystemStackError:stack Level Too Deep 错误:

$ vagrant up
Vagrant failed to initialize at a very early stage:

There was an error loading a Vagrantfile. The file being loaded
and the error message are shown below. This is usually caused by
a syntax error.

Path: /Users/donseiler/vagrant_test/testvm/nginx-test/Vagrantfile
Line number: 5
Message: SystemStackError: stack level too deep

我一直在搜索,但还没有发现任何尝试多层加载的人。我可以从 base testvm 目录中运行 vagrant up ,以提高这些计算机。我无法从 nginx-test 目录(甚至 vagrant status 等)中运行任何流Vagrant命令,

这是文件内容:

/base/vagrantfile:

VAGRANTFILE_API_VERSION = "2"

NAME = "basevm"
Vagrant.configure(VAGRANTFILE_API_VERSION) do |config|
  # Default to bento bionic box
  config.vm.box = "bento/ubuntu-18.04"
  config.vm.hostname = NAME

  # Set default virtualbox provider settings
  config.vm.provider :virtualbox do |vb|
    vb.name = NAME
    vb.memory = "1024"
    vb.gui = false
  end

  config.vm.provision "shell", inline: "DEBIAN_FRONTEND=noninteractive apt-get update"
end

/base/base/ testvm/vagrantfile:

# Load parent Vagrantfile
load "../Vagrantfile"

# define hostname
NAME = "testvm"

Vagrant.configure(VAGRANTFILE_API_VERSION) do |config|
  # Change to jammy
  config.vm.box = "bento/ubuntu-22.04"

  config.vm.provision "shell", inline: <<-SHELL
    export DEBIAN_FRONTEND=noninteractive
    apt-get -y install curl
  SHELL
end

/base/base/testvm/nginx-test/vagrantfile:

# Load parent Vagrantfile
load "../Vagrantfile"

# define hostname
NAME = "nginx-test"

Vagrant.configure(VAGRANTFILE_API_VERSION) do |config|
  config.vm.provision "shell", inline: <<-SHELL
    export DEBIAN_FRONTEND=noninteractive
    apt-get -y install nginx
  SHELL
end

update:只要我可以在vagrantfiles使用绝对文件路径(或仅在 pwd中使用相对路径的vagrantfile,或者如果所有vagrantfiles都在同一目录中)。相对路径显然相对于PWD,因此,当 ../ vagrantfile 还试图加载 ../ vagrantfile 时,它是从 pwd 而不是来自父母的dir。然后,随着父母的vagrantfile一直试图从孩子的dir加载自己,直到流浪者的短路本身。

基本上,它从任何地方加载vagrantfiles,但其中命令是从 pwd 执行的,因此相对路径是始终相对于 pwd 而不是相对于Vagrantfile居住的地方。

无法使用相对路径会使事情的灵活性降低,但至少是一种可行的选择。

I'm trying to set up an inheritance structure in my forest of Vagrantfiles. Basically I wanted something like this:

/base
  Vagrantfile
  /testvm
    Vagrantfile
    /nginx-test
      Vagrantfile

The testvm/Vagrantfile would load the base/Vagrantfile and the nginx-test/Vagrantfile would load the testvm/Vagrantfile. In my test case, the base Vagrantfile specifies one box type (eg bento/ubuntu-18.04) and sets 1GB of memory for the VM and some other stuff. The testvm Vagrantfile loads that and overrides the box to bento/ubuntu-22.04 and sets a different hostname. The nginx-test Vagrantfile loads that last file and sets its own hostname plus adds another provisioning block to install nginx.

I've tried this but I get this SystemStackError: stack level too deep error:

$ vagrant up
Vagrant failed to initialize at a very early stage:

There was an error loading a Vagrantfile. The file being loaded
and the error message are shown below. This is usually caused by
a syntax error.

Path: /Users/donseiler/vagrant_test/testvm/nginx-test/Vagrantfile
Line number: 5
Message: SystemStackError: stack level too deep

I've been searching but haven't found anyone attempting multiple layers of loading yet. I can run vagrant up just fine from the base and testvm directories to bring those machines up. I can't run any vagrant commands from the nginx-test directory (not even vagrant status, etc)

Here are the file contents:

/base/Vagrantfile:

VAGRANTFILE_API_VERSION = "2"

NAME = "basevm"
Vagrant.configure(VAGRANTFILE_API_VERSION) do |config|
  # Default to bento bionic box
  config.vm.box = "bento/ubuntu-18.04"
  config.vm.hostname = NAME

  # Set default virtualbox provider settings
  config.vm.provider :virtualbox do |vb|
    vb.name = NAME
    vb.memory = "1024"
    vb.gui = false
  end

  config.vm.provision "shell", inline: "DEBIAN_FRONTEND=noninteractive apt-get update"
end

/base/testvm/Vagrantfile:

# Load parent Vagrantfile
load "../Vagrantfile"

# define hostname
NAME = "testvm"

Vagrant.configure(VAGRANTFILE_API_VERSION) do |config|
  # Change to jammy
  config.vm.box = "bento/ubuntu-22.04"

  config.vm.provision "shell", inline: <<-SHELL
    export DEBIAN_FRONTEND=noninteractive
    apt-get -y install curl
  SHELL
end

/base/testvm/nginx-test/Vagrantfile:

# Load parent Vagrantfile
load "../Vagrantfile"

# define hostname
NAME = "nginx-test"

Vagrant.configure(VAGRANTFILE_API_VERSION) do |config|
  config.vm.provision "shell", inline: <<-SHELL
    export DEBIAN_FRONTEND=noninteractive
    apt-get -y install nginx
  SHELL
end

UPDATE: It looks like I can do multiple loading just fine as long as I use absolute file paths to the Vagrantfiles (or only use relative paths in the pwd's Vagrantfile, or if all Vagrantfiles are in the same directory). The relative path apparently is relative to the pwd, so when ../Vagrantfile also tries to load ../Vagrantfile, it does so from pwd and not from the parent dir. Then it becomes somewhat of an infinite loop as the parent dir's Vagrantfile keeps trying to load itself from the child dir until vagrant short-circuits itself.

Basically, it loads Vagrantfiles from wherever but the commands in them are executed from the pwd, so relative paths are always relative to the pwd, NOT relative to where the Vagrantfile lives.

Not being able to use relative paths makes things a little less flexible but at least it's a working alternative.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文