如何在没有sudo的情况下使用Vagrant配置软件

时间:2013-05-04 20:18:11

标签: provisioning sudo vagrant

我正在尝试设置Vagrant虚拟机以支持我通过Seven Databases in Seven Weeks学习。我使用基本shell脚本配置软件,该脚本在sudo环境中执行适当的操作。但是,我正在使用vagrant用户来运行教程,并希望配置将相应的节点/ NPM模块安装为Vagrant,而不是通过sudo。

我当前的npm命令是此配置脚本中的最后一行,但当vagrant尝试执行节点脚本时,该模块不可用。

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

4 个答案:

答案 0 :(得分:14)

只需在您的VagrantFile中将privileged设置为false,就像这样:

Vagrant.configure(VAGRANTFILE_API_VERSION) do |config|
    ...
    config.vm.provision :shell, privileged: false, path: "script.sh"
    ...
end

答案 1 :(得分:6)

shell配置作为root用户运行。如果您要以vagrant用户身份运行,则可以执行以下操作:

sudo -u vagrant npm install hiredis redis

..或多行:

sudo -u vagrant << EOF
[...]
npm install hiredis
npm install redis
EOF

答案 2 :(得分:1)

也许使用npm install -g在vm中全局安装它?

答案 3 :(得分:-1)

sed -i's /.* requiretty $ / Defaults!requiretty /'/ etc / sudoers