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

Add Check Mode capability to kernel_blacklist module #21007

Merged
merged 2 commits into from
Feb 6, 2017
Merged
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
37 changes: 29 additions & 8 deletions lib/ansible/modules/system/kernel_blacklist.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,12 +63,20 @@


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 + '$'
Expand Down Expand Up @@ -97,7 +105,10 @@ def remove_module(self):
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()):
Expand All @@ -106,9 +117,14 @@ def remove_module(self):
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(
Expand All @@ -118,7 +134,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,
Expand All @@ -129,7 +145,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':
Expand Down