function readIpFile () {
configfile='node_list.txt'
[ $# -gt 0 ] && [ -r "$1" ] && configfile="$1"
sed -e 's/[[:space:]]*#.*// ; /^[[:space:]]*$/d' "$configfile" |
while read target role;
do
case "$role" in
m) esMaster="\"$esMaster\", \"$target\""
ssh -n "root@$target" "sed -i 's/#node\.master: false/node\.master: true/' /etc/elasticsearch/elasticsearch.yml"
ssh -n "root@$target" "sed -i 's/#node\.data: false/node\.data: false/' /etc/elasticsearch/elasticsearch.yml"
ssh -n "root@$target" "sed -i 's/#node\.ingest: false/node\.ingest: false/' /etc/elasticsearch/elasticsearch.yml"
echo "Node configuration is set!"
ssh -n "root@$target" "service elasticsearch start"
;;
k) kbip=$target
echo "The Kibana node IP is: $kbip"
echo "Beginning remote kibana deployment!"
deployRemoteKibana
;;
d) ssh -n "root@$target" "sed -i 's/#node\.master: false/node\.master: false/' /etc/elasticsearch/elasticsearch.yml"
ssh -n "root@$target" "sed -i 's/#node\.data: false/node\.data: true/' /etc/elasticsearch/elasticsearch.yml"
ssh -n "root@$target" "sed -i 's/#node\.ingest: false/node\.ingest: false/' /etc/elasticsearch/elasticsearch.yml"
echo "Node configuration is set!"
ssh -n "root@$target" "service elasticsearch start"
;;
i) ssh -n "root@$target" "sed -i 's/#node\.master: false/node\.master: false/' /etc/elasticsearch/elasticsearch.yml"
ssh -n "root@$target" "sed -i 's/#node\.data: false/node\.data: false/' /etc/elasticsearch/elasticsearch.yml"
ssh -n "root@$target" "sed -i 's/#node\.ingest: false/node\.ingest: true/' /etc/elasticsearch/elasticsearch.yml"
echo "Node configuration is set!"
ssh -n "root@$target" "service elasticsearch start"
;;
c) ssh -n "root@$target" "sed -i 's/#node\.master: false/node\.master: false/' /etc/elasticsearch/elasticsearch.yml"
ssh -n "root@$target" "sed -i 's/#node\.data: false/node\.data: false/' /etc/elasticsearch/elasticsearch.yml"
ssh -n "root@$target" "sed -i 's/#node\.ingest: false/node\.ingest: false/' /etc/elasticsearch/elasticsearch.yml"
echo "Node configuration is set!"
ssh -n "root@$target" "service elasticsearch start"
;;
di) ssh -n "root@$target" "sed -i 's/#node\.master: false/node\.master: false/' /etc/elasticsearch/elasticsearch.yml"
ssh -n "root@$target" "sed -i 's/#node\.data: false/node\.data: true/' /etc/elasticsearch/elasticsearch.yml"
ssh -n "root@$target" "sed -i 's/#node\.ingest: false/node\.ingest: true/' /etc/elasticsearch/elasticsearch.yml"
echo "Node configuration is set!"
ssh -n "root@$target" "service elasticsearch start"
;;
md) ssh -n "root@$target" "sed -i 's/#node\.master: false/node\.master: true/' /etc/elasticsearch/elasticsearch.yml"
ssh -n "root@$target" "sed -i 's/#node\.data: false/node\.data: true/' /etc/elasticsearch/elasticsearch.yml"
ssh -n "root@$target" "sed -i 's/#node\.ingest: false/node\.ingest: false/' /etc/elasticsearch/elasticsearch.yml"
echo "Node configuration is set!"
ssh -n "root@$target" "service elasticsearch start"
;;
mi) ssh -n "root@$target" "sed -i 's/#node\.master: false/node\.master: true/' /etc/elasticsearch/elasticsearch.yml"
ssh -n "root@$target" "sed -i 's/#node\.data: false/node\.data: false/' /etc/elasticsearch/elasticsearch.yml"
ssh -n "root@$target" "sed -i 's/#node\.ingest: false/node\.ingest: true/' /etc/elasticsearch/elasticsearch.yml"
echo "Node configuration is set!"
ssh -n "root@$target" "service elasticsearch start"
;;
mdi)ssh -n "root@$target" "sed -i 's/#node\.master: false/node\.master: true/' /etc/elasticsearch/elasticsearch.yml"
ssh -n "root@$target" "sed -i 's/#node\.data: false/node\.data: true/' /etc/elasticsearch/elasticsearch.yml"
ssh -n "root@$target" "sed -i 's/#node\.ingest: false/node\.ingest: true/' /etc/elasticsearch/elasticsearch.yml"
echo "Node configuration is set!"
ssh -n "root@$target" "service elasticsearch start"
;;
*) echo "Unrecognized node setting in node_list.txt!"
exit
esac
recordNodes
deployRemoteElastic
applyDiscoverHosts
done
}
Here is my goal with the above code. I have a text file. The text file contains the following.
#######
#Instructions to configure file
#
#
#######
xxx.xxx.xxx.xxx m
xxx.xxx.xxx.xxx k
xxx.xxx.xxx.xxx d
The above case statement should select based on the letter which is the last field in each row in the file. This letter should be saved to the $role variable. The IP address should be saved to $target, and $target should be appended to $esMaster. Both of these variables will be used outside the while loop.
Yet every time I execute my code, $kbip is empty, and I get this error
ssh: Could not resolve hostname : Name or service not known
How can I read from my file and insert field 1, and field 2, into their respective variables, while also making those variables available outside the scope of the while loop? From what research I have done, this BASH script should not even be creating a subshell?
I am considering invoking the remote deployment functions inside the while loop, where it will make use of the variables inside the subshell? I dont want to do that though as I want to try to keep all my function calls relatively grouped together so I can move them around as needed.
I am not sure what better way there is to iterate through the file though.