apt_repository 4.71 KB
Newer Older
Matt Wright committed
1
#!/usr/bin/python
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
# -*- 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
#
Matt Wright committed
25

26 27 28
DOCUMENTATION = '''
---
module: apt_repository
29
short_description: Manages apt repositores
30
description:
Jan-Piet Mens committed
31
  - Manages apt repositories (such as for Debian/Ubuntu).
32 33 34 35 36 37 38 39 40 41 42 43 44 45
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:
Jan-Piet Mens committed
46
   - 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.
Michael DeHaan committed
47
   - This module cannot be used on Debian Squeeze (Version 6) as there is no C(add-apt-repository) in C(python-software-properties)
48
   - 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.
49
author: Matt Wright
50
examples:
51
- code: "apt_repository: repo=ppa:nginx/stable"
52
  description: Add nginx stable repository from PPA
53
- code: "apt_repository: repo='deb http://archive.canonical.com/ubuntu hardy partner'"
54
  description: Add specified repository into sources.
55
requirements: [ python-apt ]
56
'''
57

Matt Wright committed
58 59
import platform

60 61 62 63 64 65 66
try:
    import apt
    import apt_pkg
    HAVE_PYAPT = True
except ImportError:
    HAVE_PYAPT = False

Matt Wright committed
67
APT = "/usr/bin/apt-get"
68
ADD_APT_REPO = 'add-apt-repository'
Matt Wright committed
69

70
def check_cmd_needs_y():
71
    if platform.dist()[0] == 'debian' or float(platform.dist()[1]) >= 11.10:
72 73
        return True
    return False
Matt Wright committed
74

75 76 77 78 79 80 81 82 83 84
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

Matt Wright committed
85
def main():
86 87
    add_apt_repository = None

Matt Wright committed
88 89 90 91 92
    arg_spec = dict(
        repo=dict(required=True),
        state=dict(default='present', choices=['present', 'absent'])
    )

93
    module = AnsibleModule(argument_spec=arg_spec, supports_check_mode=True)
Matt Wright committed
94

95 96 97
    if not HAVE_PYAPT:
        module.fail_json(msg="Could not import python modules: apt, apt_pkg. Please install python-apt package.")

98
    add_apt_repository = module.get_bin_path(ADD_APT_REPO, True)
99 100
    if check_cmd_needs_y():
        add_apt_repository += ' -y'
Matt Wright committed
101 102 103 104

    repo = module.params['repo']
    state = module.params['state']

105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121
    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:
122 123
        if module.check_mode:
            module.exit_json(changed=True)
124 125 126
        cmd = '%s "%s" --remove' % (add_apt_repository, repo)
        rc, out, err = module.run_command(cmd)
    elif state == 'present' and not exists:
127 128
        if module.check_mode:
            module.exit_json(changed=True)
129 130 131 132
        cmd = '%s "%s"' % (add_apt_repository, repo)
        rc, out, err = module.run_command(cmd)
    else:
        module.exit_json(changed=False, repo=repo, state=state)
Matt Wright committed
133 134 135

    if rc != 0:
        module.fail_json(msg=err)
136 137
    else:
        changed = True
Matt Wright committed
138

139
    if state == 'present' and changed:
140
        rc, out, err = module.run_command('%s update' % APT)
Matt Wright committed
141

142
    module.exit_json(changed=changed, repo=repo, state=state)
Matt Wright committed
143

144

Matt Wright committed
145 146 147 148
# this is magic, see lib/ansible/module_common.py
#<<INCLUDE_ANSIBLE_MODULE_COMMON>>

main()