From bbe302764ee3f6a5671c6d8c9abf6ac62ad229d2 Mon Sep 17 00:00:00 2001 From: Kazuhiro MUSASHI Date: Wed, 3 Apr 2024 14:40:40 +0900 Subject: [PATCH] Get IPAddress of the server. --- cookbooks/loki/attributes.rb | 13 +++++++++++++ cookbooks/loki/setup.rb | 4 ++-- 2 files changed, 15 insertions(+), 2 deletions(-) diff --git a/cookbooks/loki/attributes.rb b/cookbooks/loki/attributes.rb index bffdee8..9fed441 100644 --- a/cookbooks/loki/attributes.rb +++ b/cookbooks/loki/attributes.rb @@ -1,8 +1,21 @@ # ------------------------------------------- # Specifying the default settings: # ------------------------------------------- +case run_command('grep VERSION_ID /etc/os-release | awk -F\" \'{print $2}\'').stdout.chomp +when "20.04", "22.04" + cmd = 'LANG=C ip a | grep "inet " | grep -v -E "(127|172)" | cut -d" " -f6 | perl -pe "s/\/.+//g"' + +when "18.04" + cmd = 'LANG=C /sbin/ifconfig | grep "inet " | grep -v -E "(127|172)" | cut -d" " -f10' + +else + cmd = 'LANG=C /sbin/ifconfig | grep "inet addr" | grep -v -E "(127|172)" | awk "{print $2;}" | cut -d: -f2 | cut -f 1 -d " " | tail -1' +end +ipaddr = run_command(cmd).stdout.chomp + node.reverse_merge!({ 'loki' => { + 'ipaddr' => ipaddr, 'url' => 'https://github.com/grafana/loki/releases/download/', 'zip' => 'loki-linux-amd64.zip', 'storage' => '/opt/loki/', diff --git a/cookbooks/loki/setup.rb b/cookbooks/loki/setup.rb index 58c3256..591e83e 100644 --- a/cookbooks/loki/setup.rb +++ b/cookbooks/loki/setup.rb @@ -13,7 +13,7 @@ template '/etc/loki/loki-config.yml' do group 'root' mode '644' - variables(ipaddr: node['consul']['ipaddr']) + variables(ipaddr: node['loki']['ipaddr']) notifies :restart, 'service[loki]' end @@ -36,7 +36,7 @@ template '/etc/consul.d/service-loki.json' do group 'consul' mode '644' - variables(ipaddr: node['consul']['ipaddr']) + variables(ipaddr: node['loki']['ipaddr']) notifies :reload, 'service[consul]' end