If you’re anything like me, you’ve gotten accustomed to commands like this:
ssh [user]@[remote.server.com]
If you find yourself connecting to the same machines repeatedly, save a few keystrokes by creating a handy alias for them. Create (or edit) “~/.ssh/config” and add as many of these as your little heart desires:
Host [the alias]
HostName [domain name or IP address]
User [the account to login as]
Now you don’t have to use the full command to access the machine, just use the alias! For example, here is how I access one of my DreamHost servers:
ssh dh
The same goes for SCP! So, to secure copy a file (my_file.txt) in my current directory to the same machine I would simply issue:
scp my_file.txt dh:
Ahh… that is easy on the fingers! What else can we do with SSH config files?
Even trivial apps need to be configured. I used to simply define my app config somewhere near the top of the file, as many others do.
However, this becomes troublesome in a few common scenarios:
You want to share your source code with somebody else, but not your super-secret password
Your application becomes more complex and multiple areas need access to configuration variables
Abstracting configuration out of your Ruby app and into a separate Yaml file is super-simple. Here’s some codey code to use as an example:
# this is 'myapp.rb'require'yaml'
CONFIG = Yaml.load_file("config.yml")unlessdefined? CONFIG
puts"Your super-secret password is #{CONFIG['password']}"
Can it get any easier than that? I submit that it, in fact, cannot get any easier. You probably want to know what the config.yml file looks like, huh:
# this is my 'config.yml'
username: sant0sk1
password: awesome
Now if you want to share your source with a friend, perhaps via git, you can just add config.yml to the .gitignore file in your repository and create a config-sample.yml which holds dummy values.