1

I would like to write a script that reads a text file that has all the nodes listed in there:

node1 node2 node3 . . .

  1. It creates a .conf file for each node in the /etc/icinga2/zones.d/master/hosts/new/ directory

  2. Copies the content of the file name windows-template into each new conf file.

  3. Then finds the phrase "hostname.hostdomain.com" in each conf file and replaces that with the filename minus the .conf. So for example, for node1, I will have node1.conf in which there is a phrase "hostname.hostdomain.com" which needs to be replaced with node1

  4. Then pings the hostname which is technically the filename minus ".conf" and replaces the 10.20.20.1 with the correct hostname.

I tried wrirting the script and part 1 and 2 work, part 3 works too but it replaces the hostname.hostdomain.com with "$f" which is not right. And I have no clue how to do number 4.

Can you please help?

Thank you

This is my windows-template.conf file:

    object Host "hostname.hostdomain.com" {
    import "production-host"
    check_command = "hostalive"
    address = "10.20.20.1"
    vars.client_endpoint = name

    vars.disks["disk C:"] = {
      disk_partition = "C:"
    }

    vars.os = "Windows"
}

object Zone "hostname.hostdomain.com" {
    endpoints = [ "hostname.hostdomain.com" ];
    parent = "master";
}
object Endpoint "hostname.hostdomain.com" {
    host = "10.20.20.1"
}

And this is my script:

#!/bin/bash

cd /etc/icinga2/zones.d/master/hosts/new

while read f; do
   cp -v "$f" /etc/icinga2/zones.d/master/hosts/new/"$f.conf"
   cp windows-template.conf "$f.conf"
   chown icinga:icinga "$f.conf"
   sed -i 's/hostname.hostdomain.com/$f/g' "$f.conf"
#   git add "$f.conf"
#   git commit -m "Add $f"
done < windows-list.txt

Thank you

2 Answers 2

2

You need double quotes for the shell to expand your variable. Try

sed -i "s/hostname.hostdomain.com/$f/g" "$f.conf"
Sign up to request clarification or add additional context in comments.

Comments

0

Does this work for you?

#!/bin/bash

cd /etc/icinga2/zones.d/master/hosts/new

while read f; do
   cp -v "$f" /etc/icinga2/zones.d/master/hosts/new/"$f.conf"
   cp windows-template.conf "$f.conf"
   chown icinga:icinga "$f.conf"
   sed -i "s/hostname.hostdomain.com/$f/g" "$f.conf"
   hostname=$( ssh -o StrictHostKeyChecking=no  "username@$f" -n "hostname" )
   mv "$f.conf" "${hostname}.conf"
#   git add "$f.conf"
#   git commit -m "Add $f"
done < windows-list.txt

Where username is your username, and I assume you copy your pub key to the hosts.

3 Comments

Thank you for your replies. The sed command worked, the node's IP did not replace the template IP (10.20.20.1) though. I would like the code to ping the node name, finds the IP and replace the node1.conf IP so instead of 10.20.20.1, it is the correct value. Will it be possible? Thanks again
This did the trick: ip=$( ping -c1 "$f" | sed -nE 's/^PING[^(]+(([^)]+)).*/\1/p') sed -i "s/10.20.20.1/$ip/g" "$f.conf"
@IrinaI, you might want to man getent. There are certainly easier ways to get an IP address than parsing the output of ping. In bash, I'd probably go with something like read ip hostnames < <(getent hosts "$thishost")

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.