ssh-copy-id automation with a list of hosts


Warning: count(): Parameter must be an array or an object that implements Countable in /home/fbsqlcom/public_html/youdidwhatwithtsql.com/wp-content/plugins/wp-codebox/main.php on line 31

Here’s another version of my ssh-copy-id script this time using a text file containing a list of hosts. The hosts file should contain a single host per line.

#!/bin/bash
 
export SSH_USER="user"
read -s PASSWORD
export PASSWORD
 
 
while read HOST; do
            export HOST;
            expect -c '
            set SSH_USER $env(SSH_USER)
            set HOST $env(HOST)
            set PASSWORD $env(PASSWORD)
            spawn ssh-copy-id $SSH_USER@$HOST
            expect {
                        "continue" {
                                    send "yes\n";
                                    exp_continue
                        }
                        "assword:" {
                                    send "$PASSWORD\n";
                        }
            }
            expect eof'
            echo "Done $HOST"
done < "$1"

Execute the script and pass the path to the text file as a parameter. i.e.

./auto_ssh.sh /path/to/host/list.txt;

Creating a Vagrant, Virtualbox & Ansible environment in the Windows Linux Subsystem

I’ve just been given a new Windows corporate laptop, with a huge amount of RAM (64GB), a large number of cores, and I wanted to start using this as my main development virtualisation platform. I do a lot of stuff with Vagrant, Ansible and VirtualBox and Windows hasn’t always been a welcome home for this setup. A more welcoming experience can be received through the Windows Linux Subsystem (WLSS) and is a big improvement over Cygwin. The instructions here used Debian 9.5 but should work on many other Linux distributions with minor modifications (i.e. package manager).

First install Debian (or other distro) from the official instructions (easiest way is through the MS App Store).

Then update the OS…

sudo apt-get update && sudo apt-get upgrade;

The install Python and pip…

sudo apt install python;
sudo apt install python-pip;

Now Ansible can be installed through pip…

sudo pip install ansible

Next download Vagrant and install it…

VAGRANT="https://releases.hashicorp.com/vagrant/2.1.5/vagrant_2.1.5_x86_64.deb";
wget "$VAGRANT";
sudo apt install ./$(basename "$VAGRANT");

If you’re behind a proxy you probably need this Vagrant plugin

vagrant plugin install vagrant-proxyconf;

Next install git…

sudo apt install git;

Finally we need to install VirtualBox. Don’t rush ahead and install the Linux version. I did and this and it does not work. Grab the latest Windows version and install that on the host system in the usual way.

Next back in the WLSS Debian shell we need to make a few modification to allow the Windows Version of VirtualBox to be used. Basically we allow Vagrant to access the Windows version of VB and set the proxy. Edit or delete these as necessary…

cd
echo 'export VAGRANT_WSL_ENABLE_WINDOWS_ACCESS="1"' >> .profile
echo 'export PATH="$PATH:/mnt/c/Program Files/Oracle/VirtualBox"' >> .profile
echo 'export VAGRANT_HTTP_PROXY=${http_proxy}' >> .profile
source .profile

Next let’s clone a Vagrant / Ansible / Virtual project to test the setup out…

mkdir git && cd git;
git clone https://github.com/rhysmeister/Jenkins.git
cd Jenkins
vagrant up

This is one my own projects setting up a Jenkins instance. It’s fairly simple but it will test all components of the setup we have just installed.

Vagrant: Create a series of VMs from a hostname array

I couldn’t find any examples of creating VMs from a array of strings online so sat down to work something out myself. Here’s how you do it…

Vagrant.configure("2") do |config|
 
  [ "web1",
    "db1",
    "web2",
    "db2",
    "backup1",
    "backup2",
    "admin1" ].each do |host|
      config.vm.define "#{host}" do |nrpe|
        nrpe.vm.box = "bento/centos-7.5"
        nrpe.vm.provider :virtualbox do |vb|
          vb.customize [
            "modifyvm", :id,
            "--name", "#{host}",
            "--memory", "1024"
          ]
          vb.cpus = 2
        end
        config.vm.hostname = "#{host}"
        config.vm.provision :ansible do |ansible|
          ansible.playbook = "basic.yml"
        end
      end
  end
end

This Vagrantfile will create a VM for each hostname in the array as well as running the basic.yml Ansible playbook against it. You can fire it up with…

vagrant up

Once booted you can view the status of the created vms…

vagrant status
Current machine states:

web1                   running (virtualbox)
db1                    running (virtualbox)
web2                   running (virtualbox)
db2                    running (virtualbox)
backup1                running (virtualbox)
backup2                running (virtualbox)
admin1                 running (virtualbox)

Automate ssh-copy-id with numbered hosts

Here’s a script I use to automate ssh-copy-id when I need to add a series of hosts using a incremental node number. For example…

prod-db-server001
prod-db-server002
prod-db-server003

and so on. The script uses expect to perform its work. To adjust this for your own purposes you simply need to change the SSH_USER variable, the number of hosts in the for loop and of course the hostname scheme. Once you execute the script you’ll enter your password once and ssh-copy-id will be performed for all the hosts in sequence.

#!/bin/bash
 
set -x;
 
export SSH_USER="admin"
read -s PASSWORD
export PASSWORD
 
for node in {1..91}; do
        if (( $node &lt;= 9 )); then
                export HOST=hostname00${node}.domain.ch
        else
                export HOST=hostname0${node}.domain.ch
        fi;
 
    expect -c '
    set SSH_USER $env(SSH_USER)
    set HOST $env(HOST)
    set PASSWORD $env(PASSWORD)
    spawn ssh-copy-id $SSH_USER@$HOST
    expect {
        "continue" {
            send "yes\n";
            exp_continue
        }
        "assword:" {
            send "$PASSWORD\n";
        }
    }
    expect eof'
 
    echo "Done $HOST"
done;

Ansible Playbook for Raspberry Pi Headphones Setup

I’ve created another Ansible Playbook for the Raspberry Pi to setup Headphones. It’s hosted over on my Github: PiHeadphones

The playbook can be execute with the following command…

ansible-playbook -i inventory headphones.yaml

The inventory file should contain the name of your Raspberry Pi and should already be setup for ssh. The playbook will clone the git repo and setup Headphones as a service. Afterwards there’s a little manual configuration to do in the web interface which is available at http://yourraspberrypi:8181/