xoxys.general/plugins/modules/ucr.py

135 lines
3.3 KiB
Python
Raw Normal View History

2023-01-31 19:09:29 +00:00
#!/usr/bin/python
2020-08-18 21:44:49 +00:00
# -*- coding: utf-8 -*-
2023-01-31 19:09:29 +00:00
# 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
2020-08-18 21:44:49 +00:00
ANSIBLE_METADATA = {"metadata_version": "1.1", "status": ["preview"], "supported_by": "community"}
DOCUMENTATION = """
---
module: ucr
short_description: Manage variables in univention configuration registry.
2023-01-31 19:09:29 +00:00
version_added: 1.1.0
2020-08-18 21:44:49 +00:00
description:
2023-01-31 19:09:29 +00:00
- "This module allows to manage variables inside the univention configuration registry
on a univention corporate server (UCS)."
2020-08-18 21:44:49 +00:00
options:
2023-01-31 19:09:29 +00:00
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.
2020-08-18 21:44:49 +00:00
author:
2023-01-31 19:09:29 +00:00
- Robert Kaussow (@xoxys)
2020-08-18 21:44:49 +00:00
"""
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:
2023-01-31 19:09:29 +00:00
description: The original name param that was passed in
type: str
returned: success
2020-08-18 21:44:49 +00:00
message:
2023-01-31 19:09:29 +00:00
description: The output message that the sample module generates
type: str
returned: success
2020-08-18 21:44:49 +00:00
"""
from ansible.module_utils.basic import AnsibleModule
2023-01-31 19:09:29 +00:00
try:
from univention.config_registry import ConfigRegistry
from univention.config_registry.frontend import ucr_update
HAS_UNIVENTION = True
except ImportError:
HAS_UNIVENTION = False
2020-08-18 21:44:49 +00:00
def get_variable(ucr, path):
ucr.load()
2023-01-31 19:09:29 +00:00
return ucr.get(path) if path in ucr else None
2020-08-18 21:44:49 +00:00
2023-01-31 19:09:29 +00:00
def set_variable(ucr, path, value, result): # noqa
2020-08-18 21:44:49 +00:00
org_value = get_variable(ucr, path)
ucr_update(ucr, {path: value})
new_value = get_variable(ucr, path)
2023-01-31 19:09:29 +00:00
return org_value != new_value
2020-08-18 21:44:49 +00:00
2023-01-31 19:09:29 +00:00
def dry_variable(ucr, path, value, result): # noqa
2020-08-18 21:44:49 +00:00
org_value = get_variable(ucr, path)
2023-01-31 19:09:29 +00:00
return org_value != value
2020-08-18 21:44:49 +00:00
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
)
2023-01-31 19:09:29 +00:00
if not HAS_UNIVENTION:
module.fail_json(msg="univention required for this module")
ucr = ConfigRegistry()
2020-08-18 21:44:49 +00:00
result = dict(changed=False, original_message="", message="")
path = module.params["path"]
value = module.params["value"]
2023-01-31 19:09:29 +00:00
if module.params["state"] == "present" and (value is None or value == "None"):
value = ""
2020-08-18 21:44:49 +00:00
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()