问题
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