Skip to content

Commit 2bf86f3

Browse files
committed
Wrap some add_replica actions in an if statement
Actions taken on $replica_postgresql_host were previously a noop when the parameter was not provided because the tasks would be ran with an undef set of targets. Doing this when introducing get_psql_version results in an error because you can't run functions against an undef return value if the task was a noop. Wrap everything related to $replica_postgresql_host in an if statement to prevent errors when the parameter is not provided.
1 parent c080cf4 commit 2bf86f3

File tree

1 file changed

+26
-21
lines changed

1 file changed

+26
-21
lines changed

plans/add_replica.pp

Lines changed: 26 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -60,29 +60,34 @@
6060
dns_alt_names => $dns_alt_names
6161
)
6262

63-
# On the PE-PostgreSQL server in the <replacement-avail-group-letter> group
64-
$psql_version = run_task('peadm::get_psql_version', $replica_postgresql_target).first.value['version']
65-
66-
# Stop puppet and add the following two lines to
67-
# /opt/puppetlabs/server/data/postgresql/11/data/pg_ident.conf
68-
# pe-puppetdb-pe-puppetdb-map <replacement-replica-fqdn> pe-puppetdb
69-
# pe-puppetdb-pe-puppetdb-migrator-map <replacement-replica-fqdn> pe-puppetdb-migrator
70-
apply($replica_postgresql_target) {
71-
file_line { 'pe-puppetdb-pe-puppetdb-map':
72-
path => "/opt/puppetlabs/server/data/postgresql/${psql_version}/data/pg_ident.conf",
73-
line => "pe-puppetdb-pe-puppetdb-map ${replica_target.peadm::certname()} pe-puppetdb",
63+
# Wrap these things that operate on replica_postgresql_target in an if statement
64+
# to avoid failures retrieving PSQL version because you can't operate functions
65+
# on a return value of nil.
66+
if $replica_postgresql_host {
67+
# On the PE-PostgreSQL server in the <replacement-avail-group-letter> group
68+
$psql_version = run_task('peadm::get_psql_version', $replica_postgresql_target).first.value['version']
69+
70+
# Stop puppet and add the following two lines to
71+
# /opt/puppetlabs/server/data/postgresql/11/data/pg_ident.conf
72+
# pe-puppetdb-pe-puppetdb-map <replacement-replica-fqdn> pe-puppetdb
73+
# pe-puppetdb-pe-puppetdb-migrator-map <replacement-replica-fqdn> pe-puppetdb-migrator
74+
apply($replica_postgresql_target) {
75+
file_line { 'pe-puppetdb-pe-puppetdb-map':
76+
path => "/opt/puppetlabs/server/data/postgresql/${psql_version}/data/pg_ident.conf",
77+
line => "pe-puppetdb-pe-puppetdb-map ${replica_target.peadm::certname()} pe-puppetdb",
78+
}
79+
file_line { 'pe-puppetdb-pe-puppetdb-migrator-map':
80+
path => "/opt/puppetlabs/server/data/postgresql/${psql_version}/data/pg_ident.conf",
81+
line => "pe-puppetdb-pe-puppetdb-migrator-map ${replica_target.peadm::certname()} pe-puppetdb-migrator",
82+
}
83+
file_line { 'pe-puppetdb-pe-puppetdb-read-map':
84+
path => "/opt/puppetlabs/server/data/postgresql/${psql_version}/data/pg_ident.conf",
85+
line => "pe-puppetdb-pe-puppetdb-read-map ${replica_target.peadm::certname()} pe-puppetdb-read",
86+
}
7487
}
75-
file_line { 'pe-puppetdb-pe-puppetdb-migrator-map':
76-
path => "/opt/puppetlabs/server/data/postgresql/${psql_version}/data/pg_ident.conf",
77-
line => "pe-puppetdb-pe-puppetdb-migrator-map ${replica_target.peadm::certname()} pe-puppetdb-migrator",
78-
}
79-
file_line { 'pe-puppetdb-pe-puppetdb-read-map':
80-
path => "/opt/puppetlabs/server/data/postgresql/${psql_version}/data/pg_ident.conf",
81-
line => "pe-puppetdb-pe-puppetdb-read-map ${replica_target.peadm::certname()} pe-puppetdb-read",
82-
}
83-
}
8488

85-
run_command('systemctl reload pe-postgresql.service', $replica_postgresql_target)
89+
run_command('systemctl reload pe-postgresql.service', $replica_postgresql_target)
90+
}
8691

8792
run_plan('peadm::util::update_classification', $primary_target,
8893
server_a_host => $replica_avail_group_letter ? { 'A' => $replica_host, default => undef },

0 commit comments

Comments
 (0)