zypper_repository: Fix repo parsing for empty list

When no repositories are defined in zypper, the return code
of "zypper repos" is 6. Handle that case and don't fail
if zypper_repository has to deal with an empty repo list.

Fixes https://github.com/ansible/ansible-modules-extras/issues/795
This commit is contained in:
Thomas Bechtold 2015-07-31 16:44:01 +02:00 committed by Matt Clay
parent deb36b00d0
commit 00a5c352ec

View file

@ -95,10 +95,10 @@ def zypper_version(module):
def _parse_repos(module): def _parse_repos(module):
"""parses the output of zypper -x lr and returns a parse repo dictionary""" """parses the output of zypper -x lr and returns a parse repo dictionary"""
cmd = ['/usr/bin/zypper', '-x', 'lr'] cmd = ['/usr/bin/zypper', '-x', 'lr']
repos = []
from xml.dom.minidom import parseString as parseXML from xml.dom.minidom import parseString as parseXML
rc, stdout, stderr = module.run_command(cmd, check_rc=True) rc, stdout, stderr = module.run_command(cmd, check_rc=False)
if rc == 0:
repos = []
dom = parseXML(stdout) dom = parseXML(stdout)
repo_list = dom.getElementsByTagName('repo') repo_list = dom.getElementsByTagName('repo')
for repo in repo_list: for repo in repo_list:
@ -108,8 +108,17 @@ def _parse_repos(module):
opts['url'] = repo.getElementsByTagName('url')[0].firstChild.data opts['url'] = repo.getElementsByTagName('url')[0].firstChild.data
# A repo can be uniquely identified by an alias + url # A repo can be uniquely identified by an alias + url
repos.append(opts) repos.append(opts)
return repos return repos
# exit code 6 is ZYPPER_EXIT_NO_REPOS (no repositories defined)
elif rc == 6:
return []
else:
d = { 'zypper_exit_code': rc }
if stderr:
d['stderr'] = stderr
if stdout:
d['stdout'] = stdout
module.fail_json(msg='Failed to execute "%s"' % " ".join(cmd), **d)
def _parse_repos_old(module): def _parse_repos_old(module):
"""parses the output of zypper sl and returns a parse repo dictionary""" """parses the output of zypper sl and returns a parse repo dictionary"""