From c2bd7084606079fcebd595398723fd99ba772f76 Mon Sep 17 00:00:00 2001 From: Rafael Gomez Date: Wed, 9 Oct 2024 09:57:24 +0100 Subject: [PATCH] Removing postgresql service from /etc/hosts only if service consul is registred and virtual IP is not present --- resources/providers/config.rb | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/resources/providers/config.rb b/resources/providers/config.rb index af3a816..7b71131 100644 --- a/resources/providers/config.rb +++ b/resources/providers/config.rb @@ -116,10 +116,16 @@ end end - # Check if postgresql is registered to delete postgresql in /etc/hosts + # Check if postgresql is registered in Consul consul_response = `curl #{node['ipaddress']}:8500/v1/catalog/services 2>/dev/null | jq .postgresql` postgresql_registered = (consul_response == 'null\n' || consul_response == '') ? false : true - if postgresql_registered + + # Check if the virtual IP field exists in the data bag + data_bag_response = `knife data bag show rBglobal ipvirtual-internal-postgresql 2>/dev/null` + virtual_ip_present = (data_bag_response.include?("id") && !data_bag_response.include?("id: null")) ? true : false + + # If PostgreSQL is registered in Consul and no virtual IP is set, remove postgresql from /etc/hosts + if postgresql_registered && !virtual_ip_present execute 'Removing postgresql service from /etc/hosts' do command "sed -i 's/.*postgresql.*//g' /etc/hosts" end