summaryrefslogtreecommitdiffstats
path: root/roles/lib_yaml_editor/library/yedit.py
diff options
context:
space:
mode:
Diffstat (limited to 'roles/lib_yaml_editor/library/yedit.py')
-rw-r--r--roles/lib_yaml_editor/library/yedit.py42
1 files changed, 31 insertions, 11 deletions
diff --git a/roles/lib_yaml_editor/library/yedit.py b/roles/lib_yaml_editor/library/yedit.py
index 9b565d0c7..356cf07a5 100644
--- a/roles/lib_yaml_editor/library/yedit.py
+++ b/roles/lib_yaml_editor/library/yedit.py
@@ -1,11 +1,20 @@
-#!/usr/bin/env python
+#!usr/bin/env python
+# ___ ___ _ _ ___ ___ _ _____ ___ ___
+# / __| __| \| | __| _ \ /_\_ _| __| \
+# | (_ | _|| .` | _|| / / _ \| | | _|| |) |
+# \___|___|_|\_|___|_|_\/_/_\_\_|_|___|___/_ _____
+# | \ / _ \ | \| |/ _ \_ _| | __| \_ _|_ _|
+# | |) | (_) | | .` | (_) || | | _|| |) | | | |
+# |___/ \___/ |_|\_|\___/ |_| |___|___/___| |_|
+
'''
-module for openshift cloud secrets
+module for managing yaml files
'''
import os
import yaml
+
class YeditException(Exception):
''' Exception class for Yedit '''
pass
@@ -13,10 +22,14 @@ class YeditException(Exception):
class Yedit(object):
''' Class to modify yaml files '''
- def __init__(self, filename):
+ def __init__(self, filename=None, content=None):
+ self.content = content
self.filename = filename
- self.__yaml_dict = None
- self.get()
+ self.__yaml_dict = content
+ if self.filename and not self.content:
+ self.get()
+ elif self.filename and self.content:
+ self.write()
@property
def yaml_dict(self):
@@ -84,8 +97,11 @@ class Yedit(object):
def write(self):
''' write to file '''
+ if not self.filename:
+ raise YeditException('Please specify a filename.')
+
with open(self.filename, 'w') as yfd:
- yfd.write(yaml.dump(self.yaml_dict, default_flow_style=False))
+ yfd.write(yaml.safe_dump(self.yaml_dict, default_flow_style=False))
def read(self):
''' write to file '''
@@ -105,6 +121,7 @@ class Yedit(object):
return True
return False
+
def get(self):
''' return yaml file '''
contents = self.read()
@@ -157,7 +174,6 @@ class Yedit(object):
return (False, self.get())
-
def main():
'''
ansible oc module for secrets
@@ -168,19 +184,19 @@ def main():
state=dict(default='present', type='str',
choices=['present', 'absent', 'list']),
debug=dict(default=False, type='bool'),
-
src=dict(default=None, type='str'),
+ content=dict(default=None, type='dict'),
key=dict(default=None, type='str'),
value=dict(default=None, type='str'),
value_format=dict(default='yaml', choices=['yaml', 'json'], type='str'),
),
- mutually_exclusive=[["contents", "files"]],
+ #mutually_exclusive=[["src", "content"]],
supports_check_mode=True,
)
state = module.params['state']
- yamlfile = Yedit(module.params['src'])
+ yamlfile = Yedit(module.params['src'], module.params['content'])
rval = yamlfile.get()
if not rval and state != 'present':
@@ -205,7 +221,11 @@ def main():
rval = yamlfile.put(module.params['key'], value)
module.exit_json(changed=rval[0], results=rval[1], state="present")
- rval = yamlfile.create(module.params['key'], value)
+ if not module.params['content']:
+ rval = yamlfile.create(module.params['key'], value)
+ else:
+ yamlfile.write()
+ rval = yamlfile.get()
module.exit_json(changed=rval[0], results=rval[1], state="present")
module.exit_json(failed=True,