Sobes.tech
Middle

If you have many machines and no Ansible and Python, how would you install software on them?

sobes.tech AI

Answer from AI

You can use bash scripts that will be executed via SSH or distributed by other means.

  • SSH with loops:

    #!/bin/bash
    
    # List of servers
    SERVERS=("server1" "server2" "server3")
    
    # Command to install the package
    INSTALL_CMD="sudo apt-get update && sudo apt-get install -y nginx"
    
    for server in "${SERVERS[@]}"
    do
      echo "Connecting to $server..."
      ssh $server "$INSTALL_CMD"
      if [ $? -eq 0 ]; then
        echo "Installation on $server successful."
      else
        echo "Installation error on $server."
      fi
    done
    

    This approach requires SSH access with key-based authentication without a password.

  • Parallel SSH execution: To speed up, commands can be run in parallel, for example, using GNU Parallel:

    #!/bin/bash
    
    # List of servers
    SERVERS=("server1" "server2" "server3")
    
    # Command to install the package
    INSTALL_CMD="sudo apt-get update && sudo apt-get install -y htop"
    
    # Run command in parallel on all servers
    printf "%s\n" "${SERVERS[@]}" | parallel -j 10 ssh {} "$INSTALL_CMD"
    

    Requires pre-installation of parallel on the machine from which the script is run.

  • Deploying packages (Deb/RPM): Creating installation packages and distributing them to servers with subsequent local installation.

    #!/bin/bash
    
    # Copying the package to the server
    SERVER="server1"
    PACKAGE="my-app_1.0.deb"
    scp $PACKAGE $SERVER:/tmp/
    
    # Installing the package via SSH
    ssh $SERVER "sudo dpkg -i /tmp/$PACKAGE && sudo apt-get install -f -y"
    
  • Cloud init scripts: If machines are deployed in the cloud, user-data or cloud-init scripts can be used for automatic software installation during machine startup.

  • PXE and post-installation scripts: For Bare Metal servers, PXE booting and automatic OS installation with scripts that perform software installation after OS installation can be configured.

The choice of method depends on the number of machines, network topology, automation needs, and existing infrastructure. Bash and SSH are basic tools available on almost any Linux system.