From 24ea5949bbc590598610d26ba5a76a68b59531f4 Mon Sep 17 00:00:00 2001 From: Sergey Sudakovich Date: Tue, 12 Nov 2013 15:25:39 -0800 Subject: [PATCH 1/2] fix when the system does not have lsb_release script, but has /etc/lsb_release file --- library/system/setup | 68 ++++++++++++++++++++++++++++---------------- 1 file changed, 43 insertions(+), 25 deletions(-) diff --git a/library/system/setup b/library/system/setup index c546a9ff4bc..a07267b6777 100755 --- a/library/system/setup +++ b/library/system/setup @@ -50,7 +50,7 @@ options: description: - path used for local ansible facts (*.fact) - files in this dir will be run (if executable) and their results be added to ansible_local facts - if a file is not executable it is read. + if a file is not executable it is read. File/results format can be json or ini-format required: false default: '/etc/ansible/facts.d' @@ -191,7 +191,7 @@ class Facts(object): fact_path = module.params.get('fact_path', None) if not fact_path or not os.path.exists(fact_path): return - + local = {} for fn in sorted(glob.glob(fact_path + '/*.fact')): # where it will sit under local facts @@ -204,7 +204,7 @@ class Facts(object): rc, out, err = module.run_command(fn) else: out = open(fn).read() - + # load raw json fact = 'loading %s' % fact_base try: @@ -230,7 +230,7 @@ class Facts(object): if not local: return self.facts['local'] = local - + # platform.dist() is deprecated in 2.6 # in 2.6 and newer, you should use platform.linux_distribution() def get_distribution_facts(self): @@ -376,28 +376,46 @@ class Facts(object): def get_lsb_facts(self): lsb_path = module.get_bin_path('lsb_release') - if lsb_path is None: - return self.facts - rc, out, err = module.run_command([lsb_path, "-a"]) - if rc == 0: + if lsb_path: + rc, out, err = module.run_command([lsb_path, "-a"]) + if rc == 0: + self.facts['lsb'] = {} + for line in out.split('\n'): + if len(line) < 1: + continue + value = line.split(':', 1)[1].strip() + if 'LSB Version:' in line: + self.facts['lsb']['release'] = value + elif 'Distributor ID:' in line: + self.facts['lsb']['id'] = value + elif 'Description:' in line: + self.facts['lsb']['description'] = value + elif 'Release:' in line: + self.facts['lsb']['release'] = value + elif 'Codename:' in line: + self.facts['lsb']['codename'] = value + if 'lsb' in self.facts and 'release' in self.facts['lsb']: + self.facts['lsb']['major_release'] = self.facts['lsb']['release'].split('.')[0] + elif lsb_path is None and os.path.exists('/etc/lsb-release'): self.facts['lsb'] = {} - for line in out.split('\n'): - if len(line) < 1: - continue - value = line.split(':', 1)[1].strip() - if 'LSB Version:' in line: - self.facts['lsb']['release'] = value - elif 'Distributor ID:' in line: - self.facts['lsb']['id'] = value - elif 'Description:' in line: - self.facts['lsb']['description'] = value - elif 'Release:' in line: - self.facts['lsb']['release'] = value - elif 'Codename:' in line: - self.facts['lsb']['codename'] = value + with open('/etc/lsb-release', 'r') as f: + for line in f.readlines(): + value = line.split('=',1)[1].strip() + if 'DISTRIB_ID' in line: + self.facts['lsb']['id'] = value + elif 'DISTRIB_RELEASE' in line: + self.facts['lsb']['release'] = value + elif 'DISTRIB_DESCRIPTION' in line: + self.facts['lsb']['description'] = value + elif 'DISTRIB_CODENAME' in line: + self.facts['lsb']['codename'] = value + else: + return self.facts + if 'lsb' in self.facts and 'release' in self.facts['lsb']: self.facts['lsb']['major_release'] = self.facts['lsb']['release'].split('.')[0] + def get_selinux_facts(self): if not HAVE_SELINUX: self.facts['selinux'] = False @@ -1509,15 +1527,15 @@ class LinuxNetwork(Network): ip_path = module.get_bin_path("ip") primary_data = subprocess.Popen( - [ip_path, 'addr', 'show', 'primary', device], + [ip_path, 'addr', 'show', 'primary', device], stdout=subprocess.PIPE, stderr=subprocess.PIPE).communicate()[0] secondary_data = subprocess.Popen( - [ip_path, 'addr', 'show', 'secondary', device], + [ip_path, 'addr', 'show', 'secondary', device], stdout=subprocess.PIPE, stderr=subprocess.PIPE).communicate()[0] parse_ip_output(primary_data) parse_ip_output(secondary_data, secondary=True) - # replace : by _ in interface name since they are hard to use in template + # replace : by _ in interface name since they are hard to use in template new_interfaces = {} for i in interfaces: if ':' in i: From 4d0f374dbf29aefb1e33712adb2b269bbae7371f Mon Sep 17 00:00:00 2001 From: Sergey Sudakovich Date: Mon, 18 Nov 2013 11:33:04 -0800 Subject: [PATCH 2/2] compatible with python 2.4 --- library/system/setup | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/library/system/setup b/library/system/setup index a07267b6777..de82c62e9b6 100755 --- a/library/system/setup +++ b/library/system/setup @@ -398,7 +398,8 @@ class Facts(object): self.facts['lsb']['major_release'] = self.facts['lsb']['release'].split('.')[0] elif lsb_path is None and os.path.exists('/etc/lsb-release'): self.facts['lsb'] = {} - with open('/etc/lsb-release', 'r') as f: + f = open('/etc/lsb-release', 'r') + try: for line in f.readlines(): value = line.split('=',1)[1].strip() if 'DISTRIB_ID' in line: @@ -409,6 +410,8 @@ class Facts(object): self.facts['lsb']['description'] = value elif 'DISTRIB_CODENAME' in line: self.facts['lsb']['codename'] = value + finally: + f.close() else: return self.facts