From 1fe021f7bb994573cd07b163c36839a0d6f12b4a Mon Sep 17 00:00:00 2001 From: Michael DeHaan Date: Wed, 25 Jul 2012 20:14:15 -0400 Subject: [PATCH] Further standardize the yum module --- yum | 41 ++++++++++++++++++----------------------- 1 file changed, 18 insertions(+), 23 deletions(-) diff --git a/yum b/yum index 4cb319134d8..86f5838b939 100755 --- a/yum +++ b/yum @@ -249,11 +249,10 @@ def ensure(my, state, pkgspec): res['results'] = out return res - - return {'changed': False, - 'failed': True, - 'results':'', - 'errors': 'Ensure state %r unknown' % state } + + # should be caught by AnsibleModule argument_spec + return dict(changed=False, failed=True, results='', errors='unexpected state') + def update(args): @@ -277,24 +276,21 @@ def main(): # list=pkgspec module = AnsibleModule( - argument_spec = dict() + argument_spec = dict( + pkg=dict(aliases=['name']), + # removed==absent, installed==present, these are accepted as aliases + state=dict(default='installed', choices=['absent','present','installed','removed','latest']), + list=dict(choices=['installed','updates','available','repos','pkgspec']), + ) ) params = module.params - usage = "The module expects arguments of the following forms: state= pkg= OR list=. pkgspec is nothing but the package specification. Example: google-chrome-stable.i386" - - if not len(params): - module.fail_json(msg=usage) - - # if nothing else changes - it fails - results = { 'changed':False, - 'failed':True, - 'results':'', - 'errors':'', - 'msg':usage } if 'conf_file' not in params: params['conf_file'] = None + + if 'list' in params and 'pkg' in params: + module.fail_json(msg="expected 'list=' or 'name=', but not both") if 'list' in params: try: @@ -302,21 +298,20 @@ def main(): results = dict(results=list_stuff(my, params['list'])) except Exception, e: module.fail_json(msg=str(e)) + module.exit_json(**results) else: - pkg = params.get('pkg', params.get('package', params.get('name', None))) + pkg = params['pkg'] if 'pkg' is None: - module.fail_json(msg=usage) + module.fail_json(msg="expected 'list=' or 'name='") else: try: my = yum_base(conf_file=params['conf_file'], cachedir=True) - state = params.get('state', 'installed') + state = params['state'] results = ensure(my, state, pkg) except Exception, e: module.fail_json(msg=str(e)) - - module.exit_json(**results) - + module.exit_json(**results) # this is magic, see lib/ansible/module_common.py #<>