0

I am writing a BASH script that will modify YML files on a local machine. I also want the bash script to also modify these same YML files on remote machines as well.

function checkLocalElasticConfig () {
local tLen=${#esConfig[@]}
echo "\nEnabling Xpack Security!"
sed -i 's/#xpack.security.enabled: false/xpack.security.enabled: true/g' /etc/elasticsearch/elasticsearch.yml

for ((i=0; i<${tLen}; i++));
do
    if grep "${esConfig[i]} /etc/elasticsearch/elasticsearch.yml"
    then
        echo "${esConfig[i]} is already written to config!"
    else
        echo "\nWriting ${esConfig[i]} to \/etc\/elasticsearch\/elasticsearch\.yml"
        echo "${esConfig[i]}" >> /etc/elasticsearch/elasticsearch.yml
    fi
done
}

The above function checks the local YML, sees if a setting exists or not, and if it does not, it writes to the file.

I want to be able to recycle the above function by prefixing SSH "root@$variable" so that I can write to remote configs as well, as the remote version is the same. Are there any suggestions anyone might have?

Candlejack
  • 25
  • 5
  • You might get a better response if you try to do what you have described, then ask for assistance with any specific problems you run in to. This can be more effective than asking for generic advice. This post describes some of these tips: https://meta.stackoverflow.com/questions/334822/how-do-i-ask-and-answer-homework-questions – Nathan Buesgens Feb 05 '20 at 20:37
  • You could save the function in your remote machine's `~/.bash_profile` then in your local machine use `ssh -t "source /home//.bash_profile; checkLocalElasticConfig"` – builder-7000 Feb 05 '20 at 20:51
  • 1
    @sergio, there's no reason for that kind of hack. One can use `declare -f someFunc` to emit text that, when remotely executed, will generate a copy of the `someFunc` function. Same for `declare -p someVar`. Thus, `ssh remoteHost "$(declare -f someFunc); someFunc ...args..."` Does The Right Thing, as we have duplicates in the knowledgebase already describing. – Charles Duffy Feb 05 '20 at 21:13

0 Answers0