Running Specific Shell Scripts with vagrant provision

2,944 views
Skip to first unread message

msr...@gmail.com

unread,
Jun 5, 2015, 12:24:27 PM6/5/15
to vagra...@googlegroups.com
Hello,

I have these three Shell provisioners in my Vagrantfile. 
After 'vagrant up' the first time, I like to run just the second Shell provisioner script "script2.sh".
I can only run the provision by type "vagrant provision --provision-with shell", which run all the shell scripts.

guest.vm.provision "shell" do |sh|
      sh.path = "./script1.sh"
end

guest.vm.provision "shell" do |sh|
      sh.path = "./script2.sh"
      sh.args = "centos"
end

guest.vm.provision "shell" do |sh|
      sh.path = "./script3.sh"
end

Any ideas?
Thanks

Jamie Jackson

unread,
Jun 5, 2015, 5:08:16 PM6/5/15
to vagra...@googlegroups.com
For provisioners I don't want to run more than once, I leave a "bookmark" file behind.


For what it's worth, here's some boilerplate (I ripped out of my Lucee installer):

#!/usr/bin/env bash
set -e

description="Lucee installation"
runfile_name="lucee_install"

while :
do
    case $1 in
        --provisioned-dir=*)
            provisioned_dir=${1#*=}        # Delete everything up till "="
            shift
            ;;
        # ...other option blocks
        --) # End of all options
            shift
            break
            ;;
        -*)
            echo "WARN: Unknown option (ignored): $1" >&2
            shift
            ;;
        *)  # no more options. Stop while loop
            break
            ;;
    esac
done

runfile="${provisioned_dir}/${runfile_name}"

if [ -f "${runfile}" ]; then
  echo "${description}: Already run."
  exit 0
fi

# do a bunch of stuff

touch "${runfile}" || true

I then call it with the following in my Vagrantfile:

  # Install Lucee
  config.vm.provision :shell, :path => "#{host_script_dir}/web/lucee_install.sh", :args => [
    "--provisioned-dir=#{vm_provisioned_dir}",
    "--lucee-installer-path-name=#{lucee_installer_path_name}",
    "--lucee-installer-download-url=#{lucee_installer_download_url}",
    "--lucee-password=#{password}",
    "--lucee-user=#{railo_user}"
  ]

Where:

host_script_dir = "C:/www/myproj_vagrant/scripts"
vm_provisioned_dir = "/vagrant/temp/provisioned"

Then, I've got a trigger to get rid of the bookmarks upon destroy:

  config.trigger.after :destroy, :stdout => true, :force => true do
    info "Removing provisioned directory contents: #{host_provisioned_dir}/*"
    FileUtils.rm_rf Dir.glob("#{host_provisioned_dir}/*")
  end

Where:

host_provisioned_dir = "C:/www/myproj/temp\provisioned"

Thanks,
Jamie

--
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.
For more options, visit https://groups.google.com/d/optout.

Chris Baldauf

unread,
Jun 5, 2015, 6:46:40 PM6/5/15
to vagra...@googlegroups.com
Alternatively, you can use the named provisioners feature as documented here: http://docs.vagrantup.com/v2/provisioning/basic_usage.html

A minimally viable example might look something like this:

# Vagrantfile
Vagrant.configure(2) do |config|
  config.vm.box = "ubuntu/trusty64"

  config.vm.provision "provisioner1", type: "shell", inline: "echo one"
  config.vm.provision "provisioner2", type: "shell", inline: "echo two"
  config.vm.provision "provisioner3", type: "shell", inline: "echo three"
end

$ vagrant up
...
==> default: Running provisioner: provisier1 (shell)...
    default: Running: inline script
==> default: one
==> default: Running provisioner: provisier2 (shell)...
    default: Running: inline script
==> default: two
==> default: Running provisioner: provisier3 (shell)...
    default: Running: inline script
==> default: three
...
$ vagrant provision --provision-with provisioner2
==> default: Running provisioner: provisioner2 (shell)...
    default: Running: inline script
==> default: stdin: is not a tty
==> default: two


-Chris

msr...@gmail.com

unread,
Jun 6, 2015, 2:03:21 PM6/6/15
to vagra...@googlegroups.com
Thanks Chris,  
Reply all
Reply to author
Forward
0 new messages