diff --git a/changelogs/fragments/11255-monit-integrationtests.yml b/changelogs/fragments/11255-monit-integrationtests.yml new file mode 100644 index 0000000000..57e0adf468 --- /dev/null +++ b/changelogs/fragments/11255-monit-integrationtests.yml @@ -0,0 +1,4 @@ +minor_changes: + - monit - add ``monit_version`` return value also when the module has succeeded (https://github.com/ansible-collections/community.general/pull/11255). +bugfixes: + - monit - add delay of 0.5 seconds after state change and check for status (https://github.com/ansible-collections/community.general/pull/11255). diff --git a/plugins/modules/monit.py b/plugins/modules/monit.py index 1ba74995e5..54710114e3 100644 --- a/plugins/modules/monit.py +++ b/plugins/modules/monit.py @@ -152,7 +152,12 @@ class Monit: self.module.fail_json(**kwargs) def exit_success(self, state): - self.module.exit_json(changed=True, name=self.process_name, state=state) + self.module.exit_json( + changed=True, + name=self.process_name, + monit_version=self._raw_version, + state=state, + ) @property def command_args(self): @@ -272,6 +277,9 @@ class Monit: def change_state(self, state: str, expected_status: StatusValue, invert_expected: bool | None = None): current_status = self.get_status() self.run_command(STATE_COMMAND_MAP[state]) + # Give monit daemon a moment to process the command before checking status + # to avoid race condition where HTTP interface may be temporarily unresponsive + time.sleep(0.5) status = self.wait_for_status_change(current_status) status = self.wait_for_monit_to_stop_pending(status) status_match = status.state == expected_status diff --git a/tests/integration/targets/monit/tasks/test_reload_present.yml b/tests/integration/targets/monit/tasks/test_reload_present.yml index 0bd6cd073b..fef8d84c16 100644 --- a/tests/integration/targets/monit/tasks/test_reload_present.yml +++ b/tests/integration/targets/monit/tasks/test_reload_present.yml @@ -52,6 +52,14 @@ name: monit state: restarted +- name: wait for monit daemon to be responsive after restart + command: monit status -B httpd_echo + register: monit_status_check + changed_when: false + until: monit_status_check.rc == 0 + retries: 10 + delay: 0.5 + - name: test process present again monit: name: httpd_echo