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
This commit is contained in:
parent
b55d039f67
commit
d7bf3749b7
1 changed files with 29 additions and 8 deletions
|
@ -62,12 +62,20 @@ import re
|
|||
|
||||
|
||||
class Blacklist(object):
|
||||
def __init__(self, module, filename):
|
||||
if not os.path.exists(filename):
|
||||
open(filename, 'a').close()
|
||||
|
||||
def __init__(self, module, filename, checkmode):
|
||||
self.filename = filename
|
||||
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):
|
||||
return '^blacklist\s*' + self.module + '$'
|
||||
|
@ -96,7 +104,10 @@ class Blacklist(object):
|
|||
lines = self.readlines()
|
||||
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:
|
||||
if not re.match(pattern, line.strip()):
|
||||
|
@ -105,9 +116,14 @@ class Blacklist(object):
|
|||
f.close()
|
||||
|
||||
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.close()
|
||||
|
||||
def main():
|
||||
module = AnsibleModule(
|
||||
|
@ -117,7 +133,7 @@ def main():
|
|||
default='present'),
|
||||
blacklist_file=dict(required=False, default=None)
|
||||
),
|
||||
supports_check_mode=False,
|
||||
supports_check_mode=True,
|
||||
)
|
||||
|
||||
args = dict(changed=False, failed=False,
|
||||
|
@ -128,7 +144,12 @@ def main():
|
|||
if 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 args['state'] == 'absent':
|
||||
|
|
Loading…
Reference in a new issue