From df798d0688cdf09fabce12426f0103351c96b113 Mon Sep 17 00:00:00 2001 From: Daniel Hokka Zakrisson Date: Sat, 23 Feb 2013 19:59:52 +0100 Subject: [PATCH] Use argument type checking to convert to boolean --- library/apt | 16 ++++++++-------- library/assemble | 4 ++-- library/copy | 8 ++++---- library/cron | 8 ++++---- library/easy_install | 4 ++-- library/file | 4 ++-- library/get_url | 4 ++-- library/git | 4 ++-- library/hg | 8 ++++---- library/ini_file | 4 ++-- library/lineinfile | 10 +++++----- library/pacman | 4 ++-- library/pip | 8 ++++---- library/rabbitmq_plugin | 4 ++-- library/rabbitmq_user | 4 ++-- library/rabbitmq_vhost | 4 ++-- library/seboolean | 8 ++++---- library/service | 4 ++-- library/subversion | 4 ++-- library/sysctl | 4 ++-- library/uri | 12 ++++++------ library/user | 24 ++++++++++++------------ 22 files changed, 77 insertions(+), 77 deletions(-) diff --git a/library/apt b/library/apt index 9f9d45dedf6..8aa8347433d 100644 --- a/library/apt +++ b/library/apt @@ -181,12 +181,12 @@ def main(): module = AnsibleModule( argument_spec = dict( state = dict(default='installed', choices=['installed', 'latest', 'removed', 'absent', 'present']), - update_cache = dict(default='no', choices=['yes', 'no'], aliases=['update-cache']), - purge = dict(default='no', choices=['yes', 'no']), + update_cache = dict(default='no', choices=BOOLEANS, aliases=['update-cache'], type='bool'), + purge = dict(default='no', choices=BOOLEANS, type='bool'), package = dict(default=None, aliases=['pkg', 'name']), default_release = dict(default=None, aliases=['default-release']), - install_recommends = dict(default='yes', aliases=['install-recommends'], choices=['yes', 'no']), - force = dict(default='no', choices=['yes', 'no']) + install_recommends = dict(default='yes', aliases=['install-recommends'], choices=BOOLEANS, type='bool'), + force = dict(default='no', choices=BOOLEANS, type='bool') ), supports_check_mode = True ) @@ -204,7 +204,7 @@ def main(): if p['package'] is None and p['update_cache'] != 'yes': module.fail_json(msg='pkg=name and/or update_cache=yes is required') - install_recommends = module.boolean(p['install_recommends']) + install_recommends = p['install_recommends'] cache = apt.Cache() if p['default_release']: @@ -212,13 +212,13 @@ def main(): # reopen cache w/ modified config cache.open(progress=None) - if module.boolean(p['update_cache']): + if p['update_cache']: cache.update() cache.open(progress=None) if not p['package']: module.exit_json(changed=False) - force_yes = module.boolean(p['force']) + force_yes = p['force'] packages = p['package'].split(',') latest = p['state'] == 'latest' @@ -237,7 +237,7 @@ def main(): install(module, packages, cache, default_release=p['default_release'], install_recommends=install_recommends,force=force_yes) elif p['state'] in [ 'removed', 'absent' ]: - remove(module, packages, cache, purge = module.boolean(p['purge'])) + remove(module, packages, cache, p['purge']) # this is magic, see lib/ansible/module_common.py #<> diff --git a/library/assemble b/library/assemble index 0e0eadc05a2..e4c98be4a61 100644 --- a/library/assemble +++ b/library/assemble @@ -94,7 +94,7 @@ def main(): argument_spec = dict( src = dict(required=True), dest = dict(required=True), - backup=dict(default=False, choices=BOOLEANS), + backup=dict(default=False, choices=BOOLEANS, type='bool'), ), add_file_common_args=True ) @@ -104,7 +104,7 @@ def main(): destmd5 = None src = os.path.expanduser(module.params['src']) dest = os.path.expanduser(module.params['dest']) - backup = module.boolean(module.params.get('backup', False)) + backup = module.params['backup'] if not os.path.exists(src): module.fail_json(msg="Source (%s) does not exist" % src) diff --git a/library/copy b/library/copy index 1fc450e3842..584e2eb9117 100644 --- a/library/copy +++ b/library/copy @@ -78,16 +78,16 @@ def main(): argument_spec = dict( src=dict(required=True), dest=dict(required=True), - backup=dict(default=False, choices=BOOLEANS), - force = dict(default='yes', choices=BOOLEANS, aliases=['thirsty']), + backup=dict(default=False, choices=BOOLEANS, type='bool'), + force = dict(default='yes', choices=BOOLEANS, aliases=['thirsty'], type='bool'), ), add_file_common_args=True, ) src = os.path.expanduser(module.params['src']) dest = os.path.expanduser(module.params['dest']) - backup = module.boolean(module.params.get('backup', False)) - force = module.boolean(module.params['force']) + backup = module.params['backup'] + force = module.params['force'] if not os.path.exists(src): module.fail_json(msg="Source %s failed to transfer" % (src)) diff --git a/library/cron b/library/cron index 7d0ef5db4c0..7d54b8c9533 100644 --- a/library/cron +++ b/library/cron @@ -259,17 +259,17 @@ def main(): job=dict(required=False), cron_file=dict(required=False), state=dict(default='present', choices=['present', 'absent']), - backup=dict(default=False, choices=BOOLEANS), + backup=dict(default=False, choices=BOOLEANS, type='bool'), minute=dict(default='*'), hour=dict(default='*'), day=dict(default='*'), month=dict(default='*'), weekday=dict(default='*'), - reboot=dict(required=False, default=False, choices=BOOLEANS) + reboot=dict(required=False, default=False, choices=BOOLEANS, type='bool') ) ) - backup = module.boolean(module.params.get('backup', False)) + backup = module.params['backup'] name = module.params['name'] user = module.params['user'] job = module.params['job'] @@ -279,7 +279,7 @@ def main(): day = module.params['day'] month = module.params['month'] weekday = module.params['weekday'] - reboot = module.boolean(module.params.get('reboot', False)) + reboot = module.params['reboot'] state = module.params['state'] do_install = module.params['state'] == 'present' changed = False diff --git a/library/easy_install b/library/easy_install index 9aeef9bcb0c..a6ff3fdc8f1 100644 --- a/library/easy_install +++ b/library/easy_install @@ -82,7 +82,7 @@ def main(): arg_spec = dict( name=dict(required=True), virtualenv=dict(default=None, required=False), - virtualenv_site_packages=dict(default='no', choices=BOOLEANS), + virtualenv_site_packages=dict(default='no', choices=BOOLEANS, type='bool'), virtualenv_command=dict(default='virtualenv', required=False), ) @@ -91,7 +91,7 @@ def main(): name = module.params['name'] env = module.params['virtualenv'] easy_install = module.get_bin_path('easy_install', True, ['%s/bin' % env]) - site_packages = module.boolean(module.params['virtualenv_site_packages']) + site_packages = module.params['virtualenv_site_packages'] virtualenv_command = module.params['virtualenv_command'] rc = 0 diff --git a/library/file b/library/file index 6401697b68f..0710ba3fdba 100644 --- a/library/file +++ b/library/file @@ -140,7 +140,7 @@ def main(): argument_spec = dict( state = dict(choices=['file','directory','link','absent'], default='file'), path = dict(aliases=['dest', 'name'], required=True), - recurse = dict(default='no', choices=BOOLEANS) + recurse = dict(default='no', choices=BOOLEANS, type='bool') ), add_file_common_args=True, supports_check_mode=True @@ -215,7 +215,7 @@ def main(): changed = True changed = module.set_directory_attributes_if_different(file_args, changed) - recurse = module.boolean(params['recurse']) + recurse = params['recurse'] if recurse: for root,dirs,files in os.walk( file_args['path'] ): for dir in dirs: diff --git a/library/get_url b/library/get_url index 2a8254f1355..89a1ceae217 100644 --- a/library/get_url +++ b/library/get_url @@ -195,14 +195,14 @@ def main(): argument_spec = dict( url = dict(required=True), dest = dict(required=True), - force = dict(default='no', choices=BOOLEANS, aliases=['thirsty']) + force = dict(default='no', choices=BOOLEANS, aliases=['thirsty'], type='bool') ), add_file_common_args=True ) url = module.params['url'] dest = os.path.expanduser(module.params['dest']) - force = module.boolean(module.params['force']) + force = module.params['force'] if os.path.isdir(dest): dest = os.path.join(dest, url_filename(url)) diff --git a/library/git b/library/git index 020306bd1f2..2909bd62da3 100644 --- a/library/git +++ b/library/git @@ -223,7 +223,7 @@ def main(): repo=dict(required=True, aliases=['name']), version=dict(default='HEAD'), remote=dict(default='origin'), - force=dict(default='yes', choices=['yes', 'no'], aliases=['force']) + force=dict(default='yes', choices=BOOLEANS, type='bool') ) ) @@ -231,7 +231,7 @@ def main(): repo = module.params['repo'] version = module.params['version'] remote = module.params['remote'] - force = module.boolean(module.params['force']) + force = module.params['force'] gitconfig = os.path.join(dest, '.git', 'config') diff --git a/library/hg b/library/hg index 0fdfb08c0f0..87a5b62e72d 100644 --- a/library/hg +++ b/library/hg @@ -204,15 +204,15 @@ def main(): repo = dict(required=True), dest = dict(required=True), revision = dict(default="default"), - force = dict(default='yes', choices=['yes', 'no']), - purge = dict(default='no', choices=['yes', 'no']) + force = dict(default='yes', choices=BOOLEANS, type='bool'), + purge = dict(default='no', choices=BOOLEANS, type='bool') ), ) repo = module.params['repo'] dest = module.params['dest'] revision = module.params['revision'] - force = module.boolean(module.params['force']) - purge = module.boolean(module.params['purge']) + force = module.params['force'] + purge = module.params['purge'] hgrc = os.path.join(dest, '.hg/hgrc') # initial states diff --git a/library/ini_file b/library/ini_file index cc9771380da..9b1067a843c 100644 --- a/library/ini_file +++ b/library/ini_file @@ -158,7 +158,7 @@ def main(): section = dict(required=True), option = dict(required=False), value = dict(required=False), - backup = dict(default='no', choices=BOOLEANS), + backup = dict(default='no', choices=BOOLEANS, type='bool'), state = dict(default='present', choices=['present', 'absent']) ), add_file_common_args = True @@ -171,7 +171,7 @@ def main(): option = module.params['option'] value = module.params['value'] state = module.params['state'] - backup = module.boolean(module.params['backup']) + backup = module.params['backup'] changed = do_ini(module, dest, section, option, value, state, backup) diff --git a/library/lineinfile b/library/lineinfile index f610c5b9854..ef879d7ae85 100644 --- a/library/lineinfile +++ b/library/lineinfile @@ -241,17 +241,17 @@ def main(): line=dict(aliases=['value']), insertafter=dict(default=None), insertbefore=dict(default=None), - create=dict(default=False, choices=BOOLEANS), - backup=dict(default=False, choices=BOOLEANS), + create=dict(default=False, choices=BOOLEANS, type='bool'), + backup=dict(default=False, choices=BOOLEANS, type='bool'), ), mutually_exclusive = [['insertbefore', 'insertafter']], - add_file_common_args=True, + add_file_common_args = True, supports_check_mode = True ) params = module.params - create = module.boolean(module.params.get('create', False)) - backup = module.boolean(module.params.get('backup', False)) + create = module.params['create'] + backup = module.params['backup'] dest = os.path.expanduser(params['dest']) if params['state'] == 'present': diff --git a/library/pacman b/library/pacman index ca95e2e37ad..8a256a54c6a 100755 --- a/library/pacman +++ b/library/pacman @@ -134,7 +134,7 @@ def main(): module = AnsibleModule( argument_spec = dict( state = dict(default="installed", choices=["installed","absent"]), - update_cache = dict(default="no", choices=["yes","no"], aliases=["update-cache"]), + update_cache = dict(default="no", choices=BOOLEANS, aliases=["update-cache"], type='bool'), name = dict(aliases=["pkg"], required=True))) @@ -144,7 +144,7 @@ def main(): p = module.params - if module.boolean(p["update_cache"]): + if p["update_cache"]: update_package_db(module) pkgs = p["name"].split(",") diff --git a/library/pip b/library/pip index 72918afad29..d5418255c30 100644 --- a/library/pip +++ b/library/pip @@ -158,9 +158,9 @@ def main(): version=dict(default=None, required=False), requirements=dict(default=None, required=False), virtualenv=dict(default=None, required=False), - virtualenv_site_packages=dict(default='no', choices=BOOLEANS), + virtualenv_site_packages=dict(default='no', choices=BOOLEANS, type='bool'), virtualenv_command=dict(default='virtualenv', required=False), - use_mirrors=dict(default='yes', choices=BOOLEANS), + use_mirrors=dict(default='yes', choices=BOOLEANS, type='bool'), extra_args=dict(default=None, required=False), ), required_one_of=[['name', 'requirements']], @@ -171,7 +171,7 @@ def main(): name = module.params['name'] version = module.params['version'] requirements = module.params['requirements'] - use_mirrors = module.boolean(module.params['use_mirrors']) + use_mirrors = module.params['use_mirrors'] extra_args = module.params['extra_args'] if state == 'latest' and version is not None: @@ -188,7 +188,7 @@ def main(): if env: virtualenv = module.get_bin_path(virtualenv_command, True) if not os.path.exists(os.path.join(env, 'bin', 'activate')): - if module.boolean(module.params['virtualenv_site_packages']): + if module.params['virtualenv_site_packages']: cmd = '%s --system-site-packages %s' % (virtualenv, env) else: cmd = '%s %s' % (virtualenv, env) diff --git a/library/rabbitmq_plugin b/library/rabbitmq_plugin index 4bd549dc618..3b21607ee7f 100644 --- a/library/rabbitmq_plugin +++ b/library/rabbitmq_plugin @@ -75,7 +75,7 @@ class RabbitMqPlugins(object): def main(): arg_spec = dict( names=dict(required=True, aliases=['name']), - new_only=dict(default='no', choices=BOOLEANS), + new_only=dict(default='no', choices=BOOLEANS, type='bool'), state=dict(default='enabled', choices=['enabled', 'disabled']) ) module = AnsibleModule( @@ -84,7 +84,7 @@ def main(): ) names = module.params['names'].split(',') - new_only = module.boolean(module.params['new_only']) + new_only = module.params['new_only'] state = module.params['state'] rabbitmq_plugins = RabbitMqPlugins(module) diff --git a/library/rabbitmq_user b/library/rabbitmq_user index 497ce29e5d9..a5f3ce67660 100644 --- a/library/rabbitmq_user +++ b/library/rabbitmq_user @@ -178,7 +178,7 @@ def main(): configure_priv=dict(default='^$'), write_priv=dict(default='^$'), read_priv=dict(default='^$'), - force=dict(default='no', choices=BOOLEANS), + force=dict(default='no', choices=BOOLEANS, type='bool'), state=dict(default='present', choices=['present', 'absent']) ) module = AnsibleModule( @@ -193,7 +193,7 @@ def main(): configure_priv = module.params['configure_priv'] write_priv = module.params['write_priv'] read_priv = module.params['read_priv'] - force = module.boolean(module.params['force']) + force = module.params['force'] state = module.params['state'] rabbitmq_user = RabbitMqUser(module, username, password, tags, vhost, configure_priv, write_priv, read_priv) diff --git a/library/rabbitmq_vhost b/library/rabbitmq_vhost index 31cf86de856..3ae0775bd8e 100644 --- a/library/rabbitmq_vhost +++ b/library/rabbitmq_vhost @@ -100,7 +100,7 @@ class RabbitMqVhost(object): def main(): arg_spec = dict( name=dict(required=True, aliases=['vhost']), - tracing=dict(default='off', choices=BOOLEANS, aliases=['trace']), + tracing=dict(default='off', choices=BOOLEANS, aliases=['trace'], type='bool'), state=dict(default='present', choices=['present', 'absent']) ) @@ -110,7 +110,7 @@ def main(): ) name = module.params['name'] - tracing = module.boolean(module.params['tracing']) + tracing = module.params['tracing'] state = module.params['state'] rabbitmq_vhost = RabbitMqVhost(module, name, tracing) diff --git a/library/seboolean b/library/seboolean index aee16693982..28893bd9bcd 100644 --- a/library/seboolean +++ b/library/seboolean @@ -158,8 +158,8 @@ def main(): module = AnsibleModule( argument_spec = dict( name=dict(required=True), - persistent=dict(default='no', choices=BOOLEANS), - state=dict(required=True, choices=BOOLEANS) + persistent=dict(default='no', choices=BOOLEANS, type='bool'), + state=dict(required=True, choices=BOOLEANS, type='bool') ) ) @@ -173,8 +173,8 @@ def main(): module.fail_json(msg="SELinux is disabled on this host.") name = module.params['name'] - persistent = module.boolean(module.params['persistent']) - state = module.boolean(module.params['state']) + persistent = module.params['persistent'] + state = module.params['state'] result = {} result['name'] = name diff --git a/library/service b/library/service index 963d8e6a68d..b86c541e0f4 100644 --- a/library/service +++ b/library/service @@ -101,7 +101,7 @@ class Service(object): self.name = module.params['name'] self.state = module.params['state'] self.pattern = module.params['pattern'] - self.enable = module.boolean(module.params.get('enabled', None)) + self.enable = module.params['enabled'] self.changed = False self.running = None self.action = None @@ -649,7 +649,7 @@ def main(): name = dict(required=True), state = dict(choices=['running', 'started', 'stopped', 'restarted', 'reloaded']), pattern = dict(required=False, default=None), - enabled = dict(choices=BOOLEANS), + enabled = dict(choices=BOOLEANS, type='bool'), arguments = dict(aliases=['args'], default=''), ), supports_check_mode=True diff --git a/library/subversion b/library/subversion index c8cd5198b67..c92c3c2627b 100644 --- a/library/subversion +++ b/library/subversion @@ -129,7 +129,7 @@ def main(): dest=dict(required=True), repo=dict(required=True, aliases=['name', 'repository']), revision=dict(default='HEAD', aliases=['rev']), - force=dict(default='yes', choices=['yes', 'no']), + force=dict(default='yes', choices=BOOLEANS, type='bool'), username=dict(required=False), password=dict(required=False), ) @@ -138,7 +138,7 @@ def main(): dest = os.path.expanduser(module.params['dest']) repo = module.params['repo'] revision = module.params['revision'] - force = module.boolean(module.params['force']) + force = module.params['force'] username = module.params['username'] password = module.params['password'] diff --git a/library/sysctl b/library/sysctl index 0c06e7989c7..a310f5dc9d3 100644 --- a/library/sysctl +++ b/library/sysctl @@ -208,7 +208,7 @@ def main(): value = dict(aliases=['val'], required=False), state = dict(default='present', choices=['present', 'absent']), checks = dict(default='both', choices=['none', 'before', 'after', 'both']), - reload = dict(default=True, choices=BOOLEANS), + reload = dict(default=True, choices=BOOLEANS, type='bool'), sysctl_file = dict(default='/etc/sysctl.conf') ) ) @@ -219,7 +219,7 @@ def main(): 'name': module.params['name'], 'state': module.params['state'], 'checks': module.params['checks'], - 'reload': module.boolean(module.params.get('reload', True)), + 'reload': module.params['reload'], 'value': module.params.get('value'), 'sysctl_file': module.params['sysctl_file'] } diff --git a/library/uri b/library/uri index 5f0353f7787..074658ec7af 100644 --- a/library/uri +++ b/library/uri @@ -267,9 +267,9 @@ def main(): password = dict(required=False, default=None), body = dict(required=False, default=None), method = dict(required=False, default='GET', choices=['GET', 'POST', 'PUT', 'HEAD', 'DELETE', 'OPTIONS']), - return_content = dict(required=False, default='no', choices=BOOLEANS), - force_basic_auth = dict(required=False, default='no', choices=BOOLEANS), - follow_redirects = dict(required=False, default='no', choices=BOOLEANS), + return_content = dict(required=False, default='no', choices=BOOLEANS, type='bool'), + force_basic_auth = dict(required=False, default='no', choices=BOOLEANS, type='bool'), + follow_redirects = dict(required=False, default='no', choices=BOOLEANS, type='bool'), creates = dict(required=False, default=None), removes = dict(required=False, default=None), status_code = dict(required=False, default="200"), @@ -289,9 +289,9 @@ def main(): body = module.params['body'] method = module.params['method'] dest = module.params['dest'] - return_content = module.boolean(module.params['return_content']) - force_basic_auth = module.boolean(module.params['force_basic_auth']) - follow_redirects = module.boolean(module.params['follow_redirects']) + return_content = module.params['return_content'] + force_basic_auth = module.params['force_basic_auth'] + follow_redirects = module.params['follow_redirects'] creates = module.params['creates'] removes = module.params['removes'] status_code = module.params['status_code'] diff --git a/library/user b/library/user index 93aae6ccbe6..f1e99e44b3e 100644 --- a/library/user +++ b/library/user @@ -202,12 +202,12 @@ class User(object): self.home = module.params['home'] self.shell = module.params['shell'] self.password = module.params['password'] - self.force = module.boolean(module.params['force']) - self.remove = module.boolean(module.params['remove']) - self.createhome = module.boolean(module.params['createhome']) - self.system = module.boolean(module.params['system']) - self.append = module.boolean(module.params['append']) - self.sshkeygen = module.boolean(module.params['generate_ssh_key']) + self.force = module.params['force'] + self.remove = module.params['remove'] + self.createhome = module.params['createhome'] + self.system = module.params['system'] + self.append = module.params['append'] + self.sshkeygen = module.params['generate_ssh_key'] self.ssh_bits = module.params['ssh_key_bits'] self.ssh_type = module.params['ssh_key_type'] self.ssh_comment = module.params['ssh_key_comment'] @@ -1002,15 +1002,15 @@ def main(): shell=dict(default=None), password=dict(default=None), # following options are specific to userdel - force=dict(default='no', choices=BOOLEANS), - remove=dict(default='no', choices=BOOLEANS), + force=dict(default='no', choices=BOOLEANS, type='bool'), + remove=dict(default='no', choices=BOOLEANS, type='bool'), # following options are specific to useradd - createhome=dict(default='yes', choices=BOOLEANS), - system=dict(default='no', choices=BOOLEANS), + createhome=dict(default='yes', choices=BOOLEANS, type='bool'), + system=dict(default='no', choices=BOOLEANS, type='bool'), # following options are specific to usermod - append=dict(default='no', choices=BOOLEANS), + append=dict(default='no', choices=BOOLEANS, type='bool'), # following are specific to ssh key generation - generate_ssh_key=dict(choices=BOOLEANS), + generate_ssh_key=dict(choices=BOOLEANS, type='bool'), ssh_key_bits=dict(default=ssh_defaults['bits']), ssh_key_type=dict(default=ssh_defaults['type']), ssh_key_file=dict(default=None),