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?
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 andsource
it:This will overwrite any existing
host1.cfg
on the host, so updates to the server file apply on the next run. Be warned thatsource
will execute whatever commandshost1.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 usescp
,wget
orcurl
for the task. If you don't need a local copy you can use (thanks to glenn jackman):