Knife ssh not loading environment variables

霸气de小男生 提交于 2020-01-17 03:52:04

问题


On my node node_name I've got $JAVA_HOME and other environment variables set in /etc/profile. I'm aware (found that out) that knife ssh isn't a login shell, and so doesn't load the environment variables. Is there a way to load the environment variables without having to source it? Right now I'm forced to do

knife ssh 'name:nod_name' 'source /etc/profile; echo $JAVA_HOME'

I'm chaining in a few commands during knife ssh including some of the environment variables and loading the /etc/profile just makes that longer. Is there a way to load the /etc/profile during knife ssh?


回答1:


This has nothing to do with knife ssh, it is just how SSH works for commands executed directly over a connection. You can alternatively run a command like bash -l -c "something". In general you can't count on any specific way of setting env vars in non-interactive sessions as being portable, so caveat emptor.




回答2:


Not really an answer to the OP's question, but was searching for a solution to a similar problem. Thought I would share my situation and solution to maybe help someone. My issue was trying to run Chef remotely on our on-premise servers with awscli credentials in root's .bashrc. The -i switch was what I was missing in order to load root's environment variables in .bashrc.

Did not work:

knife ssh "name:$NODE" "sudo /etc/init.d/appserver stop; sleep 10;sudo chef-client -r role_appserver" -A -x user -P password

Worked:

knife ssh "name:$NODE" "sudo /etc/init.d/appserver stop; sleep 10;sudo -i chef-client -r role_appserver" -A -x user -P password



来源:https://stackoverflow.com/questions/25530160/knife-ssh-not-loading-environment-variables

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!