Detecting machine state in Vagrantfile

classic Classic list List threaded Threaded
4 messages Options
Reply | Threaded
Open this post in threaded view
|

Detecting machine state in Vagrantfile

Sam Kaplan

Hi,

In a Vagrantfile, is it possible to detect whether a machine is already created? I'm trying to run a block of code only if "vagrant up" is creating a new box (and not when re-booting an existing one).

Even a provider-specific method would be helpful -- I'm only using Virtualbox.

Thanks,
Sam

--
This mailing list is governed under the HashiCorp Community Guidelines - https://www.hashicorp.com/community-guidelines.html. Behavior in violation of those guidelines may result in your removal from this mailing list.
 
GitHub Issues: https://github.com/mitchellh/vagrant/issues
IRC: #vagrant on Freenode
---
You received this message because you are subscribed to the Google Groups "Vagrant" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
To view this discussion on the web visit https://groups.google.com/d/msgid/vagrant-up/c3ef20a6-5ef1-4253-a076-43d381bda8fe%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Reply | Threaded
Open this post in threaded view
|

Re: Detecting machine state in Vagrantfile

Alvaro Miranda Aguilera
Hello

By default provisioning scripts only run at creation time.

Next time you do vagrant up/reload the provisioning doesn't run at all.

If there are some provisioning you want to have always to run, you can add    

,run: "always"


Does this helps?
Alvaro

On Mon, Nov 13, 2017 at 9:50 PM, Sam Kaplan <[hidden email]> wrote:

Hi,

In a Vagrantfile, is it possible to detect whether a machine is already created? I'm trying to run a block of code only if "vagrant up" is creating a new box (and not when re-booting an existing one).

Even a provider-specific method would be helpful -- I'm only using Virtualbox.

Thanks,
Sam

--
This mailing list is governed under the HashiCorp Community Guidelines - https://www.hashicorp.com/community-guidelines.html. Behavior in violation of those guidelines may result in your removal from this mailing list.
 
GitHub Issues: https://github.com/mitchellh/vagrant/issues
IRC: #vagrant on Freenode
---
You received this message because you are subscribed to the Google Groups "Vagrant" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
To view this discussion on the web visit https://groups.google.com/d/msgid/vagrant-up/c3ef20a6-5ef1-4253-a076-43d381bda8fe%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.



--
Alvaro

--
This mailing list is governed under the HashiCorp Community Guidelines - https://www.hashicorp.com/community-guidelines.html. Behavior in violation of those guidelines may result in your removal from this mailing list.
 
GitHub Issues: https://github.com/mitchellh/vagrant/issues
IRC: #vagrant on Freenode
---
You received this message because you are subscribed to the Google Groups "Vagrant" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
To view this discussion on the web visit https://groups.google.com/d/msgid/vagrant-up/CAHqq0ex1tghQPwe0SZSKs0OcbvfsbPUD9F3Pv_%2Bz%2B0jozJYEpg%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.
Reply | Threaded
Open this post in threaded view
|

Re: Detecting machine state in Vagrantfile

Sam Kaplan
Not quite...I'm not worried about provisioning scripts in this case.

I have a block in my Vagrantfile like:

  config.vm.provider "virtualbox" do |vb|
    vb.cpus = ENV["VAGRANT_CPU"] || "2"
    vb.memory = ENV["VAGRANT_MEM"] || "2048"
  end


so you can specify cpu and memory size in environment variables. But this also means you have to specify them every time you boot the machine, or they will get overwritten by the defaults. If I had a way to detect that the machine was already created, then I could avoid setting the config values in that case.

Thanks,
Sam

On Wednesday, November 15, 2017 at 3:46:37 AM UTC-5, Alvaro Miranda Aguilera wrote:
Hello

By default provisioning scripts only run at creation time.

Next time you do vagrant up/reload the provisioning doesn't run at all.

If there are some provisioning you want to have always to run, you can add    

,run: "always"


Does this helps?
Alvaro

