`执行`作为流浪的用户,而不是root,与chef-solo?

前端之家收集整理的这篇文章主要介绍了`执行`作为流浪的用户,而不是root,与chef-solo?前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我正在尝试运行以下主厨命令:
# install zombiejs,q (promises),should,coffee-script & mocha                 
execute "install q and zombiejs" do                                             
  cwd "/home/vagrant"                                                           
  user "vagrant"                                                                
  action :run                                                                   
  command "npm install -g q zombie should mocha coffee-script"                  
end

但它一直失败,因为它无法找到npm:

execute[install q and zombiejs] (chef-redtail::default line 205) had an error: Errno::ENOENT: No such file or directory – npm install -g q zombie should mocha coffee-script

通过vagrant ssh登录到vagrant并手动运行命令可以很好地完成.以root身份登录,通过sudo -i并运行命令失败,原因与主厨配方失败相同(npm仅在本地安装为流浪者用户,而不是root用户:这就是我想要的).

那么……我怎样才能指定这个厨师执行块作为流浪者用户而不是root用户

更新:我认为这总结了我的问题:

(ssh) /vagrant git:backbone ❯ whoami
vagrant

(ssh) /vagrant git:backbone ❯ which npm
/home/vagrant/.local/bin/npm

(ssh) /vagrant git:backbone ❯ echo $PATH
/home/vagrant/.local/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games

(ssh) /vagrant git:backbone ❯ sudo -H -u vagrant -i echo $PATH
/home/vagrant/.local/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games

(ssh) /vagrant git:backbone ❯ sudo -H -u vagrant -i which npm
npm not found

通过http://tickets.opscode.com/browse/CHEF-2517

另一个更新:所以我只是注意到这种差异:

(ssh) /vagrant git:backbone ❯ sudo -H -u vagrant -i echo $PATH
/home/vagrant/.local/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games

(ssh) /vagrant git:backbone ❯ sudo -H -u vagrant -i export
HOME=/home/vagrant
LANG=en_US.UTF-8
LC_CTYPE=en_US.UTF-8
LOGNAME=vagrant
MAIL=/var/mail/vagrant
PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games
SHELL=/bin/zsh
SSH_AUTH_SOCK=/tmp/ssh-uJUopA4146/agent.4146
SUDO_COMMAND='/bin/zsh -c export'
SUDO_GID=1001
SUDO_UID=1000
SUDO_USER=vagrant
TERM=xterm
USER=vagrant

注意PATH的区别?导出没有列出/home/vagrant/.local/bin目录…为什么第一个echo会返回PATH,第二个导出返回PATH而没有它?这是问题吗?

解决方法

要以用户身份执行脚本或命令,您需要组合su -l和bash -i:
execute "npm_install" do
    command "su vagrant -l -c 'cd /shared-with-host/helperScripts/ && bash -i npm install -g q zombie should mocha coffee-script'" 
    action :run
  end

由于some bugs,chef没有为执行中的指定用户正确设置环境. narced133的解决方案无效.

猜你在找的Linux相关文章