xoxys.general/plugins/modules/ucr.py
Robert Kaussow fa92fd43ee
Some checks failed
ci/woodpecker/push/lint Pipeline was successful
ci/woodpecker/push/unit-test Pipeline was successful
ci/woodpecker/push/build-package unknown status
ci/woodpecker/push/docs unknown status
ci/woodpecker/push/sanity-test Pipeline failed
ci/woodpecker/push/notify Pipeline was successful
ci: migrate to woodpecker ci (#7)
Reviewed-on: #7
Co-authored-by: Robert Kaussow <mail@thegeeklab.de>
Co-committed-by: Robert Kaussow <mail@thegeeklab.de>
2023-12-13 11:23:28 +01:00

136 lines
3.3 KiB
Python

#!/usr/bin/python
# -*- coding: utf-8 -*-
# GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt)
"""Control Univention Corporate Registry."""
from __future__ import absolute_import, division, print_function
__metaclass__ = type
ANSIBLE_METADATA = {"metadata_version": "1.1", "status": ["preview"], "supported_by": "community"}
DOCUMENTATION = """
---
module: ucr
short_description: Manage variables in univention configuration registry.
version_added: 1.1.0
description:
- "This module allows to manage variables inside the univention configuration registry
on a univention corporate server (UCS)."
options:
path:
description:
- Path for the variable
aliases:
- name
required: True
type: str
value:
description:
- New value of the variable
required: False
type: str
default: ""
state:
required: False
default: "present"
choices: ["present", "absent"]
type: str
description:
- Whether the variable should be exist or not.
author:
- Robert Kaussow (@xoxys)
"""
EXAMPLES = """
# Set variable to force https in ucs frontend
- name: Force https
ucr:
path: apache2/force_https
value: yes
# Allow another user as root to login as ssh
- name: Add ssh user
ucr:
path: auth/sshd/user/myuser
value: yes
"""
RETURN = """
original_message:
description: The original name param that was passed in
type: str
returned: success
message:
description: The output message that the sample module generates
type: str
returned: success
"""
from ansible.module_utils.basic import AnsibleModule
try:
from univention.config_registry import ConfigRegistry
from univention.config_registry.frontend import ucr_update
HAS_UNIVENTION = True
except ImportError:
HAS_UNIVENTION = False
def get_variable(ucr, path):
ucr.load()
return ucr.get(path) if path in ucr else None
def set_variable(ucr, path, value, result): # noqa
org_value = get_variable(ucr, path)
ucr_update(ucr, {path: value})
new_value = get_variable(ucr, path)
return org_value != new_value
def dry_variable(ucr, path, value, result): # noqa
org_value = get_variable(ucr, path)
return org_value != value
def main():
module_args = dict(
path=dict(type="str", required=True, aliases=["name"]),
value=dict(type="str", required=False, default=""),
state=dict(default="present", choices=["present", "absent"], type="str"),
)
required_if = [["state", "present", ["value"]]]
module = AnsibleModule(
argument_spec=module_args, supports_check_mode=True, required_if=required_if
)
if not HAS_UNIVENTION:
module.fail_json(msg="univention required for this module")
ucr = ConfigRegistry()
result = dict(changed=False, original_message="", message="")
path = module.params["path"]
value = module.params["value"]
if module.params["state"] == "present" and (value is None or value == "None"):
value = ""
elif module.params["state"] == "absent":
value = None
if not module.check_mode:
result["changed"] = set_variable(ucr, path, value, result)
else:
result["changed"] = dry_variable(ucr, path, value, result)
module.exit_json(**result)
if __name__ == "__main__":
main()