Skip to content
Advertisement

How/where to provide sudo password for Vagrant shell provisioners?

I am trying to build a Vagrant box (CentOS) that will be provisioned by an install.sh shell script. This script will do several things, the first of which, involves creating the correct directory structure under /opt so that my service can be installed there and do other things, like writing logs there, as well.

So my Vagrant project (so far) consists of:

my-app-vagrant/
    Vagrantfile
    install.sh

Where install.sh looks like:

mkdir /opt/myapp
mkdir /opt/myapp/bin        # Where we will install binary to (later in this script)
mkdir /opt/myapp/logs       # Where the binary will write logs to

Now the binary does not need elevated privileges in order to run (it is configured via command-line arguments where to write logs to). However I simply want it to live under /opt with the above directory structure, at least for this particular machine.

The problem is that /opt is owned by root. Which means I need to run these mkdirs with sudo, provide the script the password for sudo, and then tweak directory permissions so that when the app runs, it has permission to both run and to write logs to my intended destination (which again, is /opt/myapp/logs). So I tweaked install.sh to look like this:

mkdir /opt/myapp
mkdir /opt/myapp/bin
mkdir /opt/myapp/logs

chmod -R 777 /opt/myapp     # Now when the app runs as a normal non-privileged user, we can run + write logs

And I know that I can provide a password to the script via echo <rootPswd> | sudo -S sh install.sh (where <rootPswd> is the correct root password).

Now I’m trying to figure out how to get this running/working correctly when Vagrant is provisioning the VM.

My Vagrant file looks like:

Vagrant.configure(2) do |config|
    config.vm.provision "shell", path: "install.sh"

    config.vm.box = "centos7"
    config.vm.box_url = "https://github.com/tommy-muehle/puppet-vagrant-boxes/releases/download/1.1.0/centos-7.0-x86_64.box"

    config.vm.network "private_network", ip: "10.0.1.2"

    config.vm.provider "virtualbox" do |vb|
        vb.memory = "2048"
    end
end

But what I’m stuck on is: how do I extend the whole “echo <rootPswd> | sudo -S sh install.sh“-concept to Vagrant? According to their docs there is a privileged option that I might be able to use, but it is set to true by default anyways.

But nowhere in their docs do they explain how/where to provide the sudo password that should be used (at least from what I have been able to find so far).

So I ask:

  • How do I provide the sudo password for a Vagrant VM’s shell provisioner’s installation script?; and
  • Where can I find out what the sudo password even if, given the base Vagrant box that I’m trying to use?

Advertisement

Answer

Turns out that (for almost all Vagrant boxes) the vagrant user is listed in /etc/sudoers with ALL=(ALL) NOPASSWD:ALL permissions, which instructs Linux to not ask that user for a “sudo password”, ever.

Hence, you don’t need to supply your privileged user with a sudo password.

User contributions licensed under: CC BY-SA
1 People found this is helpful
Advertisement