From c3baaa8cfa14084482e8416bb64e5fd279179875 Mon Sep 17 00:00:00 2001 From: "patchback[bot]" <45432694+patchback[bot]@users.noreply.github.com> Date: Sun, 13 Aug 2023 22:15:35 +0200 Subject: [PATCH] [PR #7075/f9f5c45c backport][stable-7] ejabberd_user: use CmdRunner (#7110) ejabberd_user: use CmdRunner (#7075) * ejabberd_user: use CmdRunner * add changelog frag * regain sanity (cherry picked from commit f9f5c45c94250a2760fd611fd01fc1da98fe7251) Co-authored-by: Alexei Znamensky <103110+russoz@users.noreply.github.com> --- .../7075-ejabberd-user-cmdrunner.yml | 2 + plugins/modules/ejabberd_user.py | 37 ++++++++++++++----- 2 files changed, 30 insertions(+), 9 deletions(-) create mode 100644 changelogs/fragments/7075-ejabberd-user-cmdrunner.yml diff --git a/changelogs/fragments/7075-ejabberd-user-cmdrunner.yml b/changelogs/fragments/7075-ejabberd-user-cmdrunner.yml new file mode 100644 index 0000000000..abe4ef2fb7 --- /dev/null +++ b/changelogs/fragments/7075-ejabberd-user-cmdrunner.yml @@ -0,0 +1,2 @@ +minor_changes: + - ejabberd_user - module now using ``CmdRunner`` to execute external command (https://github.com/ansible-collections/community.general/pull/7075). diff --git a/plugins/modules/ejabberd_user.py b/plugins/modules/ejabberd_user.py index 7808fc9907..73830047fa 100644 --- a/plugins/modules/ejabberd_user.py +++ b/plugins/modules/ejabberd_user.py @@ -78,6 +78,7 @@ EXAMPLES = ''' import syslog from ansible.module_utils.basic import AnsibleModule +from ansible_collections.community.general.plugins.module_utils.cmd_runner import CmdRunner, cmd_runner_fmt class EjabberdUser(object): @@ -95,6 +96,17 @@ class EjabberdUser(object): self.host = module.params.get('host') self.user = module.params.get('username') self.pwd = module.params.get('password') + self.runner = CmdRunner( + module, + command="ejabberdctl", + arg_formats=dict( + cmd=cmd_runner_fmt.as_list(), + host=cmd_runner_fmt.as_list(), + user=cmd_runner_fmt.as_list(), + pwd=cmd_runner_fmt.as_list(), + ), + check_rc=False, + ) @property def changed(self): @@ -102,7 +114,7 @@ class EjabberdUser(object): changed. It will return True if the user does not match the supplied credentials and False if it does not """ - return bool(self.run_command('check_password', [self.user, self.host, self.pwd])[0]) + return self.run_command('check_password', 'user host pwd', (lambda rc, out, err: bool(rc))) @property def exists(self): @@ -110,7 +122,7 @@ class EjabberdUser(object): host specified. If the user exists True is returned, otherwise False is returned """ - return not bool(self.run_command('check_account', [self.user, self.host])[0]) + return self.run_command('check_account', 'user host', (lambda rc, out, err: not bool(rc))) def log(self, entry): """ This method will log information to the local syslog facility """ @@ -118,29 +130,36 @@ class EjabberdUser(object): syslog.openlog('ansible-%s' % self.module._name) syslog.syslog(syslog.LOG_NOTICE, entry) - def run_command(self, cmd, options): + def run_command(self, cmd, options, process=None): """ This method will run the any command specified and return the returns using the Ansible common module """ - cmd = [self.module.get_bin_path('ejabberdctl', required=True), cmd] + options - self.log('command: %s' % " ".join(cmd)) - return self.module.run_command(cmd) + def _proc(*a): + return a + + if process is None: + process = _proc + + with self.runner("cmd " + options, output_process=process) as ctx: + res = ctx.run(cmd=cmd, host=self.host, user=self.user, pwd=self.pwd) + self.log('command: %s' % " ".join(ctx.run_info['cmd'])) + return res def update(self): """ The update method will update the credentials for the user provided """ - return self.run_command('change_password', [self.user, self.host, self.pwd]) + return self.run_command('change_password', 'user host pwd') def create(self): """ The create method will create a new user on the host with the password provided """ - return self.run_command('register', [self.user, self.host, self.pwd]) + return self.run_command('register', 'user host pwd') def delete(self): """ The delete method will delete the user from the host """ - return self.run_command('unregister', [self.user, self.host]) + return self.run_command('unregister', 'user host') def main():