diff --git a/plugins/modules/logrotate.py b/plugins/modules/logrotate.py index c659ea0b53..f084e527b0 100644 --- a/plugins/modules/logrotate.py +++ b/plugins/modules/logrotate.py @@ -459,9 +459,10 @@ from ansible.module_utils.common.text.converters import to_native class LogrotateConfig: """Logrotate configuration manager.""" - def __init__(self, module: AnsibleModule) -> None: + def __init__(self, module: AnsibleModule, logrotate_bin: str) -> None: self.module = module self.params = module.params + self.logrotate_bin = logrotate_bin self.result: dict[str, object] = { "changed": False, "config_file": "", @@ -849,8 +850,8 @@ class LogrotateConfig: msg=f"Failed to write config file {self.result['config_file']}: {to_native(e)}" ) - if self.module.get_bin_path("logrotate"): - test_cmd = ["logrotate", "-d", str(self.result["config_file"])] + if not self.module.check_mode: + test_cmd = [self.logrotate_bin, "-d", str(self.result["config_file"])] rc, stdout, stderr = self.module.run_command(test_cmd) if rc != 0: self.module.warn(f"logrotate configuration test failed: {stderr}") @@ -918,9 +919,8 @@ def main() -> None: supports_check_mode=True, ) - if not module.get_bin_path("logrotate"): - module.fail_json(msg="logrotate is not installed or not in PATH") - logrotate_config = LogrotateConfig(module) + logrotate_bin = module.get_bin_path("logrotate", required=True) + logrotate_config = LogrotateConfig(module, logrotate_bin) result = logrotate_config.apply() module.exit_json(**result)