On Mon, Nov 13, 2017 at 9:50 PM, Sam Kaplan <<a href="javascript:" target="_blank" gdf-obfuscated-mailto="q6bUefQQCAAJ" rel="nofollow" onmousedown="this.href=&#39;javascript:&#39;;return true;" onclick="this.href=&#39;javascript:&#39;;return true;">stka...@...> wrote:

Hi,

In a Vagrantfile, is it possible to detect whether a machine is already created? I'm trying to run a block of code only if "vagrant up" is creating a new box (and not when re-booting an existing one).

Even a provider-specific method would be helpful -- I'm only using Virtualbox.

Thanks,
Sam

--
This mailing list is governed under the HashiCorp Community Guidelines - <a href="https://www.hashicorp.com/community-guidelines.html" target="_blank" rel="nofollow" onmousedown="this.href=&#39;https://www.google.com/url?q\x3dhttps%3A%2F%2Fwww.hashicorp.com%2Fcommunity-guidelines.html\x26sa\x3dD\x26sntz\x3d1\x26usg\x3dAFQjCNGHa_CaKfZaJiwbkUndUQbVGlQCDw&#39;;return true;" onclick="this.href=&#39;https://www.google.com/url?q\x3dhttps%3A%2F%2Fwww.hashicorp.com%2Fcommunity-guidelines.html\x26sa\x3dD\x26sntz\x3d1\x26usg\x3dAFQjCNGHa_CaKfZaJiwbkUndUQbVGlQCDw&#39;;return true;">https://www.hashicorp.com/community-guidelines.html. Behavior in violation of those guidelines may result in your removal from this mailing list.
 
GitHub Issues: <a href="https://github.com/mitchellh/vagrant/issues" target="_blank" rel="nofollow" onmousedown="this.href=&#39;https://www.google.com/url?q\x3dhttps%3A%2F%2Fgithub.com%2Fmitchellh%2Fvagrant%2Fissues\x26sa\x3dD\x26sntz\x3d1\x26usg\x3dAFQjCNHxcA5DuHlQlPInMi_k1Pz4pMNqNw&#39;;return true;" onclick="this.href=&#39;https://www.google.com/url?q\x3dhttps%3A%2F%2Fgithub.com%2Fmitchellh%2Fvagrant%2Fissues\x26sa\x3dD\x26sntz\x3d1\x26usg\x3dAFQjCNHxcA5DuHlQlPInMi_k1Pz4pMNqNw&#39;;return true;">https://github.com/mitchellh/vagrant/issues
IRC: #vagrant on Freenode
---
You received this message because you are subscribed to the Google Groups "Vagrant" group.
To unsubscribe from this group and stop receiving emails from it, send an email to <a href="javascript:" target="_blank" gdf-obfuscated-mailto="q6bUefQQCAAJ" rel="nofollow" onmousedown="this.href=&#39;javascript:&#39;;return true;" onclick="this.href=&#39;javascript:&#39;;return true;">vagrant-up+...@googlegroups.com.
To view this discussion on the web visit <a href="https://groups.google.com/d/msgid/vagrant-up/c3ef20a6-5ef1-4253-a076-43d381bda8fe%40googlegroups.com?utm_medium=email&amp;utm_source=footer" target="_blank" rel="nofollow" onmousedown="this.href=&#39;https://groups.google.com/d/msgid/vagrant-up/c3ef20a6-5ef1-4253-a076-43d381bda8fe%40googlegroups.com?utm_medium\x3demail\x26utm_source\x3dfooter&#39;;return true;" onclick="this.href=&#39;https://groups.google.com/d/msgid/vagrant-up/c3ef20a6-5ef1-4253-a076-43d381bda8fe%40googlegroups.com?utm_medium\x3demail\x26utm_source\x3dfooter&#39;;return true;">https://groups.google.com/d/msgid/vagrant-up/c3ef20a6-5ef1-4253-a076-43d381bda8fe%40googlegroups.com.
For more options, visit <a href="https://groups.google.com/d/optout" target="_blank" rel="nofollow" onmousedown="this.href=&#39;https://groups.google.com/d/optout&#39;;return true;" onclick="this.href=&#39;https://groups.google.com/d/optout&#39;;return true;">https://groups.google.com/d/optout.



--
Alvaro

