#!/usr/bin/python # -*- coding: utf-8 -*- # (c) 2012, Matt Wright # # 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 . # # Example: # - name: add nginx repo # action: apt_repository repo=ppa:nginx/stable state=present # DOCUMENTATION = ''' --- module: apt_repository short_description: Manages apt repositores description: - Manages apt repositories (such as for Debian/Ubuntu). 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: - 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. - This module cannot be used on Debian Squeeze (Version 6) as there is no C(add-apt-repository) in C(python-software-properties) - 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. author: Matt Wright examples: - code: "apt_repository: repo=ppa:nginx/stable" 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. ''' import platform APT = "/usr/bin/apt-get" ADD_APT_REPO = 'add-apt-repository' def check_cmd_needs_y(): if platform.dist()[0] == 'debian' or float(platform.dist()[1]) >= 11.10: return True return False def main(): add_apt_repository = None arg_spec = dict( repo=dict(required=True), state=dict(default='present', choices=['present', 'absent']) ) module = AnsibleModule(argument_spec=arg_spec) add_apt_repository = module.get_bin_path(ADD_APT_REPO, True) if check_cmd_needs_y(): add_apt_repository += ' -y' repo = module.params['repo'] state = module.params['state'] 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) cmd = '%s "%s"' % (add_apt_repository, repo) rc, out, err = module.run_command(cmd) changed = rc == 0 and not existed if rc != 0: module.fail_json(msg=err) if 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 #<> main()