【问题标题】:How can I customize the output while 'vagrant up' runs?如何在“vagrant up”运行时自定义输出?
【发布时间】:2016-03-09 13:18:21
【问题描述】:

我想告诉流浪用户使用的机器有什么IP地址。我的第一个想法是使用 '/etc/rc.local' 并将 ifconfig 的输出打印到 '/vagrant' 目录中的一个文件中,但似乎这个目录是在调用 rc.local 之后挂载的。所以我需要另一种方式来通知用户而无需任何 ssh 登录。

我的第二个想法是将 ifconfig 输出写入某个“位置”,它会显示在 vagrant 启动输出中……如下面的示例所示。

...
default: Guest Additions Version: 4.3.10
default: VirtualBox Version: 5.0
==> default: Setting hostname...
==> default: Configuring and enabling network interfaces...
==> default: Mounting shared folders...
default: /vagrant => /home/user/vagrant/test
==> default: Machine already provisioned. Run `vagrant provision` or use the `--provision`
# start of desired output
Adresses found:
10.0.2.15
172.28.128.3
# end of desired output
==> default: flag to force provisioning. Provisioners marked to run always will still run.
...

欢迎所有想法。

【问题讨论】:

    标签: linux vagrant


    【解决方案1】:

    您可能对this SO answer 感兴趣,它试图实现将网络接口的IP 输出到vagrant up 上的终端的相同功能。

    答案中的相关位-

    关于客人:

    /sbin/ifconfig eth0 | grep 'inet addr:' | cut -d: -f2 | awk '{ print $1}
    

    应该给你这样的东西:

    10.0.2.15
    

    然后可以像这样在你的 Vagrantfile 中使用它:

    config.vm.provision "shell", inline: <<-SHELL
        sudo -i /vagrant/my_provisioning_script.sh $(/sbin/ifconfig eth0 | grep 'inet addr:' | cut -d: -f2 | awk '{ print $1}')
    SHELL
    

    这里的诀窍是知道哪个接口(上例中的eth0)有你想要的IP。当然,如果你擅长 grep 或 awk,你可以修改第一个命令来检查所有接口上的 IP……但这超出了我的能力范围。

    # content of Vagrantfile
    $infoScript = <<SCRIPT
      echo 'IP-addresses of the vm ...'
      ifconfig | grep 'inet A' | grep Bcast | awk '{print $2}' | sed 's/addr://'
    SCRIPT
    
    Vagrant.configure(2) do |config|
      config.vm.box = "ubuntu/trusty64"
      config.vm.box_check_update = false
      config.vm.network "private_network", type: "dhcp"
      config.vm.network "forwarded_port", guest: 80, host: 8888
      config.vm.provider "virtualbox" do |vb|
         vb.name = "demo"
         vb.customize ["modifyvm", :id, "--cpuexecutioncap", "50"]
         vb.memory = "2048"
         vb.cpus = 2
      end
      # normal provision to set up the vm
      config.vm.provision "shell", path: "scripts/bootstrap.sh"
      # extra provision to print all the ip's of the vm
      config.vm.provision "shell", inline: $infoScript,
          run: "always"
    end
    

    编辑 您可能还对vagrant-hostmanager 插件感兴趣,如果回显 IP 的目的只是为了让您可以连接到盒子,这个插件可以修改您的访客 或主机上的 /etc/hosts 所以您无需担心 IP,而是使用类似 http://mydevbox.local

    【讨论】:

    • 是的,我很感兴趣 :) 第一次上釉看起来不错。我忽略了对客人的规定。明天试试这个。
    • 解决方案是我正在寻找的解决方案,使用 'run: "always"' 扩展名,我在 ubuntu 客户机上的输出也有问题......所以我编辑了对我有用的答案,谢谢
    • @OkieOth 我已经编辑了帖子,还提到了流浪插件vagrant-hostmanager。如果您想要 IP 的原因只是为了连接到盒子,这有助于简化。
    【解决方案2】:

    首先,您需要确定如何获取 IP 地址(即 DHCP 或静态)。然后基于此,您基本上可以将私有网络代码添加到 vargrantfile 中:

    DHCP:

    Vagrant.configure("2") do |config|
      config.vm.network "private_network", type: "dhcp"
    

    结束

    静态:

    Vagrant.configure("2") do |config|
      config.vm.network "private_network", ip: "192.168.50.4"
    

    结束

    然后你可以添加 shell 和 ruby​​ 的混合物:

    $script = <<SCRIPT
      echo private_network...
      ip: "192.168.50.4"
    SCRIPT
    
    Vagrant.configure("2") do |config|
      config.vm.provision "shell", inline: $script
    

    结束

    希望对您有所帮助。

    【讨论】:

      【解决方案3】:

      由于其他答案没有描述如何从虚拟机内部发布动态数据的方式,所以我编写了我的解决方案。

      第 1 步:我在 vagrant vm 中放置一个网络服务器,并将访客端口 80 发布到主机端口 8888

      第 2 步:准备 guest 的 /etc/rc.local,以便它收集所有需要的动态 vm 信息并将输出写入到 webserver 根目录中的文件 'info.txt'

      第 3 步:向 vagrantfile 添加第二个配置条目,该条目运行每个 'vagrant up' 并通知用户在哪里可以获得更多信息

      # Vagrantfile
      ...
      config.vm.provision "shell", path: "scripts/bootstrap.sh"
      config.vm.provision "shell", inline: "echo 'check http://localhost:8888/info.txt for more information'",
        run: "always"
      ...
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2023-04-11
        • 2016-09-04
        • 2016-07-06
        • 1970-01-01
        • 2018-08-19
        • 2015-01-29
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多