@@ -28,7 +28,7 @@ def uninstall
2828 end
2929
3030 def uninstall_from ( host )
31- node_id = host == config . primary_host ? 1 : ( config . standby_hosts . index ( host ) || 997 ) + 2
31+ node_id = config . node_id_for ( host )
3232
3333 ssh_executor . execute_on_host ( host ) do
3434 cluster_output = begin
@@ -98,6 +98,7 @@ def setup_primary
9898 puts ' Setting up primary with repmgr...'
9999
100100 host = config . primary_host
101+ primary_node_id = config . node_id_for ( host )
101102 repmgr_config = config . repmgr_config_for ( host )
102103 version = config . version
103104 repmgr_password = normalize_repmgr_password ( secrets . resolve ( 'repmgr_password' ) )
@@ -208,7 +209,7 @@ def setup_primary
208209 unless cluster_show &.match? ( /primary/i )
209210 # Fallback: verify via repmgr metadata in the repmgr database
210211 db_check = executor . run_sql_on_backend ( self ,
211- ' SELECT type FROM repmgr.nodes WHERE node_id = 1;' ,
212+ " SELECT type FROM repmgr.nodes WHERE node_id = #{ primary_node_id } ;" ,
212213 postgres_user : 'postgres' ,
213214 database : repmgr_db ,
214215 tuples_only : true ,
@@ -250,7 +251,7 @@ def setup_standby(standby_host)
250251 _ = repmgr_config
251252 _ = secrets_obj
252253
253- node_id = config . standby_hosts . index ( standby_host ) + 2
254+ node_id = config . node_id_for ( standby_host )
254255 repmgr_password = normalize_repmgr_password ( secrets_obj . resolve ( 'repmgr_password' ) )
255256 replication_password = normalize_replication_password ( secrets_obj . resolve ( 'replication_password' ) )
256257 if replication_user == repmgr_user && replication_password != repmgr_password
@@ -679,6 +680,7 @@ def verify_cluster_health
679680 puts 'Verifying PostgreSQL HA cluster health...'
680681
681682 primary_host = config . primary_host
683+ primary_node_id = config . node_id_for ( primary_host )
682684 standby_hosts = config . standby_hosts
683685 version = config . version
684686 postgres_user = config . postgres_user
@@ -709,12 +711,12 @@ def verify_cluster_health
709711 cluster_output = capture ( :sudo , '-u' , 'postgres' ,
710712 'repmgr' , '-f' , '/etc/repmgr.conf' , 'cluster' , 'show' ,
711713 raise_on_non_zero_exit : false ) . to_s
712- # Primary always has node_id=1, check if it's registered and running
713- if cluster_output . match? ( /\s +1 \s +\| .*primary.*\* \s +running/i )
714+ # Check the configured node identity, which survives role changes.
715+ if cluster_output . match? ( /\s +#{ primary_node_id } \s +\| .*primary.*\* \s +running/i )
714716 info '✓ Primary is registered with repmgr'
715717 else
716718 db_check = executor . run_sql_on_backend ( self ,
717- ' SELECT type FROM repmgr.nodes WHERE node_id = 1 AND active IS TRUE;' ,
719+ " SELECT type FROM repmgr.nodes WHERE node_id = #{ primary_node_id } AND active IS TRUE;" ,
718720 postgres_user : postgres_user ,
719721 database : repmgr_db ,
720722 tuples_only : true ,
@@ -951,7 +953,7 @@ def reload_postgres_cluster
951953
952954 def ensure_primary_registered
953955 host = config . primary_host
954- config . version
956+ primary_node_id = config . node_id_for ( host )
955957 is_registered = false
956958
957959 ssh_executor . execute_on_host ( host ) do
@@ -965,8 +967,8 @@ def ensure_primary_registered
965967 ''
966968 end
967969
968- if cluster_output . include? ( '| 1' ) && cluster_output . include? ( ' primary' )
969- info ' ✓ Primary is registered (node_id=1)'
970+ if cluster_output . match? ( / \b #{ primary_node_id } \s + \| .* primary/ )
971+ info " ✓ Primary is registered (node_id=#{ primary_node_id } )"
970972 else
971973 warn "⚠ Primary not found in cluster, assuming it's registered"
972974 end
0 commit comments