2012-08-07 22:39:31 +02:00
|
|
|
#!/usr/bin/python
|
2013-08-07 21:58:50 +02:00
|
|
|
# encoding: utf-8
|
2012-08-08 16:46:23 +02:00
|
|
|
|
|
|
|
# (c) 2012, Matt Wright <matt@nobien.net>
|
2013-08-07 21:58:50 +02:00
|
|
|
# (c) 2013, Alexander Saltanov <asd@mokote.com>
|
2014-01-02 15:41:24 +01:00
|
|
|
# (c) 2014, Rutger Spiertz <rutger@kumina.nl>
|
2012-08-08 16:46:23 +02:00
|
|
|
#
|
|
|
|
# This file is part of Ansible
|
|
|
|
#
|
|
|
|
# Ansible is free software: you can redistribute it and/or modify
|
|
|
|
# it under the terms of the GNU General Public License as published by
|
|
|
|
# the Free Software Foundation, either version 3 of the License, or
|
|
|
|
# (at your option) any later version.
|
|
|
|
#
|
|
|
|
# Ansible is distributed in the hope that it will be useful,
|
|
|
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
# GNU General Public License for more details.
|
|
|
|
#
|
|
|
|
# You should have received a copy of the GNU General Public License
|
|
|
|
# along with Ansible. If not, see <http://www.gnu.org/licenses/>.
|
2013-08-07 21:58:50 +02:00
|
|
|
|
2012-08-07 22:39:31 +02:00
|
|
|
|
2012-09-28 23:07:41 +02:00
|
|
|
DOCUMENTATION = '''
|
|
|
|
---
|
|
|
|
module: apt_repository
|
2014-07-14 10:49:08 +02:00
|
|
|
short_description: Add and remove APT repositories
|
2012-09-28 23:07:41 +02:00
|
|
|
description:
|
2013-08-07 21:58:50 +02:00
|
|
|
- Add or remove an APT repositories in Ubuntu and Debian.
|
2012-09-28 23:07:41 +02:00
|
|
|
notes:
|
2014-02-22 15:33:54 +01:00
|
|
|
- This module works on Debian and Ubuntu and requires C(python-apt).
|
2013-08-07 21:58:50 +02:00
|
|
|
- This module supports Debian Squeeze (version 6) as well as its successors.
|
|
|
|
- This module treats Debian and Ubuntu distributions separately. So PPA could be installed only on Ubuntu machines.
|
|
|
|
options:
|
|
|
|
repo:
|
|
|
|
required: true
|
|
|
|
default: none
|
|
|
|
description:
|
|
|
|
- A source string for the repository.
|
|
|
|
state:
|
|
|
|
required: false
|
|
|
|
choices: [ "absent", "present" ]
|
|
|
|
default: "present"
|
|
|
|
description:
|
|
|
|
- A source string state.
|
2014-04-14 22:42:23 +02:00
|
|
|
mode:
|
|
|
|
required: false
|
|
|
|
default: 0644
|
|
|
|
description:
|
|
|
|
- The octal mode for newly created files in sources.list.d
|
|
|
|
version_added: "1.6"
|
2013-09-18 19:14:08 +02:00
|
|
|
update_cache:
|
|
|
|
description:
|
2014-04-02 23:36:47 +02:00
|
|
|
- Run the equivalent of C(apt-get update) when a change occurs. Cache updates are run after making changes.
|
2013-09-18 19:14:08 +02:00
|
|
|
required: false
|
|
|
|
default: "yes"
|
|
|
|
choices: [ "yes", "no" ]
|
2014-04-28 19:48:52 +02:00
|
|
|
validate_certs:
|
2014-08-08 20:13:57 +02:00
|
|
|
version_added: '1.8'
|
2014-04-28 19:48:52 +02:00
|
|
|
description:
|
|
|
|
- If C(no), SSL certificates for the target repo will not be validated. This should only be used
|
|
|
|
on personally controlled sites using self-signed certificates.
|
|
|
|
required: false
|
|
|
|
default: 'yes'
|
|
|
|
choices: ['yes', 'no']
|
2015-12-16 08:48:33 +01:00
|
|
|
filename:
|
|
|
|
version_added: '2.1'
|
|
|
|
description:
|
|
|
|
- Sets the name of the source list file in sources.list.d.
|
|
|
|
Defaults to a file name based on the repository source url.
|
|
|
|
The .list extension will be automatically added.
|
|
|
|
required: false
|
2015-06-15 21:53:30 +02:00
|
|
|
author: "Alexander Saltanov (@sashka)"
|
2013-08-13 16:53:24 +02:00
|
|
|
version_added: "0.7"
|
2014-03-17 17:31:04 +01:00
|
|
|
requirements: [ python-apt ]
|
2012-09-29 01:51:55 +02:00
|
|
|
'''
|
2012-09-28 23:07:41 +02:00
|
|
|
|
2013-06-14 11:53:43 +02:00
|
|
|
EXAMPLES = '''
|
2013-08-07 21:58:50 +02:00
|
|
|
# Add specified repository into sources list.
|
|
|
|
apt_repository: repo='deb http://archive.canonical.com/ubuntu hardy partner' state=present
|
|
|
|
|
2015-12-16 08:48:33 +01:00
|
|
|
# Add specified repository into sources list using specified filename.
|
|
|
|
apt_repository: repo='deb http://dl.google.com/linux/chrome/deb/ stable main' state=present filename='google-chrome'
|
|
|
|
|
2013-08-07 21:58:50 +02:00
|
|
|
# Add source repository into sources list.
|
|
|
|
apt_repository: repo='deb-src http://archive.canonical.com/ubuntu hardy partner' state=present
|
2013-06-14 11:53:43 +02:00
|
|
|
|
2013-08-07 21:58:50 +02:00
|
|
|
# Remove specified repository from sources list.
|
|
|
|
apt_repository: repo='deb http://archive.canonical.com/ubuntu hardy partner' state=absent
|
|
|
|
|
|
|
|
# On Ubuntu target: add nginx stable repository from PPA and install its signing key.
|
|
|
|
# On Debian target: adding PPA is not available, so it will fail immediately.
|
|
|
|
apt_repository: repo='ppa:nginx/stable'
|
2013-06-14 11:53:43 +02:00
|
|
|
'''
|
|
|
|
|
2013-08-07 21:58:50 +02:00
|
|
|
import glob
|
|
|
|
import os
|
|
|
|
import re
|
|
|
|
import tempfile
|
2012-08-07 22:39:31 +02:00
|
|
|
|
2013-01-24 07:02:41 +01:00
|
|
|
try:
|
2013-09-18 11:47:39 +02:00
|
|
|
import apt
|
2013-01-24 07:02:41 +01:00
|
|
|
import apt_pkg
|
2014-05-06 15:43:37 +02:00
|
|
|
import aptsources.distro as aptsources_distro
|
|
|
|
distro = aptsources_distro.get_distro()
|
2013-08-07 21:58:50 +02:00
|
|
|
HAVE_PYTHON_APT = True
|
2013-01-24 07:02:41 +01:00
|
|
|
except ImportError:
|
2014-05-06 15:43:37 +02:00
|
|
|
distro = None
|
2013-08-07 21:58:50 +02:00
|
|
|
HAVE_PYTHON_APT = False
|
2013-01-24 07:02:41 +01:00
|
|
|
|
2012-08-13 19:06:53 +02:00
|
|
|
|
2013-08-07 21:58:50 +02:00
|
|
|
VALID_SOURCE_TYPES = ('deb', 'deb-src')
|
|
|
|
|
2014-03-12 12:15:27 +01:00
|
|
|
def install_python_apt(module):
|
|
|
|
|
|
|
|
if not module.check_mode:
|
|
|
|
apt_get_path = module.get_bin_path('apt-get')
|
|
|
|
if apt_get_path:
|
2015-12-10 00:20:13 +01:00
|
|
|
rc, so, se = module.run_command([apt_get_path, 'update'])
|
|
|
|
if rc != 0:
|
|
|
|
module.fail_json(msg="Failed to auto-install python-apt. Error was: '%s'" % se.strip())
|
|
|
|
rc, so, se = module.run_command([apt_get_path, 'install', 'python-apt', '-y', '-q'])
|
2014-03-12 12:15:27 +01:00
|
|
|
if rc == 0:
|
2014-05-06 15:43:37 +02:00
|
|
|
global apt, apt_pkg, aptsources_distro, distro, HAVE_PYTHON_APT
|
2014-03-12 12:15:27 +01:00
|
|
|
import apt
|
|
|
|
import apt_pkg
|
2014-05-06 15:43:37 +02:00
|
|
|
import aptsources.distro as aptsources_distro
|
|
|
|
distro = aptsources_distro.get_distro()
|
|
|
|
HAVE_PYTHON_APT = True
|
|
|
|
else:
|
|
|
|
module.fail_json(msg="Failed to auto-install python-apt. Error was: '%s'" % se.strip())
|
2016-01-31 10:32:09 +01:00
|
|
|
else:
|
|
|
|
module.fail_json(msg="python-apt must be installed to use check mode")
|
2013-08-13 19:25:31 +02:00
|
|
|
|
2013-08-07 21:58:50 +02:00
|
|
|
class InvalidSource(Exception):
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
|
|
|
# Simple version of aptsources.sourceslist.SourcesList.
|
|
|
|
# No advanced logic and no backups inside.
|
|
|
|
class SourcesList(object):
|
2015-07-30 09:36:50 +02:00
|
|
|
def __init__(self, module):
|
|
|
|
self.module = module
|
2013-08-07 21:58:50 +02:00
|
|
|
self.files = {} # group sources by file
|
2015-06-25 01:32:47 +02:00
|
|
|
# Repositories that we're adding -- used to implement mode param
|
|
|
|
self.new_repos = set()
|
2014-02-02 19:49:23 +01:00
|
|
|
self.default_file = self._apt_cfg_file('Dir::Etc::sourcelist')
|
2013-08-07 21:58:50 +02:00
|
|
|
|
2014-01-02 15:41:24 +01:00
|
|
|
# read sources.list if it exists
|
|
|
|
if os.path.isfile(self.default_file):
|
|
|
|
self.load(self.default_file)
|
2013-08-07 21:58:50 +02:00
|
|
|
|
|
|
|
# read sources.list.d
|
2014-02-02 19:49:23 +01:00
|
|
|
for file in glob.iglob('%s/*.list' % self._apt_cfg_dir('Dir::Etc::sourceparts')):
|
2013-08-07 21:58:50 +02:00
|
|
|
self.load(file)
|
|
|
|
|
|
|
|
def __iter__(self):
|
|
|
|
'''Simple iterator to go over all sources. Empty, non-source, and other not valid lines will be skipped.'''
|
|
|
|
for file, sources in self.files.items():
|
|
|
|
for n, valid, enabled, source, comment in sources:
|
|
|
|
if valid:
|
|
|
|
yield file, n, enabled, source, comment
|
|
|
|
raise StopIteration
|
|
|
|
|
|
|
|
def _expand_path(self, filename):
|
|
|
|
if '/' in filename:
|
|
|
|
return filename
|
|
|
|
else:
|
2014-02-02 19:49:23 +01:00
|
|
|
return os.path.abspath(os.path.join(self._apt_cfg_dir('Dir::Etc::sourceparts'), filename))
|
2013-08-07 21:58:50 +02:00
|
|
|
|
|
|
|
def _suggest_filename(self, line):
|
|
|
|
def _cleanup_filename(s):
|
2015-12-16 08:48:33 +01:00
|
|
|
filename = self.module.params['filename']
|
|
|
|
if filename is not None:
|
|
|
|
return filename
|
2013-08-07 21:58:50 +02:00
|
|
|
return '_'.join(re.sub('[^a-zA-Z0-9]', ' ', s).split())
|
2014-04-14 19:18:02 +02:00
|
|
|
def _strip_username_password(s):
|
|
|
|
if '@' in s:
|
|
|
|
s = s.split('@', 1)
|
|
|
|
s = s[-1]
|
|
|
|
return s
|
2013-08-07 21:58:50 +02:00
|
|
|
|
2013-10-03 01:31:04 +02:00
|
|
|
# Drop options and protocols.
|
|
|
|
line = re.sub('\[[^\]]+\]', '', line)
|
|
|
|
line = re.sub('\w+://', '', line)
|
|
|
|
|
2014-04-14 19:18:02 +02:00
|
|
|
# split line into valid keywords
|
2013-10-03 01:31:04 +02:00
|
|
|
parts = [part for part in line.split() if part not in VALID_SOURCE_TYPES]
|
2014-04-14 19:18:02 +02:00
|
|
|
|
|
|
|
# Drop usernames and passwords
|
|
|
|
parts[0] = _strip_username_password(parts[0])
|
|
|
|
|
2013-08-07 21:58:50 +02:00
|
|
|
return '%s.list' % _cleanup_filename(' '.join(parts[:1]))
|
|
|
|
|
|
|
|
def _parse(self, line, raise_if_invalid_or_disabled=False):
|
|
|
|
valid = False
|
|
|
|
enabled = True
|
|
|
|
source = ''
|
|
|
|
comment = ''
|
|
|
|
|
|
|
|
line = line.strip()
|
|
|
|
if line.startswith('#'):
|
|
|
|
enabled = False
|
|
|
|
line = line[1:]
|
|
|
|
|
|
|
|
# Check for another "#" in the line and treat a part after it as a comment.
|
|
|
|
i = line.find('#')
|
|
|
|
if i > 0:
|
|
|
|
comment = line[i+1:].strip()
|
|
|
|
line = line[:i]
|
|
|
|
|
|
|
|
# Split a source into substring to make sure that it is source spec.
|
|
|
|
# Duplicated whitespaces in a valid source spec will be removed.
|
|
|
|
source = line.strip()
|
|
|
|
if source:
|
|
|
|
chunks = source.split()
|
|
|
|
if chunks[0] in VALID_SOURCE_TYPES:
|
|
|
|
valid = True
|
|
|
|
source = ' '.join(chunks)
|
|
|
|
|
|
|
|
if raise_if_invalid_or_disabled and (not valid or not enabled):
|
|
|
|
raise InvalidSource(line)
|
|
|
|
|
|
|
|
return valid, enabled, source, comment
|
|
|
|
|
2014-02-02 19:49:23 +01:00
|
|
|
@staticmethod
|
|
|
|
def _apt_cfg_file(filespec):
|
|
|
|
'''
|
|
|
|
Wrapper for `apt_pkg` module for running with Python 2.5
|
|
|
|
'''
|
|
|
|
try:
|
|
|
|
result = apt_pkg.config.find_file(filespec)
|
|
|
|
except AttributeError:
|
|
|
|
result = apt_pkg.Config.FindFile(filespec)
|
|
|
|
return result
|
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
def _apt_cfg_dir(dirspec):
|
|
|
|
'''
|
|
|
|
Wrapper for `apt_pkg` module for running with Python 2.5
|
|
|
|
'''
|
|
|
|
try:
|
|
|
|
result = apt_pkg.config.find_dir(dirspec)
|
|
|
|
except AttributeError:
|
|
|
|
result = apt_pkg.Config.FindDir(dirspec)
|
|
|
|
return result
|
|
|
|
|
2013-08-07 21:58:50 +02:00
|
|
|
def load(self, file):
|
|
|
|
group = []
|
2014-02-03 05:34:28 +01:00
|
|
|
f = open(file, 'r')
|
|
|
|
for n, line in enumerate(f):
|
|
|
|
valid, enabled, source, comment = self._parse(line)
|
|
|
|
group.append((n, valid, enabled, source, comment))
|
2013-08-07 21:58:50 +02:00
|
|
|
self.files[file] = group
|
|
|
|
|
2015-07-30 09:36:50 +02:00
|
|
|
def save(self):
|
2013-08-07 21:58:50 +02:00
|
|
|
for filename, sources in self.files.items():
|
|
|
|
if sources:
|
|
|
|
d, fn = os.path.split(filename)
|
|
|
|
fd, tmp_path = tempfile.mkstemp(prefix=".%s-" % fn, dir=d)
|
2014-04-14 22:42:23 +02:00
|
|
|
|
2014-02-03 05:34:28 +01:00
|
|
|
f = os.fdopen(fd, 'w')
|
|
|
|
for n, valid, enabled, source, comment in sources:
|
|
|
|
chunks = []
|
|
|
|
if not enabled:
|
|
|
|
chunks.append('# ')
|
|
|
|
chunks.append(source)
|
|
|
|
if comment:
|
|
|
|
chunks.append(' # ')
|
|
|
|
chunks.append(comment)
|
|
|
|
chunks.append('\n')
|
|
|
|
line = ''.join(chunks)
|
|
|
|
|
|
|
|
try:
|
|
|
|
f.write(line)
|
|
|
|
except IOError, err:
|
2015-07-30 09:36:50 +02:00
|
|
|
self.module.fail_json(msg="Failed to write to file %s: %s" % (tmp_path, unicode(err)))
|
|
|
|
self.module.atomic_move(tmp_path, filename)
|
2014-11-16 01:13:29 +01:00
|
|
|
|
|
|
|
# allow the user to override the default mode
|
2015-06-25 01:32:47 +02:00
|
|
|
if filename in self.new_repos:
|
2015-07-30 09:36:50 +02:00
|
|
|
this_mode = self.module.params['mode']
|
|
|
|
self.module.set_mode_if_different(filename, this_mode, False)
|
2013-08-07 21:58:50 +02:00
|
|
|
else:
|
|
|
|
del self.files[filename]
|
|
|
|
if os.path.exists(filename):
|
|
|
|
os.remove(filename)
|
2012-08-07 22:39:31 +02:00
|
|
|
|
2013-08-07 21:58:50 +02:00
|
|
|
def dump(self):
|
|
|
|
return '\n'.join([str(i) for i in self])
|
2013-01-24 07:02:41 +01:00
|
|
|
|
2015-05-08 23:07:59 +02:00
|
|
|
def _choice(self, new, old):
|
|
|
|
if new is None:
|
|
|
|
return old
|
|
|
|
return new
|
|
|
|
|
2013-08-07 21:58:50 +02:00
|
|
|
def modify(self, file, n, enabled=None, source=None, comment=None):
|
|
|
|
'''
|
|
|
|
This function to be used with iterator, so we don't care of invalid sources.
|
|
|
|
If source, enabled, or comment is None, original value from line ``n`` will be preserved.
|
|
|
|
'''
|
|
|
|
valid, enabled_old, source_old, comment_old = self.files[file][n][1:]
|
2015-05-08 23:07:59 +02:00
|
|
|
self.files[file][n] = (n, valid, self._choice(enabled, enabled_old), self._choice(source, source_old), self._choice(comment, comment_old))
|
2013-08-07 21:58:50 +02:00
|
|
|
|
|
|
|
def _add_valid_source(self, source_new, comment_new, file):
|
|
|
|
# We'll try to reuse disabled source if we have it.
|
|
|
|
# If we have more than one entry, we will enable them all - no advanced logic, remember.
|
|
|
|
found = False
|
|
|
|
for filename, n, enabled, source, comment in self:
|
|
|
|
if source == source_new:
|
|
|
|
self.modify(filename, n, enabled=True)
|
|
|
|
found = True
|
|
|
|
|
|
|
|
if not found:
|
|
|
|
if file is None:
|
|
|
|
file = self.default_file
|
|
|
|
else:
|
|
|
|
file = self._expand_path(file)
|
|
|
|
|
|
|
|
if file not in self.files:
|
|
|
|
self.files[file] = []
|
|
|
|
|
|
|
|
files = self.files[file]
|
|
|
|
files.append((len(files), True, True, source_new, comment_new))
|
2015-06-25 01:32:47 +02:00
|
|
|
self.new_repos.add(file)
|
2013-08-07 21:58:50 +02:00
|
|
|
|
|
|
|
def add_source(self, line, comment='', file=None):
|
|
|
|
source = self._parse(line, raise_if_invalid_or_disabled=True)[2]
|
|
|
|
|
|
|
|
# Prefer separate files for new sources.
|
|
|
|
self._add_valid_source(source, comment, file=file or self._suggest_filename(source))
|
|
|
|
|
|
|
|
def _remove_valid_source(self, source):
|
|
|
|
# If we have more than one entry, we will remove them all (not comment, remove!)
|
|
|
|
for filename, n, enabled, src, comment in self:
|
|
|
|
if source == src and enabled:
|
|
|
|
self.files[filename].pop(n)
|
|
|
|
|
|
|
|
def remove_source(self, line):
|
|
|
|
source = self._parse(line, raise_if_invalid_or_disabled=True)[2]
|
|
|
|
self._remove_valid_source(source)
|
|
|
|
|
|
|
|
|
|
|
|
class UbuntuSourcesList(SourcesList):
|
2013-08-13 19:25:31 +02:00
|
|
|
|
2014-03-17 17:31:04 +01:00
|
|
|
LP_API = 'https://launchpad.net/api/1.0/~%s/+archive/%s'
|
2013-08-13 19:25:31 +02:00
|
|
|
|
2014-03-17 17:31:04 +01:00
|
|
|
def __init__(self, module, add_ppa_signing_keys_callback=None):
|
|
|
|
self.module = module
|
2013-08-07 21:58:50 +02:00
|
|
|
self.add_ppa_signing_keys_callback = add_ppa_signing_keys_callback
|
2015-07-30 09:36:50 +02:00
|
|
|
super(UbuntuSourcesList, self).__init__(module)
|
2013-08-07 21:58:50 +02:00
|
|
|
|
|
|
|
def _get_ppa_info(self, owner_name, ppa_name):
|
2013-08-13 19:25:31 +02:00
|
|
|
lp_api = self.LP_API % (owner_name, ppa_name)
|
2014-03-17 17:31:04 +01:00
|
|
|
|
|
|
|
headers = dict(Accept='application/json')
|
|
|
|
response, info = fetch_url(self.module, lp_api, headers=headers)
|
2014-05-20 06:04:13 +02:00
|
|
|
if info['status'] != 200:
|
|
|
|
self.module.fail_json(msg="failed to fetch PPA information, error was: %s" % info['msg'])
|
2014-03-17 17:31:04 +01:00
|
|
|
return json.load(response)
|
2013-08-07 21:58:50 +02:00
|
|
|
|
|
|
|
def _expand_ppa(self, path):
|
|
|
|
ppa = path.split(':')[1]
|
|
|
|
ppa_owner = ppa.split('/')[0]
|
|
|
|
try:
|
|
|
|
ppa_name = ppa.split('/')[1]
|
|
|
|
except IndexError:
|
|
|
|
ppa_name = 'ppa'
|
|
|
|
|
|
|
|
line = 'deb http://ppa.launchpad.net/%s/%s/ubuntu %s main' % (ppa_owner, ppa_name, distro.codename)
|
|
|
|
return line, ppa_owner, ppa_name
|
|
|
|
|
2014-08-11 11:57:23 +02:00
|
|
|
def _key_already_exists(self, key_fingerprint):
|
|
|
|
rc, out, err = self.module.run_command('apt-key export %s' % key_fingerprint, check_rc=True)
|
|
|
|
return len(err) == 0
|
|
|
|
|
2013-08-07 21:58:50 +02:00
|
|
|
def add_source(self, line, comment='', file=None):
|
|
|
|
if line.startswith('ppa:'):
|
|
|
|
source, ppa_owner, ppa_name = self._expand_ppa(line)
|
|
|
|
|
2015-07-29 17:49:37 +02:00
|
|
|
if source in self.repos_urls:
|
|
|
|
# repository already exists
|
|
|
|
return
|
|
|
|
|
2013-08-07 21:58:50 +02:00
|
|
|
if self.add_ppa_signing_keys_callback is not None:
|
|
|
|
info = self._get_ppa_info(ppa_owner, ppa_name)
|
2014-08-11 11:57:23 +02:00
|
|
|
if not self._key_already_exists(info['signing_key_fingerprint']):
|
|
|
|
command = ['apt-key', 'adv', '--recv-keys', '--keyserver', 'hkp://keyserver.ubuntu.com:80', info['signing_key_fingerprint']]
|
|
|
|
self.add_ppa_signing_keys_callback(command)
|
2013-08-07 21:58:50 +02:00
|
|
|
|
|
|
|
file = file or self._suggest_filename('%s_%s' % (line, distro.codename))
|
|
|
|
else:
|
|
|
|
source = self._parse(line, raise_if_invalid_or_disabled=True)[2]
|
|
|
|
file = file or self._suggest_filename(source)
|
|
|
|
self._add_valid_source(source, comment, file)
|
|
|
|
|
|
|
|
def remove_source(self, line):
|
|
|
|
if line.startswith('ppa:'):
|
|
|
|
source = self._expand_ppa(line)[0]
|
|
|
|
else:
|
|
|
|
source = self._parse(line, raise_if_invalid_or_disabled=True)[2]
|
|
|
|
self._remove_valid_source(source)
|
|
|
|
|
2015-06-25 19:56:29 +02:00
|
|
|
@property
|
|
|
|
def repos_urls(self):
|
|
|
|
_repositories = []
|
|
|
|
for parsed_repos in self.files.values():
|
|
|
|
for parsed_repo in parsed_repos:
|
|
|
|
enabled = parsed_repo[1]
|
|
|
|
source_line = parsed_repo[3]
|
|
|
|
|
|
|
|
if not enabled:
|
|
|
|
continue
|
|
|
|
|
|
|
|
if source_line.startswith('ppa:'):
|
2015-07-30 05:32:18 +02:00
|
|
|
source, ppa_owner, ppa_name = self._expand_ppa(source_line)
|
2015-06-25 19:56:29 +02:00
|
|
|
_repositories.append(source)
|
|
|
|
else:
|
|
|
|
_repositories.append(source_line)
|
|
|
|
|
|
|
|
return _repositories
|
|
|
|
|
2013-08-07 21:58:50 +02:00
|
|
|
|
|
|
|
def get_add_ppa_signing_key_callback(module):
|
|
|
|
def _run_command(command):
|
|
|
|
module.run_command(command, check_rc=True)
|
|
|
|
|
2014-03-12 21:44:03 +01:00
|
|
|
if module.check_mode:
|
|
|
|
return None
|
2014-03-14 15:24:52 +01:00
|
|
|
else:
|
|
|
|
return _run_command
|
2013-08-07 21:58:50 +02:00
|
|
|
|
|
|
|
|
|
|
|
def main():
|
|
|
|
module = AnsibleModule(
|
|
|
|
argument_spec=dict(
|
|
|
|
repo=dict(required=True),
|
|
|
|
state=dict(choices=['present', 'absent'], default='present'),
|
2014-04-14 22:42:23 +02:00
|
|
|
mode=dict(required=False, default=0644),
|
2013-10-10 23:18:06 +02:00
|
|
|
update_cache = dict(aliases=['update-cache'], type='bool', default='yes'),
|
2015-12-16 08:48:33 +01:00
|
|
|
filename=dict(required=False, default=None),
|
2014-03-12 12:15:27 +01:00
|
|
|
# this should not be needed, but exists as a failsafe
|
|
|
|
install_python_apt=dict(required=False, default="yes", type='bool'),
|
2014-04-28 19:48:52 +02:00
|
|
|
validate_certs = dict(default='yes', type='bool'),
|
2013-08-07 21:58:50 +02:00
|
|
|
),
|
|
|
|
supports_check_mode=True,
|
|
|
|
)
|
|
|
|
|
2014-03-12 12:15:27 +01:00
|
|
|
params = module.params
|
2012-08-07 22:39:31 +02:00
|
|
|
repo = module.params['repo']
|
|
|
|
state = module.params['state']
|
2013-09-18 19:14:08 +02:00
|
|
|
update_cache = module.params['update_cache']
|
2013-08-07 21:58:50 +02:00
|
|
|
sourceslist = None
|
2012-08-07 22:39:31 +02:00
|
|
|
|
2015-07-30 07:15:23 +02:00
|
|
|
if not HAVE_PYTHON_APT:
|
|
|
|
if params['install_python_apt']:
|
|
|
|
install_python_apt(module)
|
|
|
|
else:
|
|
|
|
module.fail_json(msg='python-apt is not installed, and install_python_apt is False')
|
|
|
|
|
|
|
|
if isinstance(distro, aptsources_distro.UbuntuDistribution):
|
|
|
|
sourceslist = UbuntuSourcesList(module,
|
|
|
|
add_ppa_signing_keys_callback=get_add_ppa_signing_key_callback(module))
|
2015-07-30 09:46:11 +02:00
|
|
|
elif isinstance(distro, aptsources_distro.Distribution):
|
2015-07-30 09:36:50 +02:00
|
|
|
sourceslist = SourcesList(module)
|
2013-01-24 07:02:41 +01:00
|
|
|
else:
|
2015-07-30 07:15:23 +02:00
|
|
|
module.fail_json(msg='Module apt_repository supports only Debian and Ubuntu.')
|
2012-08-07 22:39:31 +02:00
|
|
|
|
2013-08-07 21:58:50 +02:00
|
|
|
sources_before = sourceslist.dump()
|
2012-08-07 22:39:31 +02:00
|
|
|
|
2013-08-07 21:58:50 +02:00
|
|
|
try:
|
2015-07-29 17:49:37 +02:00
|
|
|
if state == 'present':
|
2013-08-07 21:58:50 +02:00
|
|
|
sourceslist.add_source(repo)
|
|
|
|
elif state == 'absent':
|
|
|
|
sourceslist.remove_source(repo)
|
2014-02-02 18:33:27 +01:00
|
|
|
except InvalidSource, err:
|
2013-08-07 21:58:50 +02:00
|
|
|
module.fail_json(msg='Invalid repository string: %s' % unicode(err))
|
2012-08-07 22:39:31 +02:00
|
|
|
|
2013-08-07 21:58:50 +02:00
|
|
|
sources_after = sourceslist.dump()
|
|
|
|
changed = sources_before != sources_after
|
|
|
|
|
|
|
|
if not module.check_mode and changed:
|
|
|
|
try:
|
2015-07-30 09:36:50 +02:00
|
|
|
sourceslist.save()
|
2013-09-18 19:14:08 +02:00
|
|
|
if update_cache:
|
|
|
|
cache = apt.Cache()
|
|
|
|
cache.update()
|
2014-02-02 18:33:27 +01:00
|
|
|
except OSError, err:
|
2013-08-07 21:58:50 +02:00
|
|
|
module.fail_json(msg=unicode(err))
|
2012-08-07 22:39:31 +02:00
|
|
|
|
2013-08-07 21:58:50 +02:00
|
|
|
module.exit_json(changed=changed, repo=repo, state=state)
|
2013-01-24 07:02:41 +01:00
|
|
|
|
2013-12-02 21:13:49 +01:00
|
|
|
# import module snippets
|
2013-12-02 21:11:23 +01:00
|
|
|
from ansible.module_utils.basic import *
|
2014-03-17 17:31:04 +01:00
|
|
|
from ansible.module_utils.urls import *
|
2012-08-07 22:39:31 +02:00
|
|
|
|
|
|
|
main()
|