117 lines
2.9 KiB
Python
117 lines
2.9 KiB
Python
|
# -*- coding: utf-8 -*-
|
||
|
"""Module to control Univention Corporate Registry."""
|
||
|
|
||
|
ANSIBLE_METADATA = {"metadata_version": "1.1", "status": ["preview"], "supported_by": "community"}
|
||
|
|
||
|
DOCUMENTATION = """
|
||
|
---
|
||
|
module: ucr
|
||
|
short_description: Manage variables in univention configuration registry.
|
||
|
version_added: "2.6"
|
||
|
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
|
||
|
required: True
|
||
|
default: null
|
||
|
value:
|
||
|
description:
|
||
|
- New value of the variable
|
||
|
required: False
|
||
|
state:
|
||
|
required: False
|
||
|
default: "present"
|
||
|
choices: ["present", "absent"]
|
||
|
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
|
||
|
message:
|
||
|
description: The output message that the sample module generates
|
||
|
"""
|
||
|
|
||
|
from ansible.module_utils.basic import AnsibleModule
|
||
|
from univention.config_registry import ConfigRegistry # noqa
|
||
|
from univention.config_registry.frontend import ucr_update # noqa
|
||
|
|
||
|
|
||
|
def get_variable(ucr, path):
|
||
|
ucr.load()
|
||
|
if path in ucr:
|
||
|
value = ucr.get(path)
|
||
|
else:
|
||
|
value = None
|
||
|
return value
|
||
|
|
||
|
|
||
|
def set_variable(ucr, path, value, result):
|
||
|
org_value = get_variable(ucr, path)
|
||
|
ucr_update(ucr, {path: value})
|
||
|
new_value = get_variable(ucr, path)
|
||
|
return not org_value == new_value
|
||
|
|
||
|
|
||
|
def dry_variable(ucr, path, value, result):
|
||
|
org_value = get_variable(ucr, path)
|
||
|
return not org_value == value
|
||
|
|
||
|
|
||
|
def main():
|
||
|
ucr = ConfigRegistry()
|
||
|
|
||
|
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
|
||
|
)
|
||
|
|
||
|
result = dict(changed=False, original_message="", message="")
|
||
|
|
||
|
path = module.params["path"]
|
||
|
value = module.params["value"]
|
||
|
if module.params["state"] == "present":
|
||
|
if 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()
|