Vagrant Provisioning


Provisioners in Vagrant allow you to automatically install software, alter configurations, and more on the machine as part of the vagrant up process.
This is useful since boxes typically are not built perfectly for your use case. Of course, if you want to just use vagrant sshand install the software by hand, that works. But by using the provisioning systems built-in to Vagrant, it automates the process so that it is repeatable. Most importantly, it requires no human interaction, so you can vagrant destroy and vagrant up and have a fully ready-to-go work environment with a single command. Powerful.
Vagrant gives you multiple options for provisioning the machine, from simple shell scripts to more complex, industry-standard configuration management systems.
When provisioning happens
Provisioning happens at certain points during the lifetime of your Vagrant environment:
·         On the first vagrant up that creates the environment, provisioning is run. If the environment was already created and the up is just resuming a machine or booting it up, they will not run unless the --provision flag is explicitly provided.
·         When vagrant provision is used on a running environment.
·         When vagrant reload --provision is called. The --provision flag must be present to force provisioning.
You can also bring up your environment and explicitly not run provisioners by specifying --no-provision.
Configuring provisioning
every provisioner is configured within your Vagrantfile using the config.vm.provision method call. For example, the Vagrantfile below enables shell provisioning:
config.vm.provision "shell", inline: "echo hello"
By default, provisioners are only run once, during the first vagrant up since the last vagrant destroy, unless the --provision flag is set, as noted above.
Optionally, you can configure provisioners to run on every up or reload. They will only be not run if the --no-provisionflag is explicitly specified. To do this set the run option to "always", as shown below:
Vagrant.configure("2") do |config|
  config.vm.provision "shell", inline: "echo hello",
    run: "always"
end
You can also set run: to "never" if you have an optional provisioner that you want to mention to the user in a "post up message" or that requires some other configuration before it is possible, then call this with vagrant provision --provision-with bootstrap.
If you are using the block format, you must specify it outside of the block, as shown below
Vagrant.configure("2") do |config|
  config.vm.provision "bootstrap", type: "shell", run: "never" do |s|
    s.inline = "echo hello"
  end
end
Running Provisioners
Provisioners are run in three cases: the initial vagrant upvagrant provision, and vagrant reload --provision.
--no-provision flag can be passed to up and reload if you do not want to run provisioners. Likewise, you can pass --provision to force provisioning.
The --provision-with flag can be used if you only want to run a specific provisioner if you have multiple provisioners specified. For example, if you have a shell and Puppet provisioner and only want to run the shell one, you can do vagrant provision --provision-with shell. The arguments to --provision-with can be the provisioner type (such as "shell") or the provisioner name (such as "bootstrap" from above).
Shell Provisioners
The Vagrant Shell provisioner allows you to upload and execute a script within the guest machine.
Shell provisioning is ideal for users new to Vagrant who want to get up and running quickly and provides a strong alternative for users who are not comfortable with a full configuration management system such as Ansible, Chef or Puppet.
Running external script
The shell provisioner can also take an option specifying a path to a shell script on the host machine. Vagrant will then upload this script into the guest and execute it. An example:
Vagrant.configure("2") do |config|
  config.vm.provision "shell", path: "script.sh"
end


<< Previous                                                                                                                                 



No comments:

Post a Comment