Provision

Other topics

Remarks:

Provisioning is used to automatically configure a virtual machine. It is performed automatically when a virtual machine is first created (using vagrant up). It can also be re-run later using vagrant provision.

Minimal setup

Vagrantfile

Vagrant.configure("2") do |config|
  config.vm.box = "ubuntu/trusty64"
  config.vm.provision "ansible" do |ansible|
    ansible.playbook = "vagrant-playbook.yml"
  end
end

vagrant-playbook.yml

---
- hosts: default
  tasks:
    - name: Say hello
      debug:
        msg: 'Hello, World'

Launch and provision the box

vagrant up

By default the box will be provisioned.

Launch the box without provisioning

vagrant up --no-provision

Provision a running box

vagrant provision

Shell provisioner

The shell provisioner runs a shell script when provisioning.

$setup = <<SETUP
# You can write your shell script between here ...
sudo echo "Hello, World!" > /etc/motd.tail
# ... and here.
SETUP

Vagrant.configure("2") do |config|
  config.vm.box = "ubuntu/trusty64"
  config.vm.provision "shell", inline: $setup
end

Run shell script from file (not using inlining)

# provision/bootstrap-controller.sh : path and shell filename from vagrantfile location
config.vm.define "configcontroller" do |controller|
...
controller.vm.provision :shell do |shell|
    shell.path = "provision/bootstrap-controller.sh"
end
...

Syntax:

  • config.vm.provision "shell", inline: COMMANDS
  • config.vm.provision "shell", path: "relativePath/script.sh"

Parameters:

ParameterDetails
COMMANDSThe shell commands to run. Can be a string (e.g. "echo \"Hello, World!\"") or a string variable (e.g. $setup).

Contributors

Topic Id: 3091

Example Ids: 10113,10114,10115,10116,10520,13741

This site is not affiliated with any of the contributors.