From da7f9ffc3ff9cedac649a365866c68fa8ec518e4 Mon Sep 17 00:00:00 2001 From: Andrew Klychkov Date: Wed, 14 Oct 2020 14:30:19 +0300 Subject: [PATCH] postgresql_info: add in_recovery return value to show if a service in recovery mode or not (#1091) * postgresql_info: add in_recovery return value to show if a service is in recovery mode or not * add changelog fragment * fix sanity --- ...1091-postgresql_info_add_in_recovery_ret_val.yml | 2 ++ .../modules/database/postgresql/postgresql_info.py | 13 ++++++++++++- .../tasks/postgresql_info_initial.yml | 6 ++++++ 3 files changed, 20 insertions(+), 1 deletion(-) create mode 100644 changelogs/fragments/1091-postgresql_info_add_in_recovery_ret_val.yml diff --git a/changelogs/fragments/1091-postgresql_info_add_in_recovery_ret_val.yml b/changelogs/fragments/1091-postgresql_info_add_in_recovery_ret_val.yml new file mode 100644 index 0000000000..39f6f66359 --- /dev/null +++ b/changelogs/fragments/1091-postgresql_info_add_in_recovery_ret_val.yml @@ -0,0 +1,2 @@ +minor_changes: +- postgresql_info - add ``in_recovery`` return value to show if a service in recovery mode or not (https://github.com/ansible-collections/community.general/issues/1068). diff --git a/plugins/modules/database/postgresql/postgresql_info.py b/plugins/modules/database/postgresql/postgresql_info.py index b93f4bff61..14047c22bf 100644 --- a/plugins/modules/database/postgresql/postgresql_info.py +++ b/plugins/modules/database/postgresql/postgresql_info.py @@ -18,7 +18,7 @@ options: description: - Limit the collected information by comma separated string or YAML list. - Allowable values are C(version), - C(databases), C(settings), C(tablespaces), C(roles), + C(databases), C(in_recovery), C(settings), C(tablespaces), C(roles), C(replications), C(repl_slots). - By default, collects all subsets. - You can use shell-style (fnmatch) wildcard to pass groups of values (see Examples). @@ -117,6 +117,11 @@ version: returned: always type: int sample: 1 +in_recovery: + description: Indicates if the service is in recovery mode or not. + returned: always + type: bool + sample: false databases: description: Information about databases. returned: always @@ -550,6 +555,7 @@ class PgClusterInfo(object): self.cursor = db_conn_obj.connect() self.pg_info = { "version": {}, + "in_recovery": None, "tablespaces": {}, "databases": {}, "replications": {}, @@ -563,6 +569,7 @@ class PgClusterInfo(object): """Collect information based on 'filter' option.""" subset_map = { "version": self.get_pg_version, + "in_recovery": self.get_recovery_state, "tablespaces": self.get_tablespaces, "databases": self.get_db_info, "replications": self.get_repl_info, @@ -922,6 +929,10 @@ class PgClusterInfo(object): minor=int(raw[1]), ) + def get_recovery_state(self): + """Get if the service is in recovery mode.""" + self.pg_info["in_recovery"] = self.__exec_sql("SELECT pg_is_in_recovery()")[0][0] + def get_db_info(self): """Get information about the current database.""" # Following query returns: diff --git a/tests/integration/targets/postgresql_info/tasks/postgresql_info_initial.yml b/tests/integration/targets/postgresql_info/tasks/postgresql_info_initial.yml index 38232eb3f1..0a117b7598 100644 --- a/tests/integration/targets/postgresql_info/tasks/postgresql_info_initial.yml +++ b/tests/integration/targets/postgresql_info/tasks/postgresql_info_initial.yml @@ -63,6 +63,7 @@ - assert: that: - result.version != {} + - result.in_recovery == false - result.databases.{{ db_default }}.collate - result.databases.{{ db_default }}.languages - result.databases.{{ db_default }}.namespaces @@ -81,11 +82,13 @@ filter: - ver* - rol* + - in_recov* - assert: that: - result.version != {} - result.roles + - result.in_recovery == false - result.databases == {} - result.repl_slots == {} - result.replications == {} @@ -126,10 +129,12 @@ filter: - "!ver*" - "!rol*" + - "!in_rec*" - assert: that: - result.version == {} + - result.in_recovery == None - result.roles == {} - result.databases @@ -144,6 +149,7 @@ - assert: that: - result.version != {} + - result.in_recovery == false - result.databases.{{ db_default }}.collate - result.databases.{{ db_default }}.languages - result.databases.{{ db_default }}.namespaces