Add Check Mode capability to kernel_blacklist module (#21007)

* Add Check Mode capability to kernel_blacklist module
* Add suggested changes from @tmshn
* Pass a bool into `Blacklist` that'll just be `module.check_mode`
* Move detection and creation of a file to a separate function within `Blacklist`
* If there's no file on the system and we are running under `Check Mode`, set `self.filename` to `os.devnull` AND mark a change, as the module would have created an empty file.
* Whenever a `self.filename` is being opened in a mode where changes can be made, replace with an if statement that checks `self.checkmode` and if true then open up `os.devnull` instead
pull/21059/head
Imran Hussain 8 years ago committed by jctanner
parent b55d039f67
commit d7bf3749b7

@ -62,12 +62,20 @@ import re
class Blacklist(object): class Blacklist(object):
def __init__(self, module, filename): def __init__(self, module, filename, checkmode):
if not os.path.exists(filename):
open(filename, 'a').close()
self.filename = filename self.filename = filename
self.module = module self.module = module
self.checkmode = checkmode
def create_file(self):
if not self.checkmode and not os.path.exists(self.filename):
open(self.filename, 'a').close()
return True
elif self.checkmode and not os.path.exists(self.filename):
self.filename = os.devnull
return True
else:
return False
def get_pattern(self): def get_pattern(self):
return '^blacklist\s*' + self.module + '$' return '^blacklist\s*' + self.module + '$'
@ -96,7 +104,10 @@ class Blacklist(object):
lines = self.readlines() lines = self.readlines()
pattern = self.get_pattern() pattern = self.get_pattern()
f = open(self.filename, 'w') if self.checkmode:
f = open(os.devnull, 'w')
else:
f = open(self.filename, 'w')
for line in lines: for line in lines:
if not re.match(pattern, line.strip()): if not re.match(pattern, line.strip()):
@ -105,9 +116,14 @@ class Blacklist(object):
f.close() f.close()
def add_module(self): def add_module(self):
f = open(self.filename, 'a') if self.checkmode:
f = open(os.devnull, 'a')
else:
f = open(self.filename, 'a')
f.write('blacklist %s\n' % self.module) f.write('blacklist %s\n' % self.module)
f.close()
def main(): def main():
module = AnsibleModule( module = AnsibleModule(
@ -117,7 +133,7 @@ def main():
default='present'), default='present'),
blacklist_file=dict(required=False, default=None) blacklist_file=dict(required=False, default=None)
), ),
supports_check_mode=False, supports_check_mode=True,
) )
args = dict(changed=False, failed=False, args = dict(changed=False, failed=False,
@ -128,7 +144,12 @@ def main():
if module.params['blacklist_file']: if module.params['blacklist_file']:
filename = module.params['blacklist_file'] filename = module.params['blacklist_file']
blacklist = Blacklist(args['name'], filename) blacklist = Blacklist(args['name'], filename, module.check_mode)
if blacklist.create_file():
args['changed'] = True
else:
args['changed'] = False
if blacklist.module_listed(): if blacklist.module_listed():
if args['state'] == 'absent': if args['state'] == 'absent':

Loading…
Cancel
Save