Update apt_repository to query if repo is already configured

This adds two dependencies to the apt_repository module:  apt and
apt_pkg.  These come from the package python-apt.  This is used to parse
the host's sources.list and examine whether the repo is already
configured.  This then eliminates the unnecessary
'add-apt-repository --remove' from running.
pull/1907/head
Stephen Fromm 12 years ago
parent 5f5b7cc56b
commit e0741e7810

@ -52,10 +52,18 @@ examples:
description: Add nginx stable repository from PPA description: Add nginx stable repository from PPA
- code: "apt_repository: repo='deb http://archive.canonical.com/ubuntu hardy partner'" - code: "apt_repository: repo='deb http://archive.canonical.com/ubuntu hardy partner'"
description: Add specified repository into sources. description: Add specified repository into sources.
requirements: [ python-apt ]
''' '''
import platform import platform
try:
import apt
import apt_pkg
HAVE_PYAPT = True
except ImportError:
HAVE_PYAPT = False
APT = "/usr/bin/apt-get" APT = "/usr/bin/apt-get"
ADD_APT_REPO = 'add-apt-repository' ADD_APT_REPO = 'add-apt-repository'
@ -64,6 +72,16 @@ def check_cmd_needs_y():
return True return True
return False return False
def repo_exists(module, repo):
configured = False
slist = apt_pkg.SourceList()
if not slist.read_main_list():
module.fail_json(msg="Failed to parse sources.list")
for metaindex in slist.list:
if repo in metaindex.uri:
configured = True
return configured
def main(): def main():
add_apt_repository = None add_apt_repository = None
@ -74,6 +92,9 @@ def main():
module = AnsibleModule(argument_spec=arg_spec) module = AnsibleModule(argument_spec=arg_spec)
if not HAVE_PYAPT:
module.fail_json(msg="Could not import python modules: apt, apt_pkg. Please install python-apt package.")
add_apt_repository = module.get_bin_path(ADD_APT_REPO, True) add_apt_repository = module.get_bin_path(ADD_APT_REPO, True)
if check_cmd_needs_y(): if check_cmd_needs_y():
add_apt_repository += ' -y' add_apt_repository += ' -y'
@ -81,30 +102,42 @@ def main():
repo = module.params['repo'] repo = module.params['repo']
state = module.params['state'] state = module.params['state']
cmd = '%s "%s" --remove' % (add_apt_repository, repo) repo_url = repo
rc, out, err = module.run_command(cmd) if 'ppa' in repo_url:
existed = 'Error' not in out # looks like ppa:nginx/stable
repo_url = repo.split(':')[1]
if state == 'absent': elif len(repo_url.split(' ')) > 1:
if not existed: # could be:
module.exit_json(changed=False, repo=repo, state=state) # http://myserver/path/to/repo free non-free
else: # deb http://myserver/path/to/repo free non-free
module.exit_json(changed=True, repo=repo, state=state) for i in repo_url.split():
if 'http' in i:
cmd = '%s "%s"' % (add_apt_repository, repo) repo_url = i
exists = repo_exists(module, repo_url)
rc, out, err = module.run_command(cmd)
rc = 0
changed = rc == 0 and not existed out = ''
err = ''
if state == 'absent' and exists:
cmd = '%s "%s" --remove' % (add_apt_repository, repo)
rc, out, err = module.run_command(cmd)
elif state == 'present' and not exists:
cmd = '%s "%s"' % (add_apt_repository, repo)
rc, out, err = module.run_command(cmd)
else:
module.exit_json(changed=False, repo=repo, state=state)
if rc != 0: if rc != 0:
module.fail_json(msg=err) module.fail_json(msg=err)
else:
changed = True
if changed: if state == 'present' and changed:
rc, out, err = module.run_command('%s update' % APT) rc, out, err = module.run_command('%s update' % APT)
module.exit_json(changed=changed, repo=repo, state=state) module.exit_json(changed=changed, repo=repo, state=state)
# this is magic, see lib/ansible/module_common.py # this is magic, see lib/ansible/module_common.py
#<<INCLUDE_ANSIBLE_MODULE_COMMON>> #<<INCLUDE_ANSIBLE_MODULE_COMMON>>

Loading…
Cancel
Save