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,21 +95,30 @@ 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)
dom = parseXML(stdout) if rc == 0:
repo_list = dom.getElementsByTagName('repo') repos = []
for repo in repo_list: dom = parseXML(stdout)
opts = {} repo_list = dom.getElementsByTagName('repo')
for o in REPO_OPTS: for repo in repo_list:
opts[o] = repo.getAttribute(o) opts = {}
opts['url'] = repo.getElementsByTagName('url')[0].firstChild.data for o in REPO_OPTS:
# A repo can be uniquely identified by an alias + url opts[o] = repo.getAttribute(o)
repos.append(opts) opts['url'] = repo.getElementsByTagName('url')[0].firstChild.data
# A repo can be uniquely identified by an alias + url
return repos repos.append(opts)
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"""