hashicorp / vagrant

Vagrant is a tool for building and distributing development environments.
https://www.vagrantup.com
Other
26.26k stars 4.43k forks source link

File upload source file F:/junk/07/provision/generated/host-localip must exist #12324

Open survivant opened 3 years ago

survivant commented 3 years ago

I have this scenario. I will create VM and provision them.

but I'll generate a file in the BEFORE -PROVISION STEP. the file doesn't exist yet, but it will before the provisionning.

I have a validation error when I do

PS F:\junk\07> vagrant up
There are errors in the configuration of this machine. Please fix
the following errors and try again:

File provisioner:
* File upload source file F:/junk/07/provision/generated/host-localip must exist

PS F:\junk\07>
Vagrant.configure("2") do |config|
  config.vm.box = "generic/ubuntu2004"

  # cleanup
  config.trigger.before :provision do |trigger|
      trigger.info = "More information"
      trigger.run = {inline: "del provision/generated/*"}
  end
  config.trigger.before :provision do |trigger|
      trigger.info = "More information"
      trigger.run = {inline: "(Test-Connection -ComputerName (hostname) -Count 1).IPV4Address.IPAddressToString > provision/generated/host-localip"}
  end  

  # mount SMB folder for all VMs
  config.vm.provision "file", source: "provision/generated/host-localip", destination: "/home/vagrant/provision/generated/host-localip"
...
survivant commented 3 years ago

but If I create a empty file vagrant up the validation will passed but the file will be empty on the vm :(

soapy1 commented 3 years ago

Hey there, thanks for opening up an issue. This is expected Vagrant behavior. config.trigger.before :provision do |trigger| will get triggered when a vagrant provision command is run. You might want to use typed triggers instead to run those triggers on every provisioning action. Or, config.trigger.before :up do |trigger|

survivant commented 3 years ago

but my problem is also.. why the destination file must exists ?

soapy1 commented 3 years ago

The destination file does not need to exist, only the source file. If you have a scenario where the destination file must exist, could you share a debug log and the associated Vagrantfile. I could not reproduce that situation with the Vagrantfile in the original issue report.

survivant commented 3 years ago

yep.. it will works with before: up

survivant commented 3 years ago

finally .. the issue is still there for me. The source must be present. There is a way to tell Vagrant to not validate a provision command ?

# generate files for provisionning at startup
  config.trigger.before :up do |trigger|
    trigger.info = "init script before provisionning"
    trigger.run = {inline: "provision/preparing-provision.ps1"}
  end 

  # copy SMB files to all VMs and mount folder
  config.vm.provision "file", source: "provision/generated/host-localip", destination: "/home/vagrant/host-localip"

here my usecase. When I do : vagrant up

there is a script that will run locally (preparing-provision.ps1). That script will remove the folder provision/generated and recreate the file : provision/generated/host-localip

so when I first start the folder generated will not exists. Vagrant will give me the error :

PS F:\junk\tutoriel\03-createvm-fixed-ip-provisionning> vagrant up
There are errors in the configuration of this machine. Please fix
the following errors and try again:

File provisioner:
* File upload source file F:/junk/tutoriel/03-createvm-fixed-ip-provisionning/provision/generated/host-localip must exist

PS F:\junk\tutoriel\03-createvm-fixed-ip-provisionning>
soapy1 commented 3 years ago

There is a way to tell Vagrant to not validate a provision command ?

No. Vagrant will always try to validate provisioners. I don't think this is likely to change.

One option is to manually create the file provision/generated/host-localip initially. Or, since the Vagrantfile is Ruby you can add a little snippet like

File.write("#{Dir.pwd}/provision/generated/host-localip", "")

Vagrant.configure("2") do |config|
  ...
end