I have a list of 50 Linux servers that I need to SSH into and check that a key-value pair configuration parameter within a .conf file is set. I need to check that the key-value pair exists. If the key-value pair does exist, I need to modify the boolean value to FALSE. If the key-value pair does not exist, I need to create the key-value pair with value set to FALSE.
I have no issues with the SSH part and using a Here Document to execute a list of commands on the remote server. However, this particular solution requires some bash logic on the remote server for checking the key-value pair and doing operations depending on the results. Is there a way to incorporate bash scripting logic within the Here Document? If not, what are alternative approaches to handling this particular situation?
I suppose I could have a bash script on each server that I then call from within the Here Document that executes the logic I need on the remote server, but I was hoping for a solution that did not require additional files and administering said files on 50 servers.
Example code below to show the logic I am looking at implementing with some pseudocode. Thanks in advance for the help and suggestions.
#! /bin/bash
SERVER_LIST=/path/to/servers_list.txt
for server in $(cat ${SERVER_LIST}); do
ssh ${server} <<CommandList
# if key-value pair exists in my.conf
# modify value to FALSE
# else
# add key-value pair with value set to FALSE
CommandList
done
Example configuration file
[general]
setting1 = true
setting2 = false
setting3 = true
sed -i -e '/setting3/ s/true/false' /path/to/my.conf || sed '/general/,/^$/s/^$/setting3 = true\n/g' /path/to/my.conf
sed
is inadvisable.