On Thursday, October 18, 2018 at 1:17:49 PM UTC-7, Brian Cain wrote: > > On Thu, Oct 18, 2018 at 11:03 AM Darren S. <phatb...@gmail.com > <javascript:>> wrote: > >> So it did, thanks! I should have checked that first. >> >> Now that the pre-up trigger is running, I'm noticing that the action I'm >> trying to take (setting hostname in VM using sensor.vm.hostname) doesn't >> appear to be working as expected. Do values set in the trigger block >> survive outside scope of block? This is my trigger, and in the booted VM I >> can see that the default hostname is set, not the user-supplied one read >> into the hostname variable and set in sensor.vm.hostname. >> >> >> server.trigger.before :up do |trigger| >> login = Etc.getlogin >> trigger.info = "Checking for user-supplied server hostname prior >> to continuing" >> trigger.ruby do |env,machine| >> hostname = ENV["SERVER_HOSTNAME"] >> unless hostname >> STDERR.puts <<-MESSAGE >> [!] Before installing the server, set a unique hostname in the >> SERVER_HOSTNAME environment variable. >> Example: SERVER_HOSTNAME="test-server-#{login}" vagrant up >> MESSAGE >> abort >> end >> server.vm.hostname = hostname >> end >> end >> >> > Without seeing any output it's hard to know what might be going wrong, but > I think I've done close to what you are wanting here: > > Vagrant.configure("2") do |config| > config.vm.define "bork" do |b| > b.vm.box = "bento/ubuntu-18.04" > b.vm.hostname = "test.test" > > b.trigger.before :up do |t| > hostname = ENV["HELLO"] > t.ruby do |env,machine| > unless hostname > puts "it's not set" > abort > end > end > b.vm.hostname = hostname > end > b.vm.provision "shell", inline:<<-SHELL > hostname -f > SHELL > end > end > > brian@localghost:vagrant-sandbox % HELLO="hello" be vagrant up bork > ±[●][master] > Bringing machine 'bork' up with 'virtualbox' provider... > ==> bork: Running triggers before up ... > ==> bork: Running trigger... > ==> bork: Cloning VM... > ==> bork: Matching MAC address for NAT networking... > ==> bork: Checking if box 'bento/ubuntu-18.04' is up to date... > ==> bork: Setting the name of the VM: > vagrant-sandbox_bork_1539893707113_18201 > ==> bork: Fixed port collision for 22 => 2222. Now on port 2200. > ==> bork: Clearing any previously set network interfaces... > ==> bork: Preparing network interfaces based on configuration... > bork: Adapter 1: nat > ==> bork: Forwarding ports... > bork: 22 (guest) => 2200 (host) (adapter 1) > ==> bork: Booting VM... > ==> bork: Waiting for machine to boot. This may take a few minutes... > bork: SSH address: 127.0.0.1:2200 > bork: SSH username: vagrant > bork: SSH auth method: private key > bork: > bork: Vagrant insecure key detected. Vagrant will automatically replace > bork: this with a newly generated keypair for better security. > bork: > bork: Inserting generated public key within guest... > bork: Removing insecure key from the guest if it's present... > bork: Key inserted! Disconnecting and reconnecting using new SSH key... > ==> bork: Machine booted and ready! > ==> bork: Checking for guest additions in VM... > ==> bork: Setting hostname... > ==> bork: Mounting shared folders... > bork: /vagrant => /home/brian/code/vagrant-sandbox > ==> bork: Running provisioner: shell... > bork: Running: inline script > bork: hello > > > You should be able to set the guests hostname option within the up trigger > block. If you run Vagrant up with that you'll see that the env var will > override the hostname setting that I originally had set earlier in the > guest definition. >
Once again, thanks. I couldn't spot the problem in my side so I started to rebuild the config using yours as a reference and I think I found that the issue was I tried to set server.vm.hostname inside of the ruby block. Once I moved it outside the ruby block it remained in scope through the rest of the setup. All fixed up! - Darren > On Thursday, October 18, 2018 at 9:28:19 AM UTC-7, Brian Cain wrote: >>> >>> Hey there - >>> >>> This feature is actually only in the latest release of Vagrant, version >>> 2.2.0. Upgrading to that should resolve your config issue! >>> >>> On Thu, Oct 18, 2018 at 9:20 AM Darren S. <phatb...@gmail.com> wrote: >>> >>>> Vagrant 2.1.5 >>>> macOS 10.13.6 >>>> >>>> >>>> My Vagrantfile: >>>> >>>> Vagrant.configure("2") do |config| >>>> config.vm.define "server" do |server| >>>> server.vm.box = "ubuntu/xenial64" >>>> >>>> server.vm.provider "virtualbox" do |vb| >>>> vb.cpus = 2 >>>> vb.memory = 4096 >>>> end >>>> >>>> server.trigger.before :up do |trigger| >>>> trigger.info = "Checking for user-supplied hostname prior to >>>> continuing" >>>> trigger.ruby do |env,machine| >>>> hostname = ENV["SERVER_HOSTNAME"] >>>> unless hostname >>>> trigger.warning = '[!] Before installing the server, set a >>>> unique ' \ >>>> << 'hostname in the SERVER_HOSTNAME environment variable.' >>>> trigger.abort = true >>>> end >>>> server.vm.hostname = hostname >>>> end >>>> end >>>> end >>>> end >>>> >>>> >>>> Errors out when attempting to run with the trigger as currently >>>> configured: >>>> >>>> $ vagrant up >>>> Bringing machine 'server' up with 'virtualbox' provider... >>>> ==> server: Running triggers before up ... >>>> ==> server: Running trigger... >>>> ==> server: Checking for user-supplied hostname prior to continuing >>>> There are errors in the configuration of this machine. Please fix >>>> the following errors and try again: >>>> >>>> trigger: >>>> * The following settings shouldn't exist: ruby >>>> >>>> >>>> Seems clear that I didn't specify something correctly, but I'm trying >>>> to follow the documentation under >>>> https://www.vagrantup.com/docs/triggers/ as closely as possible. >>>> >>>> One more question too, is it required by the statement: >>>> >>>> trigger.ruby do |env,machine| >>>> >>>> ... that a host/node be explicitly defined prior to use as I've done, >>>> or will it still work fine with the implicit "default" node? Docs at >>>> https://www.vagrantup.com/docs/triggers/configuration.html and >>>> https://www.vagrantup.com/docs/triggers/usage.html#ruby-option show it >>>> done on an explicit host but it's not clear if that's a requirement. >>>> >>> >>> If you want, you can make a "global" trigger that would fire with any >>> guest, but it would need to be defined outside of your "server" guest >>> definition in your Vagrantfile. The ruby option should still work as a >>> global trigger. >>> >>> >>>> >>>> - Darren >>>> >>>> -- >>>> 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/5b05a738-3fb7-4d0b-9593-92a66ead492d%40googlegroups.com >>>> >>>> <https://groups.google.com/d/msgid/vagrant-up/5b05a738-3fb7-4d0b-9593-92a66ead492d%40googlegroups.com?utm_medium=email&utm_source=footer> >>>> . >>>> For more options, visit https://groups.google.com/d/optout. >>>> >>> >>> >>> -- >>> Brian Cain >>> >> -- >> 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 <javascript:>. >> To view this discussion on the web visit >> https://groups.google.com/d/msgid/vagrant-up/f20f570f-6d7a-4add-b889-6fc647888d9b%40googlegroups.com >> >> <https://groups.google.com/d/msgid/vagrant-up/f20f570f-6d7a-4add-b889-6fc647888d9b%40googlegroups.com?utm_medium=email&utm_source=footer> >> . >> For more options, visit https://groups.google.com/d/optout. >> > > > -- > Brian Cain > -- 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+unsubscr...@googlegroups.com. To view this discussion on the web visit https://groups.google.com/d/msgid/vagrant-up/bf212596-39d4-4082-8748-34fa518b91d0%40googlegroups.com. For more options, visit https://groups.google.com/d/optout.