| 1 |
#!/usr/bin/python |
| 2 |
# |
| 3 |
# Copyright (C) 2013, Peter Sprygada <[email protected]> |
| 4 |
# GNU General Public License v3.0+ (see LICENSES/GPL-3.0-or-later.txt or https://www.gnu.org/licenses/gpl-3.0.txt) |
| 5 |
# SPDX-License-Identifier: GPL-3.0-or-later |
| 6 |
|
| 7 |
from __future__ import annotations |
| 8 |
|
| 9 |
DOCUMENTATION = r""" |
| 10 |
module: ejabberd_user |
| 11 |
author: "Peter Sprygada (@privateip)" |
| 12 |
short_description: Manages users for ejabberd servers |
| 13 |
requirements: |
| 14 |
- ejabberd with mod_admin_extra |
| 15 |
description: |
| 16 |
- This module provides user management for ejabberd servers. |
| 17 |
extends_documentation_fragment: |
| 18 |
- community.general._attributes |
| 19 |
attributes: |
| 20 |
check_mode: |
| 21 |
support: full |
| 22 |
diff_mode: |
| 23 |
support: none |
| 24 |
options: |
| 25 |
username: |
| 26 |
type: str |
| 27 |
description: |
| 28 |
- The name of the user to manage. |
| 29 |
required: true |
| 30 |
host: |
| 31 |
type: str |
| 32 |
description: |
| 33 |
- The ejabberd host associated with this username. |
| 34 |
required: true |
| 35 |
password: |
| 36 |
type: str |
| 37 |
description: |
| 38 |
- The password to assign to the username. |
| 39 |
state: |
| 40 |
type: str |
| 41 |
description: |
| 42 |
- Describe the desired state of the user to be managed. |
| 43 |
default: 'present' |
| 44 |
choices: ['present', 'absent'] |
| 45 |
notes: |
| 46 |
- Password parameter is required for O(state=present) only. |
| 47 |
- Passwords must be stored in clear text for this release. |
| 48 |
- The ejabberd configuration file must include mod_admin_extra as a module. |
| 49 |
""" |
| 50 |
EXAMPLES = r""" |
| 51 |
# Example playbook entries using the ejabberd_user module to manage users state. |
| 52 |
|
| 53 |
- name: Create a user if it does not exist |
| 54 |
community.general.ejabberd_user: |
| 55 |
username: test |
| 56 |
host: server |
| 57 |
password: password |
| 58 |
|
| 59 |
- name: Delete a user if it exists |
| 60 |
community.general.ejabberd_user: |
| 61 |
username: test |
| 62 |
host: server |
| 63 |
state: absent |
| 64 |
""" |
| 65 |
|
| 66 |
from ansible.module_utils.basic import AnsibleModule |
| 67 |
|
| 68 |
from ansible_collections.community.general.plugins.module_utils._cmd_runner import CmdRunner, cmd_runner_fmt |
| 69 |
|
| 70 |
|
| 71 |
class EjabberdUser: |
| 72 |
"""This object represents a user resource for an ejabberd server. The |
| 73 |
object manages user creation and deletion using ejabberdctl. The following |
| 74 |
commands are currently supported: |
| 75 |
* ejabberdctl register |
| 76 |
* ejabberdctl unregister |
| 77 |
""" |
| 78 |
|
| 79 |
def __init__(self, module): |
| 80 |
self.module = module |
| 81 |
self.state = module.params.get("state") |
| 82 |
self.host = module.params.get("host") |
| 83 |
self.user = module.params.get("username") |
| 84 |
self.pwd = module.params.get("password") |
| 85 |
self.runner = CmdRunner( |
| 86 |
module, |
| 87 |
command="ejabberdctl", |
| 88 |
arg_formats=dict( |
| 89 |
cmd=cmd_runner_fmt.as_list(), |
| 90 |
host=cmd_runner_fmt.as_list(), |
| 91 |
user=cmd_runner_fmt.as_list(), |
| 92 |
pwd=cmd_runner_fmt.as_list(), |
| 93 |
), |
| 94 |
check_rc=False, |
| 95 |
) |
| 96 |
|
| 97 |
@property |
| 98 |
def changed(self): |
| 99 |
"""This method will check the current user and see if the password has |
| 100 |
changed. It will return True if the user does not match the supplied |
| 101 |
credentials and False if it does not |
| 102 |
""" |
| 103 |
return self.run_command("check_password", "user host pwd", (lambda rc, out, err: bool(rc))) |
| 104 |
|
| 105 |
@property |
| 106 |
def exists(self): |
| 107 |
"""This method will check to see if the supplied username exists for |
| 108 |
host specified. If the user exists True is returned, otherwise False |
| 109 |
is returned |
| 110 |
""" |
| 111 |
return self.run_command("check_account", "user host", (lambda rc, out, err: not bool(rc))) |
| 112 |
|
| 113 |
def log(self, entry): |
| 114 |
"""This method does nothing""" |
| 115 |
pass |
| 116 |
|
| 117 |
def run_command(self, cmd, options, process=None): |
| 118 |
"""This method will run the any command specified and return the |
| 119 |
returns using the Ansible common module |
| 120 |
""" |
| 121 |
|
| 122 |
def _proc(*a): |
| 123 |
return a |
| 124 |
|
| 125 |
if process is None: |
| 126 |
process = _proc |
| 127 |
|
| 128 |
with self.runner(f"cmd {options}", output_process=process) as ctx: |
| 129 |
res = ctx.run(cmd=cmd, host=self.host, user=self.user, pwd=self.pwd) |
| 130 |
self.log(f"command: {' '.join(ctx.run_info['cmd'])}") |
| 131 |
return res |
| 132 |
|
| 133 |
def update(self): |
| 134 |
"""The update method will update the credentials for the user provided""" |
| 135 |
return self.run_command("change_password", "user host pwd") |
| 136 |
|
| 137 |
def create(self): |
| 138 |
"""The create method will create a new user on the host with the |
| 139 |
password provided |
| 140 |
""" |
| 141 |
return self.run_command("register", "user host pwd") |
| 142 |
|
| 143 |
def delete(self): |
| 144 |
"""The delete method will delete the user from the host""" |
| 145 |
return self.run_command("unregister", "user host") |
| 146 |
|
| 147 |
|
| 148 |
def main(): |
| 149 |
module = AnsibleModule( |
| 150 |
argument_spec=dict( |
| 151 |
host=dict(required=True, type="str"), |
| 152 |
username=dict(required=True, type="str"), |
| 153 |
password=dict(type="str", no_log=True), |
| 154 |
state=dict(default="present", choices=["present", "absent"]), |
| 155 |
), |
| 156 |
required_if=[ |
| 157 |
("state", "present", ["password"]), |
| 158 |
], |
| 159 |
supports_check_mode=True, |
| 160 |
) |
| 161 |
|
| 162 |
obj = EjabberdUser(module) |
| 163 |
|
| 164 |
rc = None |
| 165 |
result = dict(changed=False) |
| 166 |
|
| 167 |
if obj.state == "absent": |
| 168 |
if obj.exists: |
| 169 |
if module.check_mode: |
| 170 |
module.exit_json(changed=True) |
| 171 |
(rc, out, err) = obj.delete() |
| 172 |
if rc != 0: |
| 173 |
module.fail_json(msg=err, rc=rc) |
| 174 |
|
| 175 |
elif obj.state == "present": |
| 176 |
if not obj.exists: |
| 177 |
if module.check_mode: |
| 178 |
module.exit_json(changed=True) |
| 179 |
(rc, out, err) = obj.create() |
| 180 |
elif obj.changed: |
| 181 |
if module.check_mode: |
| 182 |
module.exit_json(changed=True) |
| 183 |
(rc, out, err) = obj.update() |
| 184 |
if rc is not None and rc != 0: |
| 185 |
module.fail_json(msg=err, rc=rc) |
| 186 |
|
| 187 |
if rc is None: |
| 188 |
result["changed"] = False |
| 189 |
else: |
| 190 |
result["changed"] = True |
| 191 |
|
| 192 |
module.exit_json(**result) |
| 193 |
|
| 194 |
|
| 195 |
if __name__ == "__main__": |
| 196 |
main() |