|
@@ -33,6 +33,7 @@
|
|
|
|
|
|
from __future__ import print_function
|
|
|
import atexit
|
|
|
+import copy
|
|
|
import json
|
|
|
import os
|
|
|
import re
|
|
@@ -40,7 +41,11 @@ import shutil
|
|
|
import subprocess
|
|
|
import tempfile
|
|
|
# pylint: disable=import-error
|
|
|
-import ruamel.yaml as yaml
|
|
|
+try:
|
|
|
+ import ruamel.yaml as yaml
|
|
|
+except ImportError:
|
|
|
+ import yaml
|
|
|
+
|
|
|
from ansible.module_utils.basic import AnsibleModule
|
|
|
|
|
|
# -*- -*- -*- End included fragment: lib/import.py -*- -*- -*-
|
|
@@ -330,11 +335,15 @@ class Yedit(object):
|
|
|
if self.backup and self.file_exists():
|
|
|
shutil.copy(self.filename, self.filename + '.orig')
|
|
|
|
|
|
- # pylint: disable=no-member
|
|
|
- if hasattr(self.yaml_dict, 'fa'):
|
|
|
- self.yaml_dict.fa.set_block_style()
|
|
|
+ if hasattr(yaml, 'RoundTripDumper'):
|
|
|
+ # pylint: disable=no-member
|
|
|
+ if hasattr(self.yaml_dict, 'fa'):
|
|
|
+ self.yaml_dict.fa.set_block_style()
|
|
|
|
|
|
- Yedit._write(self.filename, yaml.dump(self.yaml_dict, Dumper=yaml.RoundTripDumper))
|
|
|
+ # pylint: disable=no-member
|
|
|
+ Yedit._write(self.filename, yaml.dump(self.yaml_dict, Dumper=yaml.RoundTripDumper))
|
|
|
+ else:
|
|
|
+ Yedit._write(self.filename, yaml.safe_dump(self.yaml_dict, default_flow_style=False))
|
|
|
|
|
|
return (True, self.yaml_dict)
|
|
|
|
|
@@ -374,10 +383,16 @@ class Yedit(object):
|
|
|
# check if it is yaml
|
|
|
try:
|
|
|
if content_type == 'yaml' and contents:
|
|
|
- self.yaml_dict = yaml.load(contents, yaml.RoundTripLoader)
|
|
|
+ # pylint: disable=no-member
|
|
|
+ if hasattr(yaml, 'RoundTripLoader'):
|
|
|
+ self.yaml_dict = yaml.load(contents, yaml.RoundTripLoader)
|
|
|
+ else:
|
|
|
+ self.yaml_dict = yaml.safe_load(contents)
|
|
|
+
|
|
|
# pylint: disable=no-member
|
|
|
if hasattr(self.yaml_dict, 'fa'):
|
|
|
self.yaml_dict.fa.set_block_style()
|
|
|
+
|
|
|
elif content_type == 'json' and contents:
|
|
|
self.yaml_dict = json.loads(contents)
|
|
|
except yaml.YAMLError as err:
|
|
@@ -542,12 +557,19 @@ class Yedit(object):
|
|
|
return (False, self.yaml_dict)
|
|
|
|
|
|
# deepcopy didn't work
|
|
|
- tmp_copy = yaml.load(yaml.round_trip_dump(self.yaml_dict,
|
|
|
- default_flow_style=False),
|
|
|
- yaml.RoundTripLoader)
|
|
|
- # pylint: disable=no-member
|
|
|
- if hasattr(self.yaml_dict, 'fa'):
|
|
|
- tmp_copy.fa.set_block_style()
|
|
|
+ if hasattr(yaml, 'round_trip_dump'):
|
|
|
+ # pylint: disable=no-member
|
|
|
+ tmp_copy = yaml.load(yaml.round_trip_dump(self.yaml_dict,
|
|
|
+ default_flow_style=False),
|
|
|
+ yaml.RoundTripLoader)
|
|
|
+
|
|
|
+ # pylint: disable=no-member
|
|
|
+ if hasattr(self.yaml_dict, 'fa'):
|
|
|
+ tmp_copy.fa.set_block_style()
|
|
|
+
|
|
|
+ else:
|
|
|
+ tmp_copy = copy.deepcopy(self.yaml_dict)
|
|
|
+
|
|
|
result = Yedit.add_entry(tmp_copy, path, value, self.separator)
|
|
|
if not result:
|
|
|
return (False, self.yaml_dict)
|
|
@@ -560,11 +582,17 @@ class Yedit(object):
|
|
|
''' create a yaml file '''
|
|
|
if not self.file_exists():
|
|
|
# deepcopy didn't work
|
|
|
- tmp_copy = yaml.load(yaml.round_trip_dump(self.yaml_dict, default_flow_style=False), # noqa: E501
|
|
|
- yaml.RoundTripLoader)
|
|
|
- # pylint: disable=no-member
|
|
|
- if hasattr(self.yaml_dict, 'fa'):
|
|
|
- tmp_copy.fa.set_block_style()
|
|
|
+ if hasattr(yaml, 'round_trip_dump'):
|
|
|
+ # pylint: disable=no-member
|
|
|
+ tmp_copy = yaml.load(yaml.round_trip_dump(self.yaml_dict, default_flow_style=False), # noqa: E501
|
|
|
+ yaml.RoundTripLoader)
|
|
|
+
|
|
|
+ # pylint: disable=no-member
|
|
|
+ if hasattr(self.yaml_dict, 'fa'):
|
|
|
+ tmp_copy.fa.set_block_style()
|
|
|
+ else:
|
|
|
+ tmp_copy = copy.deepcopy(self.yaml_dict)
|
|
|
+
|
|
|
result = Yedit.add_entry(tmp_copy, path, value, self.separator)
|
|
|
if result:
|
|
|
self.yaml_dict = tmp_copy
|
|
@@ -1006,7 +1034,12 @@ class Utils(object):
|
|
|
tmp = Utils.create_tmpfile(prefix=rname)
|
|
|
|
|
|
if ftype == 'yaml':
|
|
|
- Utils._write(tmp, yaml.dump(data, Dumper=yaml.RoundTripDumper))
|
|
|
+ # pylint: disable=no-member
|
|
|
+ if hasattr(yaml, 'RoundTripDumper'):
|
|
|
+ Utils._write(tmp, yaml.dump(data, Dumper=yaml.RoundTripDumper))
|
|
|
+ else:
|
|
|
+ Utils._write(tmp, yaml.safe_dump(data, default_flow_style=False))
|
|
|
+
|
|
|
elif ftype == 'json':
|
|
|
Utils._write(tmp, json.dumps(data))
|
|
|
else:
|
|
@@ -1088,7 +1121,11 @@ class Utils(object):
|
|
|
contents = sfd.read()
|
|
|
|
|
|
if sfile_type == 'yaml':
|
|
|
- contents = yaml.load(contents, yaml.RoundTripLoader)
|
|
|
+ # pylint: disable=no-member
|
|
|
+ if hasattr(yaml, 'RoundTripLoader'):
|
|
|
+ contents = yaml.load(contents, yaml.RoundTripLoader)
|
|
|
+ else:
|
|
|
+ contents = yaml.safe_load(contents)
|
|
|
elif sfile_type == 'json':
|
|
|
contents = json.loads(contents)
|
|
|
|