Search code examples
environment-variableschef-infraknife

Knife ssh not loading environment variables


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?


Solution

  • 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.