Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add module check_mode #19

Merged
merged 1 commit into from
Dec 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 10 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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. |



Expand Down Expand Up @@ -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
Expand Down
58 changes: 30 additions & 28 deletions plugins/modules/check_mode.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,16 +15,35 @@
version_added: 2.5.0
author: "Bodo Schulz (@bodsch) <bodo@boone-schulz.de>"

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):
"""
"""
Expand All @@ -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():
Expand All @@ -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)

Expand Down