profile for Gajendra D Ambi on Stack Exchange, a network of free, community-driven Q&A sites

Thursday, December 7, 2017

Automating the updating of redhat linux ethernet files

So we were building this big VxRack for our customer and there were some challenges. Client had some pretty interesting demands too. As you know VxRack goes with VMware and Redhat linux too if you want. It is all powered by Dell Emc Scale IO.
Challenge:

  1. remove all lines except the one with UUID
  2. append some lines to the same file
as usual the file was at /etc/sysconfig/network-scripts/ifcfg-me3

1
cp ifcfg-me3ifcfg-me3.backup
back up the file

1
 awk '/UUID/' ifcfg-me3> temp
extract the line with UUID and save it to temp file

1
rm -rf ifcfg-me3
remove the original file

1
 mv temp ifcfg-me3

rename the temp file as the new ifcfg-me3

1
2
3
4
array=(
'BOOTPROTO=NONE'
'DEFROUTE=NO'
'NAME=me3')

create an array with the lines that you want to add


1
2
3
4
for i in "${array[@]}"
do
        echo $i >> ifcfg-me3
done

line 1 : iterate through each line
line 2 : telling shell to do stuff
line 3 : appending each line to ifcfg-me3
line 4 : finishing the loop


1
2
3
4
5
6
7
8
9
cp ifcfg-me3 ifcfg-me3.backup; awk '/UUID/' ifcfg-me3 > temp; rm -rf ifcfg-me3 ; mv temp ifcfg-me3
array=(
'BOOTPROTO=NONE'
'DEFROUTE=NO'
'NAME=me3')for i in "${array[@]}"
do
        echo $i >> ifcfg-me3

done

the whole code looks like this. notice that i have made many short shell scripts into just one line to make it all run at one shot.
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
path="/etc/sysconfig/network-scripts"
myfile="$path/ifcfg-me3"
backupfile="$path/ifcfg-me3.backup"
mytemp="$path/temp"

rm -rf $mytemp $backupfile

cp -i $myfile $backupfile; awk '/UUID/' $myfile > $mytemp; rm -rf $myfile ; mv $mytemp $myfile
array=(
'BOOTPROTO=NONE'
'DEFROUTE=NO'
'NAME=me3')for i in "${array[@]}"
do
        echo $i >> $myfile
done
lines vs meaning
1. location where it all happens
2. my filename and path
3. backup filename and path
4. path and temp filename
5. removing the existing temp or backup file in that location if any
The rest is something which we already know.

Now we have to just write another powershell+plink/babun/pscp script to copy this to around 70 redhat nodes and make it rain i mean run :)


No comments:

Post a Comment