Various small fixes to boolean usage, using module.params.get and default values

This commit is contained in:
Dag Wieers 2012-10-07 22:06:20 +02:00
parent 250f52bfba
commit 5503cfaa44
4 changed files with 47 additions and 35 deletions

View file

@ -68,13 +68,17 @@ def main():
) )
) )
if module.params['fail'] in BOOLEANS_TRUE and module.params['rc'] == 0: fail = module.boolean(module.params.get('fail'))
module.params['rc'] = 1 msg = module.params.get('msg')
rc = module.params.get('rc')
if module.params['fail'] in BOOLEANS_TRUE: if fail and rc == 0:
module.fail_json(rc=module.params['rc'], msg=module.params['msg']) rc = 1
if fail:
module.fail_json(rc=rc, msg=msg)
else: else:
module.exit_json(msg=module.params['msg']) module.exit_json(msg=msg)
# 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>>

View file

@ -61,7 +61,7 @@ options:
C(protocol://username:password@hostname:port/filename) C(protocol://username:password@hostname:port/filename)
- protocol is either C(http) or C(https) - protocol is either C(http) or C(https)
- username:password is optional - username:password is optional
- port is optional - port is optional
required: false required: false
state: state:
description: description:
@ -111,52 +111,59 @@ def main():
login = dict(default='Administrator'), login = dict(default='Administrator'),
password = dict(default='admin'), password = dict(default='admin'),
match = dict(default=None), match = dict(default=None),
media = dict(choices=['cdrom', 'floppy', 'hdd', 'network', 'normal', 'usb']), media = dict(default=None, choices=['cdrom', 'floppy', 'hdd', 'network', 'normal', 'usb']),
image = dict(default=None), image = dict(default=None),
state = dict(required=True, default='boot_once', choices=['boot_always', 'boot_once', 'connect', 'disconnect', 'no_boot']), state = dict(default='boot_once', choices=['boot_always', 'boot_once', 'connect', 'disconnect', 'no_boot']),
force = dict(default=True, choices=BOOLEANS), force = dict(default='no', choices=BOOLEANS),
) )
) )
host = module.params['host'] host = module.params.get('host')
login = module.params['login'] login = module.params.get('login')
password = module.params['password'] password = module.params.get('password')
force = module.params['force'] match = module.params.get('match')
media = module.params.get('media')
image = module.params.get('image')
state = module.params.get('state')
force = module.boolean(module.params.get('force'))
ilo = hpilo.Ilo(host, login=login, password=password) ilo = hpilo.Ilo(host, login=login, password=password)
# If match=string is provided, only reboot server if iLO name matches 'string' # If match=string is provided, only reboot server if iLO name matches 'string'
if module.params['match'] != None: if match != None:
try: try:
server_name = ilo.get_server_name() server_name = ilo.get_server_name()
except Exception, e: except Exception, e:
module.fail_json(rc=1, msg='Failed to connect to %s: %s' % (host, e.message)) module.fail_json(rc=1, msg='Failed to connect to %s: %s' % (host, e.message))
if not server_name.lower().startswith(module.params['match'].lower()): if not server_name.lower().startswith(match.lower()):
module.fail_json(rc=1, msg='The iLO server name \'%s\' does not match \'%s\'' % (server_name, module.params['match'])) module.fail_json(rc=1, msg='The iLO server name \'%s\' does not match \'%s\'' % (server_name, match))
if module.params['media']: if media:
### FIXME: In the below case iLO fails for a short period of time due to the server rebooting ### FIXME: In the below case iLO fails for a short period of time due to the server rebooting
# File "/usr/lib/python2.6/site-packages/hpilo.py", line 381, in _parse_message # File "/usr/lib/python2.6/site-packages/hpilo.py", line 381, in _parse_message
# raise IloError("Error communicating with iLO: %s" % child.get('MESSAGE')) # raise IloError("Error communicating with iLO: %s" % child.get('MESSAGE'))
#hpilo.IloError: Error communicating with iLO: Problem manipulating EV #hpilo.IloError: Error communicating with iLO: Problem manipulating EV
ilo.set_one_time_boot(module.params['media']) ilo.set_one_time_boot(media)
# TODO: Verify if image URL exists/works # TODO: Verify if image URL exists/works
if module.params['image']: if image:
ilo.insert_virtual_media(module.params['media'], module.params['image']) ilo.insert_virtual_media(media, image)
if module.params['media'] == 'cdrom': if media == 'cdrom':
ilo.set_vm_status('cdrom', module.params['state'], True) ilo.set_vm_status('cdrom', state, True)
status = ilo.get_vm_status() status = ilo.get_vm_status()
elif module.params['media'] == 'floppy': elif media == 'floppy':
ilo.set_vf_status(module.params['state'], True) ilo.set_vf_status(state, True)
status = ilo.get_vf_status()
elif media == 'usb':
ilo.set_vf_status(state, True)
status = ilo.get_vf_status() status = ilo.get_vf_status()
# Only perform a boot when state is boot_once or boot_always, or in case we want to force a reboot # Only perform a boot when state is boot_once or boot_always, or in case we want to force a reboot
if module.params['state'] in ('boot_once', 'boot_always') or force: if state in ('boot_once', 'boot_always') or force:
power_status = ilo.get_host_power_status() power_status = ilo.get_host_power_status()

View file

@ -106,21 +106,22 @@ def main():
) )
) )
host = module.params['host'] host = module.params('host')
login = module.params['login'] login = module.params('login')
password = module.params['password'] password = module.params('password')
match = module.params.get('match')
ilo = hpilo.Ilo(host, login=login, password=password) ilo = hpilo.Ilo(host, login=login, password=password)
# If match=string is provided, only reboot server if iLO name matches 'string' # If match=string is provided, only reboot server if iLO name matches 'string'
if module.params['match'] != None: if match != None:
try: try:
server_name = ilo.get_server_name() server_name = ilo.get_server_name()
except Exception, e: except Exception, e:
module.fail_json(rc=1, msg='Failed to connect to %s: %s' % (host, e.message)) module.fail_json(rc=1, msg='Failed to connect to %s: %s' % (host, e.message))
if not server_name.lower().startswith(module.params['match'].lower()): if not server_name.lower().startswith(match.lower()):
module.fail_json(rc=1, msg='The iLO server name \'%s\' does not match \'%s\'' % (server_name, module.params['match'])) module.fail_json(rc=1, msg='The iLO server name \'%s\' does not match \'%s\'' % (server_name, match))
# TODO: Count number of CPUs, DIMMs and total memory # TODO: Count number of CPUs, DIMMs and total memory
data = ilo.get_host_data() data = ilo.get_host_data()

View file

@ -88,10 +88,10 @@ def main():
) )
) )
host = module.params['host'] host = module.params.get('host')
login = module.params['login'] login = module.params.get('login')
password = module.params['password'] password = module.params.get('password')
guest = module.params['guest'] guest = module.params.get('guest')
server = pysphere.VIServer() server = pysphere.VIServer()
try: try: