1

I have, what I think, is a complex task.

I have 1 machine we'll call a server and a whole bunch of other machines, hostX. They all run Ubuntu 16.04 server. Each host runs a simple bash script at startup, but that script currently has hard coded values. I would like to use variables within that script, but the variable values should be stored in files/a file on the server, allowing me to change values in one central place and affect a host, or a group of hosts. I'm happy that I'll have to either reboot, or restart the script on each host after a change, that's no problem. The issue I have is that I don't know how to use a variable from a remote machine, all I can find is info about running a script on a remote machine. I want it to run locally, but pull info from the remote.

So here's my work flow, in my head.

Host1 boots and looks at the contents of host1.cfg located on the server. host1.cfg looks like this:

NAME=John
ADDRESS='10 The High Street'
AGE=25

Host1 runs:

echo server:$HOSTNAME.cfg:$NAME & $ADDRESS & $AGE

Or maybe have a central HOST.CFG file with:

[HOST1]
NAME=John
ADDRESS='10 The High Street'
AGE=25

[HOST2]
NAME=Mike
ADDRESS='30 The Avenue'
AGE=80

Does that make sense?

dessert
  • 39,982
  • If I understand you correctly you could just use rsync or scp to copy the config file over and then use it locally. Or read it with e.g. wget or curl: https://stackoverflow.com/questions/8978261/how-can-i-cat-a-remote-file-to-read-the-parameters-in-bash – dessert Dec 09 '17 at 12:07
  • You think adding a line in the initial script to copy the remote file to the local host, then reading it on the next line is the way to go? I suppose that would be quick easy way. I'll play and let you know how I get on.

    Thanks

    – Preston Cole Dec 09 '17 at 12:13

1 Answers1

1

I'd go with a file for every single host containing the variables, let's call it host1.cfg, copy it over at the beginning of the script and source it:

rsync user@server:/path/to/host1.cfg .
source host1.cfg

This will overwrite any existing host1.cfg on the host, so updates to the server file apply on the next run. Be warned that source will execute whatever commands host1.cfg contain with no mercy. Make sure it can't be changed by anyone, especially if you run the script on the host as root. For alternative and maybe more secure approaches (depending on your setup) see: How do I read a variable from a file?

I like rsync, but you can very well also use scp, wget or curl for the task. If you don't need a local copy you can use (thanks to glenn jackman):

source <(ssh user@server "cat /path/to/host1.cfg")
dessert
  • 39,982