From 7de8c2d65d53646530dc45bd51bf8b99d698207a Mon Sep 17 00:00:00 2001 From: Bodo Schulz Date: Mon, 9 Dec 2024 08:23:19 +0100 Subject: [PATCH] add module check_mode --- README.md | 16 ++++++---- plugins/modules/check_mode.py | 58 ++++++++++++++++++----------------- 2 files changed, 40 insertions(+), 34 deletions(-) diff --git a/README.md b/README.md index a79718e..01b54b7 100644 --- a/README.md +++ b/README.md @@ -46,12 +46,14 @@ pip install dirsync | Name | Description | |:--------------------------|:----| -| [remove_ansible_backups](./plugins/modules/remove_ansible_backups.py) | Remove older backup files created by ansible | -| [package_version](./plugins/modules/package_version.py) | Attempts to determine the version of a package to be installed or already installed. | -| [aur](./plugins/modules/aur.py) | Installing packages for ArchLinux with aur | -| [journalctl](./plugins/modules/journalctl.py) | Query the systemd journal with a very limited number of possible parameters | -| [facts](./plugins/modules/facts.py) | Write ansible facts | -| [sync_directory](./plugins/modules/sync_directory.py) | Syncronises directories similar to rsync | +| [bodsch.core.remove_ansible_backups](./plugins/modules/remove_ansible_backups.py) | Remove older backup files created by ansible | +| [bodsch.core.package_version](./plugins/modules/package_version.py) | Attempts to determine the version of a package to be installed or already installed. | +| [bodsch.core.aur](./plugins/modules/aur.py) | Installing packages for ArchLinux with aur | +| [bodsch.core.journalctl](./plugins/modules/journalctl.py) | Query the systemd journal with a very limited number of possible parameters | +| [bodsch.core.facts](./plugins/modules/facts.py) | Write ansible facts | +| [bodsch.core.sync_directory](./plugins/modules/sync_directory.py) | Syncronises directories similar to rsync | +| [bodsch.core.check_mode](./plugins/modules/check_mode.py) | Replacement for `ansible_check_mode`. | +| [bodsch.core.facts](./plugins/modules/facts.py) | Creates a facts file for ansible. | @@ -91,6 +93,8 @@ pip install -r requirements.txt You can either call modules by their Fully Qualified Collection Name (FQCN), such as `bodsch.core.remove_ansible_backups`, or you can call modules by their short name if you list the `bodsch.core` collection in the playbook's `collections` keyword: + + ```yaml --- - name: remove older ansible backup files diff --git a/plugins/modules/check_mode.py b/plugins/modules/check_mode.py index b18e201..e6555a5 100644 --- a/plugins/modules/check_mode.py +++ b/plugins/modules/check_mode.py @@ -15,16 +15,35 @@ version_added: 2.5.0 author: "Bodo Schulz (@bodsch) " -short_description: another solution for ansible_check_mode +short_description: Replacement for ansible_check_mode. description: - - returns a valid state for check_mode + - Replacement for ansible_check_mode. + - The magic variable `ansible_check_mode` was not defined with the correct value in some cases. options: """ +EXAMPLES = r""" +- name: detect ansible check_mode + bodsch.core.check_mode: + register: _check_mode + +- name: define check_mode + ansible.builtin.set_fact: + check_mode: '{{ _check_mode.check_mode }}' +""" + +RETURN = r""" +check_mode: + description: + - Status for check_mode. + type: bool +""" + # --------------------------------------------------------------------------------------- + class CheckMode(object): """ """ @@ -35,40 +54,23 @@ def __init__(self, module): """ self.module = module - self._openvpn = module.get_bin_path('/bin/true', True) - def run(self): """ """ + result = dict( + failed=False, + changed=False, + check_mode=False + ) + if self.module.check_mode: - return dict( + result = dict( failed=False, changed=False, check_mode=True ) - else: - return dict( - failed=False, - changed=False, - check_mode=False - ) - - def _exec(self, commands): - """ - """ - rc, out, err = self.module.run_command(commands, check_rc=False) - - if int(rc) != 0: - self.module.log(msg=f" rc : '{rc}'") - self.module.log(msg=f" out: '{out}'") - self.module.log(msg=f" err: '{err}'") - - return rc, out - -# =========================================== -# Module execution. -# + return result def main(): @@ -83,7 +85,7 @@ def main(): o = CheckMode(module) result = o.run() - module.log(msg="= result: {}".format(result)) + module.log(msg=f"= result: {result}") module.exit_json(**result)