--
This mailing list is governed under the HashiCorp Community Guidelines - https://www.hashicorp.com/community-guidelines.html. Behavior in violation of those guidelines may result in your removal from this mailing list.
 
GitHub Issues: https://github.com/mitchellh/vagrant/issues
IRC: #vagrant on Freenode
---
You received this message because you are subscribed to the Google Groups "Vagrant" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
To view this discussion on the web visit https://groups.google.com/d/msgid/vagrant-up/824fbc3f-d644-4230-bc95-a425543aca49%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Reply | Threaded
Open this post in threaded view
|

Re: Detecting machine state in Vagrantfile

Alvaro Miranda Aguilera
Hello

There was an old email once that used ruby magic to detect if the id of the vm existed, but i don't think is needed.

check for a local file, if doesn't exist, set those values.

and in a provision script, just do `touch /vagrant/.provisioned` or similar.

will be more easy to read.

My 2 cents

alvaro.

On Wed, Nov 15, 2017 at 3:42 PM, Sam Kaplan <[hidden email]> wrote:
Not quite...I'm not worried about provisioning scripts in this case.

I have a block in my Vagrantfile like:

  config.vm.provider "virtualbox" do |vb|
    vb.cpus = ENV["VAGRANT_CPU"] || "2"
    vb.memory = ENV["VAGRANT_MEM"] || "2048"
  end


so you can specify cpu and memory size in environment variables. But this also means you have to specify them every time you boot the machine, or they will get overwritten by the defaults. If I had a way to detect that the machine was already created, then I could avoid setting the config values in that case.

Thanks,
Sam

On Wednesday, November 15, 2017 at 3:46:37 AM UTC-5, Alvaro Miranda Aguilera wrote:
Hello

By default provisioning scripts only run at creation time.

Next time you do vagrant up/reload the provisioning doesn't run at all.

If there are some provisioning you want to have always to run, you can add    

,run: "always"


Does this helps?
Alvaro

On Mon, Nov 13, 2017 at 9:50 PM, Sam Kaplan <[hidden email]> wrote:

Hi,

In a Vagrantfile, is it possible to detect whether a machine is already created? I'm trying to run a block of code only if "vagrant up" is creating a new box (and not when re-booting an existing one).

Even a provider-specific method would be helpful -- I'm only using Virtualbox.

Thanks,
Sam

--
This mailing list is governed under the HashiCorp Community Guidelines - https://www.hashicorp.com/community-guidelines.html. Behavior in violation of those guidelines may result in your removal from this mailing list.
 
GitHub Issues: https://github.com/mitchellh/vagrant/issues
IRC: #vagrant on Freenode
---
You received this message because you are subscribed to the Google Groups "Vagrant" group.
To unsubscribe from this group and stop receiving emails from it, send an email to vagrant-up+...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/vagrant-up/c3ef20a6-5ef1-4253-a076-43d381bda8fe%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.



--
Alvaro

--
This mailing list is governed under the HashiCorp Community Guidelines - https://www.hashicorp.com/community-guidelines.html. Behavior in violation of those guidelines may result in your removal from this mailing list.
 
GitHub Issues: https://github.com/mitchellh/vagrant/issues
IRC: #vagrant on Freenode
---
You received this message because you are subscribed to the Google Groups "Vagrant" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
To view this discussion on the web visit https://groups.google.com/d/msgid/vagrant-up/824fbc3f-d644-4230-bc95-a425543aca49%40googlegroups.com.

For more options, visit https://groups.google.com/d/optout.



--
Alvaro

--
This mailing list is governed under the HashiCorp Community Guidelines - https://www.hashicorp.com/community-guidelines.html. Behavior in violation of those guidelines may result in your removal from this mailing list.
 
GitHub Issues: https://github.com/mitchellh/vagrant/issues
IRC: #vagrant on Freenode
---
You received this message because you are subscribed to the Google Groups "Vagrant" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
To view this discussion on the web visit https://groups.google.com/d/msgid/vagrant-up/CAHqq0eysO_gcTMrQX3rzqSAoxm8MpmxEsBU6wSHWTeMDaLC_CA%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.