Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to provision software using Vagrant without sudo

I'm trying to setup Vagrant virtual machines to support my learning through Seven Databases in Seven Weeks. I'm provisioning software using basic shell scripts which performs appropriate actions within a sudo environment. However, I'm using the vagrant user to run the tutorials, and would like the provisioning to install the appropriate node / NPM modules as Vagrant, rather than through sudo.

My current npm command is the last line in this provisioning script, but the module is unavailable when vagrant tried to execute node scripts.

apt-get update
apt-get -y install build-essential
apt-get -y install tcl8.5
wget http://redis.googlecode.com/files/redis-2.6.0-rc3.tar.gz
tar xzf redis-2.6.0-rc3.tar.gz
cd redis-2.6.0-rc3
make
make install
make test
mkdir /etc/redis
mv redis.conf /etc/redis/redis.conf
sed -i.bak 's/127.0.0.1/0.0.0.0/g' /etc/redis/redis.conf
sed -i.bak 's/daemonize no/daemonize yes/g' /etc/redis/redis.conf
sed -i.bak 's/dir .\//dir \/var\/lib\/redis/g' /etc/redis/redis.conf

cd src/
wget https://raw.github.com/gist/1053791/880a4a046e06028e160055406d02bdc7c57f3615/redis-server
mv redis-server.1 /etc/init.d/redis-server
mv redis-cli /etc/init.d/redis-cli
chmod +x /etc/init.d/redis-server
sed -i.bak 's/DAEMON=\/usr\/bin\/redis-server/DAEMON=\/usr\/local\/bin\/redis-server/g'     /etc/init.d/redis-server 
useradd redis
mkdir -p /var/lib/redis
mkdir -p /var/log/redis
chown redis.redis /var/lib/redis
chown redis.redis /var/log/redis
update-rc.d redis-server defaults
/etc/init.d/redis-server start
cd /etc/init.d/
echo ./redis-cli 
echo http://blog.hemantthorat.com/install-redis-2-6-on-ubuntu/

apt-get -y install python-software-properties python g++ make
add-apt-repository -y ppa:chris-lea/node.js
apt-get update
apt-get -y install nodejs

npm install hiredis redis csv
like image 230
Paul Williams Avatar asked May 04 '13 20:05

Paul Williams


People also ask

What is provision in Vagrant?

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.

What is the Provisioner that used in Vagrant by default?

Every Vagrant provisioner accepts a few base options. The only required option is what type a provisioner is: name (string) - The name of the provisioner.

What is Vagrant Linux sh?

Vagrant is an open-source tool that allows you to create, configure, and manage boxes of virtual machines through an easy to use command interface. Essentially, it is a layer of software installed between a virtualization tool (such as VirtualBox, Docker, Hyper-V) and a VM.

What is Shell provisioning?

The shell Packer provisioner provisions machines built by Packer using shell scripts. Shell provisioning is the easiest way to get software installed and configured on a machine. Building Windows images? You probably want to use the PowerShell or Windows Shell provisioners.


2 Answers

Simply set privileged to false in your VagrantFile like this:

Vagrant.configure(VAGRANTFILE_API_VERSION) do |config|
    ...
    config.vm.provision :shell, privileged: false, path: "script.sh"
    ...
end
like image 176
Martin Tapp Avatar answered Nov 15 '22 10:11

Martin Tapp


The shell provision runs as the root user. If you with to run as the vagrant user, you can do something like this:

sudo -u vagrant npm install hiredis redis

..or for multiple lines:

sudo -u vagrant << EOF
[...]
npm install hiredis
npm install redis
EOF
like image 35
dbr Avatar answered Nov 15 '22 09:11

dbr