diff --git a/salt/modules/system.py b/salt/modules/system.py index 1632d290c8..c2d1841991 100644 --- a/salt/modules/system.py +++ b/salt/modules/system.py @@ -11,6 +11,7 @@ import os.path # Import salt libs import salt.utils +import salt.ext.six as six from salt.exceptions import CommandExecutionError, SaltInvocationError @@ -504,10 +505,14 @@ def get_computer_desc(): match = pattern.match(line) if match: # get rid of whitespace then strip off quotes - desc = _strip_quotes(match.group(1).strip()).replace('\\"', '"') + desc = _strip_quotes(match.group(1).strip()) # no break so we get the last occurance except IOError: return False + if six.PY3: + desc = desc.replace('\\"', '"').decode('unicode_escape') + else: + desc = desc.replace('\\"', '"').decode('string_escape') return desc @@ -526,9 +531,13 @@ def set_computer_desc(desc): salt '*' system.set_computer_desc "Michael's laptop" ''' + if six.PY3: + desc = desc.encode('unicode_escape').replace('"', '\\"') + else: + desc = desc.encode('string_escape').replace('"', '\\"') hostname_cmd = salt.utils.which('hostnamectl') if hostname_cmd: - result = __salt__['cmd.retcode']('{0} set-hostname --pretty {1}'.format(hostname_cmd, desc)) + result = __salt__['cmd.retcode']('{0} set-hostname --pretty "{1}"'.format(hostname_cmd, desc)) return True if result == 0 else False if not os.path.isfile('/etc/machine-info'): @@ -537,7 +546,7 @@ def set_computer_desc(desc): is_pretty_hostname_found = False pattern = re.compile(r'^\s*PRETTY_HOSTNAME=(.*)$') - new_line = 'PRETTY_HOSTNAME="{0}"'.format(desc.replace('"', '\\"')) + new_line = 'PRETTY_HOSTNAME="{0}"'.format(desc) try: with salt.utils.fopen('/etc/machine-info', 'r+') as mach_info: lines = mach_info.readlines()