Our Rails app is scaled at multiple machines, from time to time, we need to change settings at production.yml , right now we have to ssh into each server and do the editing at each machine individually.

What's the right way to handle this case?


Configuration management systems like Puppet and Chef are made for this exact purpose.

If you don't want to use them, you can use a script to SCP the file to the server:

while read host; do
    scp -i yourKey production.yml user@$host:/destination/production.yml
    # Add an ssh command here if you need to restart any services
done <  hosts.txt; 
  • currently we are not using those tools, wouldn't this approach involve full deployment cycle ? we only need this particular sync – simo Jan 29 at 14:12
  • Not necessarily, there's nothing wrong with using them to deploy files outside of the development cycle. Although they might be overkill for something small like this. I've edited my answer – Farhan.K Jan 29 at 14:21
  • 1
    I would consider using one of these tools if I were you. It makes it much easier to manage multiple machines. – Farhan.K Jan 29 at 14:27
  • If there are any chances that those files will be changed by someone else after you modify them , thus causing a configuration drift , I would suggest going with @Farhan.K's suggestion as Puppet and Chef will check periodically if there has been any change to the file and if so , change it to what you define . If not , Puppet and Chef could have an overhead since they are agent based , and in this case I would strongly recommend using Ansible which is agentless and uses SSH to connect to managed hosts. You could manage one file and push it to managed hosts. good luck – John Doe Jan 29 at 16:20
  • 1
    @pizdelect it is not horrible, in fact it is recommended practice and The Correct Way® to read multiple lines from a file. What is horrible is the for line in $(cat file) construct which can fail very easily. See bash pitfall #1. Note that all files should be terminated by a newline, if you have files that don't, then that's a whole different problem, but that's not an issue with read. – terdon Feb 1 at 9:36

Better to use rsync instead of scp. Advantages of rsync :

First, it will check whether any changes are there or not in source file when compared with file in destination. Only if there is a change then it will transfer so it will take less time and be faster when compared with scp.

while read host
    rsync -avzh production.yml userid@$host:/destination_location/production.yml
done < hosts.txt
  • using while read; do ... done < hosts.txt is horrible -- same comment as here. Also @terdon. – pizdelect Feb 1 at 9:21
  • 1
    @pizdelect no, it isn't. It is the best and safest way to read lines from a file. The absolute safest is while IFS= read -r var; do..., but this is not needed here. IN any case, using a while read loop instead of something truly awful such as for line in $(cat file) is both standard and the best practice recommended pretty much everywhere. Also see 1, 2, 3, 4, 5. – terdon Feb 1 at 9:42

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.