1

i would like to find a program that maintains a list of commands that can be aliased for different hosts. for example, we have a database server, and four web servers. a common task is to login to the admin server and restart mysql using

# ssh dbserver "service mysql restart"

then we restart each of the web servers by running

# ssh webserver1 "service apache2 restart"
# ssh webserver2 "service apache2 restart"
# ssh webserver3 "service apache2 restart"
# ssh webserver4 "service apache2 restart"

I would like to have an package that keeps track of these commands and i could run them as such:

# blah dbserver,webserver1-4 "restartServices"

then it would loop through each host listed and run the commands necessary to restart the services on those boxes.

Nic
  • 13,025
  • 16
  • 59
  • 102
jaymz
  • 29
  • 3
  • adding information: we have over 2500 systems and remembering the list of commands to restart X service on Y system is cumbersome. – jaymz Jun 29 '12 at 23:22
  • 5
    If you have 2500 systems then may I suggest, that you may be looking at this completely backwards, and instead you should be looking for something like puppet + mcollective. – Zoredache Jun 30 '12 at 00:03
  • 2
    Please include additional details in the question itself, not in comments. – EEAA Jun 30 '12 at 00:53
  • @Zoredache do you have an example of how mcollective would accomplish this? – jaymz Jul 02 '12 at 20:09
  • I haven't actually used mcollective, just read a bit about it when I was reading through [Pro Puppet](http://www.amazon.com/Pro-Puppet-James-Turnbull/dp/1430230576). I deal with a smaller set, and I suspect far more diverse set of servers. I am not sure mcollective would be as useful for what I am doing. From what I understand basically allow you to issue commands to a group of system that you can specify based on a large number of facts provided by facter that can be filtered with regular expressions. If you are interested, I suggest you grab a copy of the book I linked to. – Zoredache Jul 02 '12 at 20:40
  • I believe capistrano will do what i need, it is called namespacing https://github.com/leehambley/capistrano-handbook/blob/master/index.markdown#namespacing-tasks – jaymz Jul 02 '12 at 21:53

3 Answers3

3

You can write your own bash script for this.

#!/bin/bash
ssh server 1 "service apache2 restart"
ssh server 2 "service apache2 restart"
ssh server 3 "service apache2 restart" etc....

Then just run the script.

David W
  • 3,405
  • 5
  • 34
  • 61
  • Thanks for the suggestion, but we have 2500 servers with random start and stop commands. It's extremely difficult to remember them all and i don't want to do them in the same order every time. for example, what if i only want to restart the dbserver and a couple of the web servers? I will add information to my post to clarify the requirements. – jaymz Jun 29 '12 at 23:17
  • If you only want to restart the dbserver and a couple of the webservers, then... only restart the dbserver and a couple of the webservers. You can do all *sorts* of things with shell scripts. – womble Jun 30 '12 at 01:59
  • ... and if you want even more control, then use variables in the script to group multiple servers together and/or execute on stand-alone machines. Maybe something like ./run-my-script {server x} {server-group y}. – David W Jun 30 '12 at 11:32
1

This can be done in a few ways.

Perhaps the most simple way would be to use pssh (parallel ssh).

If your needs are more complex, you can use a framework like Fabric or MCollective.

EEAA
  • 108,414
  • 18
  • 172
  • 242
  • from what i can tell, pssh only will execute the same command on multiple systems. i'm looking for a command that helps keep a list of what commands i need to run. then using aliases i can use the same command to restart different services depending on which host i am trying to administer. i've looked at fabric and it is interesting but seems like more of a 'batch' system? is there some tutorial you know of that shows how to use it more flexibly? – jaymz Jun 29 '12 at 23:19
  • Well you gave a pretty irrelevant example then. In as large of an environment as you have, you're already *way* behind the game if you don't have this sorted already. As zoredache mentioned above, look into puppet and mcollective. You're going to need to do a large amount of customization regardless of what solution you choose. – EEAA Jun 30 '12 at 00:26
  • "keep a list of what commands i need to run" -- that is the *very* definition of a shell script. – womble Jun 30 '12 at 01:58
  • The example is exactly what i want. I've looked at fabric a bit deeper now, and I still can't see a way to get it where i need it. With a fabfile.py set out like this: from fabric.api import run, env, roles env.roledefs = { 'web': ['localhost'], 'dns': ['172.25.16.55'] } @roles('dns') def taskA(): run('ls') @roles('web') def taskA(): run('whoami') – jaymz Jul 02 '12 at 19:58
  • the above did not format correctly, but what i am looking for is a way to get fabric to run the following command: fab -R dns,web taskA where taskA runs different commands on dns then it does web. – jaymz Jul 02 '12 at 20:08
1

It sounds to me like what you want is rundeck. It comes with a nice web interface, but also works just as well on the command line.

Quinn Murphy
  • 443
  • 2
  • 7