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.
reviewable/pr18780/r1
Stephen Fromm 12 years ago
parent 67a1997274
commit 25d6975f67

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

Loading…
Cancel
Save