From c4c53d545a963d0b44f763a30b5c8845310a9675 Mon Sep 17 00:00:00 2001 From: Seth Vidal Date: Mon, 30 Jul 2012 13:07:19 -0400 Subject: [PATCH 1/2] fix trace-creating error in apt module - also make the default setting actualy work --- library/apt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/library/apt b/library/apt index 41705ce66ea..bce0199f6e2 100755 --- a/library/apt +++ b/library/apt @@ -164,7 +164,7 @@ def main(): install(module, p['package'], cache, default_release=p['default_release'], install_recommends=install_recommends,force=force_yes) elif p['state'] == 'removed': - remove(module, p['package'], cache, purge == 'yes') + remove(module, p['package'], cache, purge = p['purge']) # this is magic, see lib/ansible/module_common.py #<> From 21a35bde0082e19cf87afe02e6daef8ab4985f57 Mon Sep 17 00:00:00 2001 From: Seth Vidal Date: Mon, 30 Jul 2012 13:41:42 -0400 Subject: [PATCH 2/2] new patch - adds a 'boolean' function to the module_common class and cleans up the apt module to use it --- lib/ansible/module_common.py | 16 ++++++++++++++++ library/apt | 11 ++++------- 2 files changed, 20 insertions(+), 7 deletions(-) diff --git a/lib/ansible/module_common.py b/lib/ansible/module_common.py index cc1b078b275..9a1d9034e0a 100644 --- a/lib/ansible/module_common.py +++ b/lib/ansible/module_common.py @@ -23,6 +23,9 @@ MODULE_COMMON = """ # == BEGIN DYNAMICALLY INSERTED CODE == MODULE_ARGS = "<>" +BOOLEANS_TRUE = ['yes', 'on', '1', 'true', 1] +BOOLEANS_FALSE = ['no', 'off', '0', 'false', 0] +BOOLEANS = BOOLEANS_TRUE + BOOLEANS_FALSE # ansible modules can be written in any language. To simplify # development of Python modules, the functions available here @@ -42,6 +45,7 @@ import shlex import subprocess import sys import syslog +import types try: from hashlib import md5 as _md5 @@ -128,6 +132,18 @@ class AnsibleModule(object): log_args = re.sub(r'password=.+ (.*)', r"password=NOT_LOGGING_PASSWORD \1", self.args) syslog.syslog(syslog.LOG_NOTICE, 'Invoked with %s' % log_args) + def boolean(self, arg): + ''' return a bool for the arg ''' + if type(arg) in types.StringTypes: + arg = arg.lower() + + if arg in BOOLEANS_TRUE: + return True + elif arg in BOOLEANS_FALSE: + return False + else: + self.fail_json(msg='Boolean %s not in either boolean list' % arg) + def jsonify(self, data): return json.dumps(data) diff --git a/library/apt b/library/apt index bce0199f6e2..abc93eafbd1 100755 --- a/library/apt +++ b/library/apt @@ -130,7 +130,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 = (p['install_recommends'] == 'yes') + install_recommends = module.boolean(p['install_recommends']) cache = apt.Cache() if p['default_release']: @@ -138,16 +138,13 @@ def main(): # reopen cache w/ modified config cache.open(progress=None) - if p['update_cache'] == 'yes': + if modules.boolean(p['update_cache']) cache.update() cache.open(progress=None) if p['package'] == None: module.exit_json(changed=False) - if p['force'] == 'yes': - force_yes = True - else: - force_yes = False + force_yes = modules.boolean(p['force']) if p['package'].count('=') > 1: module.fail_json(msg='invalid package spec') @@ -164,7 +161,7 @@ def main(): install(module, p['package'], cache, default_release=p['default_release'], install_recommends=install_recommends,force=force_yes) elif p['state'] == 'removed': - remove(module, p['package'], cache, purge = p['purge']) + remove(module, p['package'], cache, purge = modules.boolean(p['purge'])) # this is magic, see lib/ansible/module_common.py #<>