Last active
May 27, 2018 14:21
-
-
Save krdlab/a154bc6491dc2768c4ca4a0d6b7c0eac to your computer and use it in GitHub Desktop.
Ansible モジュールのサンプル (check mode & diff 対応済)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import os | |
from tempfile import gettempdir | |
from ansible.module_utils.basic import AnsibleModule | |
class BasicFile(object): | |
def __init__(self, path): | |
self.path = path | |
def exists(self): | |
return os.path.exists(self.path) | |
def touch(self): | |
pass | |
def remove(self): | |
pass | |
class CheckModeFile(BasicFile): | |
def __init__(self, path): | |
super(CheckModeFile, self).__init__(path) | |
def touch(self): | |
return not self.exists() | |
def remove(self): | |
return self.exists() | |
class File(BasicFile): | |
def __init__(self, path): | |
super(File, self).__init__(path) | |
def touch(self): | |
with open(self.path, 'a'): | |
os.utime(self.path, None) | |
def remove(self): | |
if self.exists(): | |
os.remove(self.path) | |
return True | |
else: | |
return False | |
class SimpleFileModule(object): | |
def __init__(self, expected, check_mode): | |
self.expected = expected | |
self.check_mode = check_mode | |
self.file = self.__file(expected['path']) | |
def __file(self, path): | |
if self.check_mode: | |
return CheckModeFile(path) | |
else: | |
return File(path) | |
def ensure(self): | |
if self.expected['state'] == 'present': | |
return self.__ensure_present() | |
else: | |
return self.__ensure_absent() | |
def __ensure_present(self): | |
diff = self.__create_diff() | |
if self.file.exists(): | |
return (False, diff, self.expected) | |
else: | |
self.file.touch() | |
return (True, diff, self.expected) | |
def __ensure_absent(self): | |
diff = self.__create_diff() | |
return (self.file.remove(), diff, self.expected) | |
def __create_diff(self): | |
path = self.expected['path'] | |
act_state, exp_state = self.__file_state(), self.expected['state'] | |
diff = dict(before=dict(path=path), after=dict(path=path)) | |
if act_state != exp_state: | |
diff['before']['state'] = act_state | |
diff['after']['state'] = exp_state | |
return diff | |
def __file_state(self): | |
return 'present' if self.file.exists() else 'absent' | |
def main(): | |
module = AnsibleModule( | |
argument_spec=dict( | |
path=dict(required=True, type='path'), | |
state=dict(choices=['present', 'absent'], default=None) | |
), | |
supports_check_mode=True | |
) | |
simplefile = SimpleFileModule( | |
expected=dict( | |
path=module.params['path'], | |
state=module.params['state'] | |
), | |
check_mode=module.check_mode | |
) | |
changed, diff, params = simplefile.ensure() | |
module.exit_json(changed=changed, diff=diff, simplefile=params) | |
if __name__ == '__main__': | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment