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.
Update Per Audience Feedback:
- Thanks to Madhu Nagaraju C: Create Jenkins job from config.xml
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 done
It works nicely. From my working experience, I do see two major improvement points:
- 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.
- 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 184.108.40.206 220.127.116.11 18.104.22.168 EOF # parallel run remote ssh commands ansible all -i my_hosts \ --private-key=$ssh_key_file \ -u root -m script -a "test.sh"