Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix trace-creating error in apt module - also make the default setting a... #723

Merged
merged 2 commits into from
Jul 30, 2012
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
16 changes: 16 additions & 0 deletions lib/ansible/module_common.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,9 @@
# == BEGIN DYNAMICALLY INSERTED CODE ==

MODULE_ARGS = "<<INCLUDE_ANSIBLE_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
Expand All @@ -42,6 +45,7 @@
import subprocess
import sys
import syslog
import types

try:
from hashlib import md5 as _md5
Expand Down Expand Up @@ -128,6 +132,18 @@ def _log_invocation(self):
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)

Expand Down
11 changes: 4 additions & 7 deletions library/apt
Original file line number Diff line number Diff line change
Expand Up @@ -130,24 +130,21 @@ 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']:
apt_pkg.config['APT::Default-Release'] = p['default_release']
# 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')
Expand All @@ -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 == 'yes')
remove(module, p['package'], cache, purge = modules.boolean(p['purge']))

# this is magic, see lib/ansible/module_common.py
#<<INCLUDE_ANSIBLE_MODULE_COMMON>>
Expand Down