Managing systems today, Linux or otherwise, really is all about automating routine tasks, security settings, profiles and much more. It may not seem important if you only have a couple systems, but if you’re creating new VMs all the time, it can help to automate some basic configurations.
There are a number of free tools to help you do that, namely Ansible, Chef, Puppet and Salt. Some of these tools work by having a master that communicates with each minion (some other system) by using a small application. Others just use ssh as the transport, which means you can automate remote systems without having to pre-install some app or agent.
Ansible is perhaps best known for its agentless approach, but Puppet recently introduced Bolt, which offers many of the same capabilities. As long as you have ssh (port 22) access to your remote machines, Ansible and Puppet Bolt can do a lot of mundane work for you.
Before you download one of those, though, keep in mind that the plain bash shell has some automation capabilities of its own that’s useful for some tasks. These can also be bundled into bash scripts and become pretty sophisticated.
For starters, though, say you want to copy your .bashrc file from your local machine to three others named vm01, vm02 and vm03. You can do that with a simple “for” statement:
$ for i in vm01 vm02 vm03; do scp .bashrc user@$i:/home/user/; done
In this example, the “i” becomes a variable that’s replaced by each item in the list of VMs. It’s like executing “scp .bashrc user@vm01:/home/user/” and “scp .bashrc user@vm02:/home/user/” and “scp .bashrc user@vm03:/home/user/”.
You can also do things like enable or disable a service or install the same package on any number of remote machines:
$ for i in vm01 vm02 vm03; do \
> ssh user@$i sudo zypper -n in cowsay;
In the above example, I’m install the application “cowsay” on each remote VM using zypper. I’m adding the -n option, which suppresses the need for user interaction.
Any command you would repeat over and over and over again can be executed this way with just the bash shell. You can make it even easier by adding ssh keys from your machine to all your remote hosts. That way, you’ll be able to skip any password authentication.
$ ssh-keygen # Generate a key pair on your host system
Copy the key to your remote host(s):
$ssh-copy-id -i ~/.ssh/id_rsa.pub user@remotehost
You’ll be prompted to enter the password for the username you provided. That’s it. Now you can ssh to those hosts without having to always enter your password.