Further standardize the yum module

This commit is contained in:
Michael DeHaan 2012-07-25 20:14:15 -04:00
parent 1584eda3f8
commit 2030f82bf2

View file

@ -250,10 +250,9 @@ def ensure(my, state, pkgspec):
return res return res
return {'changed': False, # should be caught by AnsibleModule argument_spec
'failed': True, return dict(changed=False, failed=True, results='', errors='unexpected state')
'results':'',
'errors': 'Ensure state %r unknown' % state }
def update(args): def update(args):
@ -277,47 +276,43 @@ def main():
# list=pkgspec # list=pkgspec
module = AnsibleModule( 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 params = module.params
usage = "The module expects arguments of the following forms: state=<installed|removed|latest> pkg=<pkgspec> OR list=<installed|updates|available|repos|pkgspec>. 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: if 'conf_file' not in params:
params['conf_file'] = None 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: if 'list' in params:
try: try:
my = yum_base(conf_file=params['conf_file'], cachedir=True) my = yum_base(conf_file=params['conf_file'], cachedir=True)
results = dict(results=list_stuff(my, params['list'])) results = dict(results=list_stuff(my, params['list']))
except Exception, e: except Exception, e:
module.fail_json(msg=str(e)) module.fail_json(msg=str(e))
module.exit_json(**results)
else: else:
pkg = params.get('pkg', params.get('package', params.get('name', None))) pkg = params['pkg']
if 'pkg' is None: if 'pkg' is None:
module.fail_json(msg=usage) module.fail_json(msg="expected 'list=' or 'name='")
else: else:
try: try:
my = yum_base(conf_file=params['conf_file'], cachedir=True) my = yum_base(conf_file=params['conf_file'], cachedir=True)
state = params.get('state', 'installed') state = params['state']
results = ensure(my, state, pkg) results = ensure(my, state, pkg)
except Exception, e: except Exception, e:
module.fail_json(msg=str(e)) module.fail_json(msg=str(e))
module.exit_json(**results) module.exit_json(**results)
# this is magic, see lib/ansible/module_common.py # this is magic, see lib/ansible/module_common.py
#<<INCLUDE_ANSIBLE_MODULE_COMMON>> #<<INCLUDE_ANSIBLE_MODULE_COMMON>>