diff options
author | Kenny Woodson <kwoodson@redhat.com> | 2018-02-13 20:19:33 -0500 |
---|---|---|
committer | Kenny Woodson <kwoodson@redhat.com> | 2018-02-13 22:21:44 -0500 |
commit | 42fd5866e3eeb878f7d890005bf69365fade5482 (patch) | |
tree | 1482bdc24b7232a3103c4865f5306f304eac864b /roles/lib_utils/src | |
parent | ea96a91a2775321aa60062e25934e22ad8be9de3 (diff) | |
download | openshift-42fd5866e3eeb878f7d890005bf69365fade5482.tar.gz openshift-42fd5866e3eeb878f7d890005bf69365fade5482.tar.bz2 openshift-42fd5866e3eeb878f7d890005bf69365fade5482.tar.xz openshift-42fd5866e3eeb878f7d890005bf69365fade5482.zip |
Adding ability to yedit json files.
Diffstat (limited to 'roles/lib_utils/src')
-rw-r--r-- | roles/lib_utils/src/ansible/yedit.py | 2 | ||||
-rw-r--r-- | roles/lib_utils/src/class/yedit.py | 24 |
2 files changed, 19 insertions, 7 deletions
diff --git a/roles/lib_utils/src/ansible/yedit.py b/roles/lib_utils/src/ansible/yedit.py index c4b818cf1..c2ae08654 100644 --- a/roles/lib_utils/src/ansible/yedit.py +++ b/roles/lib_utils/src/ansible/yedit.py @@ -13,7 +13,7 @@ def main(): debug=dict(default=False, type='bool'), src=dict(default=None, type='str'), content=dict(default=None), - content_type=dict(default='dict', choices=['dict']), + content_type=dict(default='yaml', choices=['yaml', 'json']), key=dict(default='', type='str'), value=dict(), value_type=dict(default='', type='str'), diff --git a/roles/lib_utils/src/class/yedit.py b/roles/lib_utils/src/class/yedit.py index 0a4fbe07a..5f69d797c 100644 --- a/roles/lib_utils/src/class/yedit.py +++ b/roles/lib_utils/src/class/yedit.py @@ -207,10 +207,16 @@ class Yedit(object): pass # Try to use RoundTripDumper if supported. - try: - Yedit._write(self.filename, yaml.dump(self.yaml_dict, Dumper=yaml.RoundTripDumper)) - except AttributeError: - Yedit._write(self.filename, yaml.safe_dump(self.yaml_dict, default_flow_style=False)) + if self.content_type == 'yaml': + try: + Yedit._write(self.filename, yaml.dump(self.yaml_dict, Dumper=yaml.RoundTripDumper)) + except AttributeError: + Yedit._write(self.filename, yaml.safe_dump(self.yaml_dict, default_flow_style=False)) + elif self.content_type == 'json': + Yedit._write(self.filename, json.dumps(self.yaml_dict, indent=4, sort_keys=True)) + else: + raise YeditException('Unsupported content_type: {}.'.format(self.content_type) + + 'Please specify a content_type of yaml or json.') return (True, self.yaml_dict) @@ -258,7 +264,7 @@ class Yedit(object): # Try to use RoundTripLoader if supported. try: - self.yaml_dict = yaml.safe_load(contents, yaml.RoundTripLoader) + self.yaml_dict = yaml.load(contents, yaml.RoundTripLoader) except AttributeError: self.yaml_dict = yaml.safe_load(contents) @@ -503,7 +509,12 @@ class Yedit(object): curr_value = invalue if val_type == 'yaml': - curr_value = yaml.load(invalue) + try: + # AUDIT:maybe-no-member makes sense due to different yaml libraries + # pylint: disable=maybe-no-member + curr_value = yaml.safe_load(invalue, Loader=yaml.RoundTripLoader) + except AttributeError: + curr_value = yaml.safe_load(invalue) elif val_type == 'json': curr_value = json.loads(invalue) @@ -572,6 +583,7 @@ class Yedit(object): '''perform the idempotent crud operations''' yamlfile = Yedit(filename=params['src'], backup=params['backup'], + content_type=params['content_type'], separator=params['separator']) state = params['state'] |