mirror of
https://github.com/ansible-collections/community.general.git
synced 2024-09-14 20:13:21 +02:00
ejabberd_user: use CmdRunner (#7075)
* ejabberd_user: use CmdRunner * add changelog frag * regain sanity
This commit is contained in:
parent
eafdf87b1b
commit
f9f5c45c94
2 changed files with 30 additions and 9 deletions
2
changelogs/fragments/7075-ejabberd-user-cmdrunner.yml
Normal file
2
changelogs/fragments/7075-ejabberd-user-cmdrunner.yml
Normal file
|
@ -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).
|
|
@ -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():
|
||||
|
|
Loading…
Reference in a new issue