2012-08-07 20:39:31 +00:00
#!/usr/bin/python
2012-08-08 14:46:23 +00:00
# -*- coding: utf-8 -*-
# (c) 2012, Matt Wright <matt@nobien.net>
#
# 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/>.
#
# Example:
# - name: add nginx repo
# action: apt_repository repo=ppa:nginx/stable state=present
#
2012-08-07 20:39:31 +00:00
2012-09-28 21:07:41 +00:00
DOCUMENTATION = '''
---
module: apt_repository
2012-09-28 23:51:55 +00:00
short_description: Manages apt repositores
2012-09-28 21:07:41 +00:00
description:
2012-11-21 17:49:30 +00:00
- Manages apt repositories (such as for Debian/Ubuntu).
2012-09-28 21:07:41 +00:00
version_added: "0.7"
options:
repo:
description:
- The repository name/value
required: true
default: null
state:
description:
- The repository state
required: false
default: present
choices: [ "present", "absent" ]
notes:
2012-11-21 17:49:30 +00:00
- This module works on Debian and Ubuntu only and requires C(apt-add-repository) be available on the destination server. To ensure this package is available use the M(apt) module and install the C(python-software-properties) package before using this module.
2012-10-16 22:17:44 +00:00
- This module cannot be used on Debian Squeeze (Version 6) as there is no C(add-apt-repository) in C(python-software-properties)
2012-10-03 02:32:17 +00:00
- A bug in C(apt-add-repository) always adds C(deb) and C(deb-src) types for repositories (see the issue on Launchpad U(https://bugs.launchpad.net/ubuntu/+source/software-properties/+bug/987264)), if a repo doesn't have source information (eg MongoDB repo from 10gen) the system will fail while updating repositories.
2012-09-28 21:07:41 +00:00
author: Matt Wright
2012-09-28 23:51:55 +00:00
examples:
2012-12-12 04:01:36 +00:00
- code: "apt_repository: repo=ppa:nginx/stable"
2012-09-28 21:07:41 +00:00
description: Add nginx stable repository from PPA
2012-10-23 13:14:01 +00:00
- code: "apt_repository: repo='deb http://archive.canonical.com/ubuntu hardy partner'"
2012-09-28 21:07:41 +00:00
description: Add specified repository into sources.
2013-01-24 06:02:41 +00:00
requirements: [ python-apt ]
2012-09-28 23:51:55 +00:00
'''
2012-09-28 21:07:41 +00:00
2012-08-07 20:39:31 +00:00
import platform
2013-01-24 06:02:41 +00:00
try:
import apt
import apt_pkg
HAVE_PYAPT = True
except ImportError:
HAVE_PYAPT = False
2012-08-07 20:39:31 +00:00
APT = "/usr/bin/apt-get"
2012-08-21 14:36:38 +00:00
ADD_APT_REPO = 'add-apt-repository'
2012-08-07 20:39:31 +00:00
Update modules to use run_command in module_common.py
This updates apt, apt_repository, command, cron, easy_install, facter,
fireball, git, group, mount, ohai, pip, service, setup, subversion,
supervisorctl, svr4pkg, user, and yum to take advantage of run_command
in module_common.py.
2013-01-12 06:10:21 +00:00
def check_cmd_needs_y():
2012-08-30 16:35:37 +00:00
if platform.dist()[0] == 'debian' or float(platform.dist()[1]) >= 11.10:
Update modules to use run_command in module_common.py
This updates apt, apt_repository, command, cron, easy_install, facter,
fireball, git, group, mount, ohai, pip, service, setup, subversion,
supervisorctl, svr4pkg, user, and yum to take advantage of run_command
in module_common.py.
2013-01-12 06:10:21 +00:00
return True
return False
2012-08-07 20:39:31 +00:00
2013-01-24 06:02:41 +00:00
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
2012-08-07 20:39:31 +00:00
def main():
2012-08-13 17:06:53 +00:00
add_apt_repository = None
2012-08-07 20:39:31 +00:00
arg_spec = dict(
repo=dict(required=True),
state=dict(default='present', choices=['present', 'absent'])
)
2013-02-27 20:23:35 +00:00
module = AnsibleModule(argument_spec=arg_spec, supports_check_mode=True)
2012-08-07 20:39:31 +00:00
2013-01-24 06:02:41 +00:00
if not HAVE_PYAPT:
module.fail_json(msg="Could not import python modules: apt, apt_pkg. Please install python-apt package.")
2012-08-30 17:31:23 +00:00
add_apt_repository = module.get_bin_path(ADD_APT_REPO, True)
Update modules to use run_command in module_common.py
This updates apt, apt_repository, command, cron, easy_install, facter,
fireball, git, group, mount, ohai, pip, service, setup, subversion,
supervisorctl, svr4pkg, user, and yum to take advantage of run_command
in module_common.py.
2013-01-12 06:10:21 +00:00
if check_cmd_needs_y():
add_apt_repository += ' -y'
2012-08-07 20:39:31 +00:00
repo = module.params['repo']
state = module.params['state']
2013-01-24 06:02:41 +00:00
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:
2013-02-27 20:23:35 +00:00
if module.check_mode:
module.exit_json(changed=True)
2013-01-24 06:02:41 +00:00
cmd = '%s "%s" --remove' % (add_apt_repository, repo)
rc, out, err = module.run_command(cmd)
elif state == 'present' and not exists:
2013-02-27 20:23:35 +00:00
if module.check_mode:
module.exit_json(changed=True)
2013-01-24 06:02:41 +00:00
cmd = '%s "%s"' % (add_apt_repository, repo)
rc, out, err = module.run_command(cmd)
else:
module.exit_json(changed=False, repo=repo, state=state)
2012-08-07 20:39:31 +00:00
if rc != 0:
module.fail_json(msg=err)
2013-01-24 06:02:41 +00:00
else:
changed = True
2012-08-07 20:39:31 +00:00
2013-01-24 06:02:41 +00:00
if state == 'present' and changed:
Update modules to use run_command in module_common.py
This updates apt, apt_repository, command, cron, easy_install, facter,
fireball, git, group, mount, ohai, pip, service, setup, subversion,
supervisorctl, svr4pkg, user, and yum to take advantage of run_command
in module_common.py.
2013-01-12 06:10:21 +00:00
rc, out, err = module.run_command('%s update' % APT)
2012-08-07 20:39:31 +00:00
2012-08-08 14:46:23 +00:00
module.exit_json(changed=changed, repo=repo, state=state)
2012-08-07 20:39:31 +00:00
2013-01-24 06:02:41 +00:00
2012-08-07 20:39:31 +00:00
# this is magic, see lib/ansible/module_common.py
#<<INCLUDE_ANSIBLE_MODULE_COMMON>>
main()