Create a Post
cancel
Showing results for 
Search instead for 
Did you mean: 
Bernhard_Sayer
Contributor

Mass configuration file modification

Hi!

I want to create a mass configuration file for adding new host objects with different values and fitting name conventions.

In following configuration line, the variables "AAAAA", "BBBB", "XYZ" and "WWWWWW" should be replaced with values from a CSV oder text file:

add host name "AAAAA-node-svBBBBvh01" ip-address "10.XYZ.0.8" color "WWWWWW" groups.1 "Group-Vcenter"

The result should be like this:

add host name "G1AT1-node-svAT1vh01" ip-address "10.50.0.8" color "Red" groups.1 "Group-Vcenter"
add host name "G1AT2-node-svAT2vh01" ip-address "10.51.0.8" color "Red" groups.1 "Group-Vcenter"
add host name "G2DE1-node-svDE1vh01" ip-address "10.66.0.8" color "Blue" groups.1 "Group-Vcenter"
add host name "G2FR1-node-svFR1vh01" ip-address "10.67.0.8" color "Blue" groups.1 "Group-Vcenter"
add host name "G3US1-node-svUS1vh01" ip-address "10.99.0.8" color "Orange" groups.1 "Group-Vcenter"
add host name "G3CN1-node-svCN1vh01" ip-address "10.98.0.8" color "Orange" groups.1 "Group-Vcenter"
...

I know how to create a bash script, which changes one variable like the following in a loop. But i have no idea how to adapt this to meet my multi variable change requirement.

  while read p; do
    blabla $p
 done < table.txt

Thanks in advance,

Bernhard

0 Kudos
3 Replies
Vladimir
Champion
Champion

...or use excel to add few columns to CSV, upload to management server, run "dos2unix /var/log/script.txt" on it...

0 Kudos
Kaspars_Zibarts
Employee Employee
Employee

awk is your friend. It is possible to make a one-liner but for clarity i did it long way Smiley Happy

Assuming your CSV file looks like this

AAAAA,BBBB,XYZ,WWWWWW

while read line; do
   

# Read each parameter into own variable

place1=`echo $line | awk -F, '{print $1}'`
place2=`echo $line | awk -F, '{print $2}'`
place3=`echo $line | awk -F, '{print $3}'`
place4=`echo $line | awk -F, '{print $4}'`

   

cmdline="add host name \"${place1}-node-sv${place2}vh01\" ip-address \"10.${place3}.0.8\" color \"${place4}\" groups.1 \"Group-Vcenter\""

   

then you can use $cmdline string for running the command

done < inputfile

google awk samples Smiley Happy I haven't tested it but gives you idea Smiley Happy

0 Kudos
Bernhard_Sayer
Contributor

Thanks!

I did it successful today as follows:

1. Create a CSV file "name-ip-values.csv" with the required values of IP, Name, Color:

      200;G0AT0;dark gray

     ... .

2. Create a template file "template.txt" which includes all commands with variables:

      mgmt_cli add host name "AAAAA-node-svh01" ip-address "10.XYZ.0.8" color "WWWWWW"

     .. .

3. Create my first powerful bash script 🙂

      #/bin/bash!

IFS='\;'
while read IP SITE COLOR
do
        sed 's/XYZ/'"$IP"'/g ;
                s/AAAAA/'"$SITE"'/g ;
                s/WWWWWW/'"$COLOR"'/g' template.txt >> output-clish.conf
done < name-ip-values.csv
4. Run output-clish.conf
5. Happy 🙂

Leaderboard

Epsum factorial non deposit quid pro quo hic escorol.

Upcoming Events

    CheckMates Events