2012-08-07 22:39:31 +02:00
|
|
|
#!/usr/bin/python
|
2012-08-08 16:46:23 +02: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 22:39:31 +02:00
|
|
|
|
|
|
|
import platform
|
|
|
|
|
|
|
|
APT = "/usr/bin/apt-get"
|
2012-08-21 16:36:38 +02:00
|
|
|
ADD_APT_REPO = 'add-apt-repository'
|
2012-08-07 22:39:31 +02:00
|
|
|
|
|
|
|
def _run(cmd):
|
2012-08-30 18:35:37 +02:00
|
|
|
if platform.dist()[0] == 'debian' or float(platform.dist()[1]) >= 11.10:
|
|
|
|
cmd = cmd + ' -y'
|
2012-08-07 22:39:31 +02:00
|
|
|
# returns (rc, stdout, stderr) from shell command
|
|
|
|
process = subprocess.Popen(cmd, stdout=subprocess.PIPE,
|
|
|
|
stderr=subprocess.PIPE, shell=True)
|
|
|
|
stdout, stderr = process.communicate()
|
|
|
|
return (process.returncode, stdout, stderr)
|
|
|
|
|
|
|
|
|
|
|
|
def main():
|
2012-08-13 19:06:53 +02:00
|
|
|
add_apt_repository = None
|
|
|
|
|
2012-08-07 22:39:31 +02:00
|
|
|
arg_spec = dict(
|
|
|
|
repo=dict(required=True),
|
|
|
|
state=dict(default='present', choices=['present', 'absent'])
|
|
|
|
)
|
|
|
|
|
|
|
|
module = AnsibleModule(argument_spec=arg_spec)
|
|
|
|
|
2012-08-30 19:31:23 +02:00
|
|
|
add_apt_repository = module.get_bin_path(ADD_APT_REPO, True)
|
2012-08-07 22:39:31 +02:00
|
|
|
|
|
|
|
repo = module.params['repo']
|
|
|
|
state = module.params['state']
|
|
|
|
|
2012-08-13 19:36:08 +02:00
|
|
|
rc, out, err = _run('%s "%s" --remove' % (add_apt_repository, repo))
|
2012-08-07 22:39:31 +02:00
|
|
|
existed = 'Error' not in out
|
|
|
|
|
|
|
|
if state == 'absent':
|
|
|
|
if not existed:
|
2012-08-08 16:46:23 +02:00
|
|
|
module.exit_json(changed=False, repo=repo, state=state)
|
2012-08-07 22:39:31 +02:00
|
|
|
else:
|
2012-08-08 16:46:23 +02:00
|
|
|
module.exit_json(changed=True, repo=repo, state=state)
|
2012-08-07 22:39:31 +02:00
|
|
|
|
2012-08-13 19:36:08 +02:00
|
|
|
cmd = '%s "%s"' % (add_apt_repository, repo)
|
2012-08-07 22:39:31 +02:00
|
|
|
|
|
|
|
rc, out, err = _run(cmd)
|
|
|
|
|
|
|
|
changed = rc == 0 and not existed
|
|
|
|
|
|
|
|
if rc != 0:
|
|
|
|
module.fail_json(msg=err)
|
|
|
|
|
|
|
|
if changed:
|
2012-08-08 16:46:23 +02:00
|
|
|
rc, out, err = _run('%s update' % APT)
|
2012-08-07 22:39:31 +02:00
|
|
|
|
2012-08-08 16:46:23 +02:00
|
|
|
module.exit_json(changed=changed, repo=repo, state=state)
|
2012-08-07 22:39:31 +02:00
|
|
|
|
|
|
|
# this is magic, see lib/ansible/module_common.py
|
|
|
|
#<<INCLUDE_ANSIBLE_MODULE_COMMON>>
|
|
|
|
|
|
|
|
main()
|