Run code in Vagrantfile only if

I want to display text on the screen at startup vagrant up(or vagrant provisionetc.) if and only if it is running . (For vagrant upit only starts for the first time or if it is forcibly used with --provision.)

How can I do that?

+4
source share
3 answers

Adding shell support is probably the easiest solution, at a low cost, which it runs on a virtual machine through SSH.

Another option is to use the vagrant-host-shell plugin :

Vagrant.configure('2') do |config|
  # other config and provisioners
  # [...]

  config.vm.provision :host_shell, inline: 'echo "Provisioned!"'
end

, Vagrantfile.;)

class EchoPlugin < Vagrant.plugin('2')
  class EchoAction
    def initialize(app, env)
      @app = app
    end

    def call(env)
      @app.call(env)
      puts "Provisioned!"
    end
  end

  name 'echo'

  action_hook 'echo' do |hook|
    hook.before Vagrant::Action::Builtin::Provision, EchoAction
  end
end

Vagrant.configure('2') do |config|
  # ...
end
+7

, , , , , vagrant up --provision - ?

, echo.

Vagrant Vagrantfile , , , - - .

+2

Vagrant issue # 7043, - @env[:provision_enabled], , . , , Vagrantfile:

, Vagrantfile . - , Vagrantfile - . , @env , Vagrantfile , , , Vagrantfile . -22 .

- ARGV Vagrantfile. - :

if ARGV.include?("up") || (ARGV.include?("reload") && ARGV.include?("--provision")) 
    ... 
end

Vagrantfile:

def provisioned?(vm_name='default', provider='virtualbox')
  File.exists?(File.join(File.dirname(__FILE__),".vagrant/machines/#{vm_name}/#{provider}/action_provision"))
end

def explicit_provisioning?()
   (ARGV.include?("reload") && ARGV.include?("--provision")) || ARGV.include?("provision")
end

Vagrantfile:

if (not provisioned?) || explicit_provisioning?
    ...
end
0

Source: https://habr.com/ru/post/1527775/


All Articles