Parallel Run Commands On Multiple Servers

If maintaining servers is one of your routine jobs, occasionally you will need to run commands on multiple servers. Say restart service, update firewall rules, grep files, etc.

It’s a bit boring and inefficient to manually ssh and run commands one by one.

Parallel Commands

Update Per Audience Feedback:

Manual steps are slow. If you have multiple servers, you will complain. If your network to servers is doggy, you will complain more. To automate this, people usually write a tiny bash script running a loop on the servers.

for server in $server_list; do
    ssh -i $ssh_key_file \
        -o StrictHostKeyChecking=no \
        -p $ssh_port root@$server

It works nicely. From my working experience, I do see two major improvement points:

  1. It can be faster, if we can run remote commands parallelly not sequentially. The more servers involved, the bigger difference it would be. In certain scenarios, it even means less downtime or faster response.
  2. Track command history and console output. This will be helpful for trouble shooting, since it enables us to better audit servers’ change history,

Using ansible, we can easily fix point #1 like below.

# install package
sudo pip install ansible

# define a list of hosts to manage
cat > my_hosts <<EOF

# parallel run remote ssh commands
ansible all -i my_hosts \
--private-key=$ssh_key_file \
-u root -m script -a ""

For the audit purpose in point #2, we can wrap up above ansible command as a Jenkins job: RunCommandOnServers.




PRs Welcome

Blog URL:

3 Responses to Parallel Run Commands On Multiple Servers

  1. That is a great way to set up a jenkins job by wrapping up the whole process into it. Is it possible for you to list out the steps or the way to use the Jenkins job that you have mentioned in the blog and even the way to test it too ?

    Thanks in Prior Denny !

    • Madhu, thanks for the feedback.

      From the blog post, we will a config.xml. Right?

      Create a jenkins from config.xml is easy.

        – Create a dummy Jenkins job from GUI. You can choose any proper job name, if you like. Let’s say RunCommandOnServers.
        – Go to $JOB_HOME/jobs, we will see a new folder with that job name. ($JOB_HOME represents jenkins home directory. In Ubuntu/CentOS, normally it is /var/lib/jenkins/.ssh/)
        – Under the job folder, we will see a file called config.xml
        – Overwrite config.xml with the content we have just downloaded.
        – Reload or restart jenkins
        – We will see the Jenkins job ready for your use. You might need to do some cosmetic customization. Which should be easy for you, Right?

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.