From 73512eeb7870c64da3fdf51b003e792db3fe5521 Mon Sep 17 00:00:00 2001 From: Shinichi TAMURA Date: Thu, 15 Mar 2018 17:00:28 +0900 Subject: [PATCH] timezone module: allow suse linux as target (#36719) * timezone module: allow suse linux as target * use with statement for file handling --- lib/ansible/modules/system/timezone.py | 17 ++++++++++++++--- 1 file changed, 14 insertions(+), 3 deletions(-) diff --git a/lib/ansible/modules/system/timezone.py b/lib/ansible/modules/system/timezone.py index 315bc51681..f7d6ea555f 100644 --- a/lib/ansible/modules/system/timezone.py +++ b/lib/ansible/modules/system/timezone.py @@ -344,7 +344,7 @@ class NosystemdTimezone(Timezone): self.regexps['name'] = re.compile(r'^([^\s]+)', re.MULTILINE) self.tzline_format = '%s\n' else: - # RHEL/CentOS + # RHEL/CentOS/SUSE if self.module.get_bin_path('tzdata-update') is not None: self.update_timezone = [self.module.get_bin_path('tzdata-update', required=True)] self.allow_no_file['name'] = True @@ -353,8 +353,19 @@ class NosystemdTimezone(Timezone): # self.allow_no_file['name'] = False <- this is default behavior self.conf_files['name'] = '/etc/sysconfig/clock' self.conf_files['hwclock'] = '/etc/sysconfig/clock' - self.regexps['name'] = re.compile(r'^ZONE\s*=\s*"?([^"\s]+)"?', re.MULTILINE) - self.tzline_format = 'ZONE="%s"\n' + # The key for timezone might be `ZONE` or `TIMEZONE` + # (the former is used in RHEL/CentOS and the latter is used in SUSE linux). + # So check the content of /etc/sysconfig/clock and decide which key to use. + with open(self.conf_files['name'], mode='r') as f: + sysconfig_clock = f.read() + if re.search(r'^TIMEZONE\s*=', sysconfig_clock, re.MULTILINE): + # For SUSE + self.regexps['name'] = re.compile(r'^TIMEZONE\s*=\s*"?([^"\s]+)"?', re.MULTILINE) + self.tzline_format = 'TIMEZONE="%s"\n' + else: + # For RHEL/CentOS + self.regexps['name'] = re.compile(r'^ZONE\s*=\s*"?([^"\s]+)"?', re.MULTILINE) + self.tzline_format = 'ZONE="%s"\n' def _allow_ioerror(self, err, key): # In some cases, even if the target file does not exist,