|
|
|
#!/usr/bin/python
|
|
|
|
# -*- 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
|
|
|
|
#
|
|
|
|
|
|
|
|
import platform
|
|
|
|
|
|
|
|
APT = "/usr/bin/apt-get"
|
|
|
|
|
|
|
|
|
|
|
|
def _find_binary(module):
|
|
|
|
binaries = ['/usr/bin/add-apt-repository']
|
|
|
|
|
|
|
|
for e in binaries:
|
|
|
|
if os.path.exists(e):
|
|
|
|
return e
|
|
|
|
|
|
|
|
module.fail_json(msg='Unabled to find any of the following executables '
|
|
|
|
'%s' % binaries)
|
|
|
|
|
|
|
|
def _run(cmd):
|
|
|
|
# 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():
|
|
|
|
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 = _find_binary(module)
|
|
|
|
|
|
|
|
repo = module.params['repo']
|
|
|
|
state = module.params['state']
|
|
|
|
|
|
|
|
rc, out, err = _run('%s %s --remove' % (add_apt_repository, repo))
|
|
|
|
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)
|
|
|
|
|
|
|
|
if float(platform.dist()[1]) >= 11.10:
|
|
|
|
cmd = cmd + ' -y'
|
|
|
|
|
|
|
|
rc, out, err = _run(cmd)
|
|
|
|
|
|
|
|
changed = rc == 0 and not existed
|
|
|
|
|
|
|
|
if rc != 0:
|
|
|
|
module.fail_json(msg=err)
|
|
|
|
|
|
|
|
if changed:
|
|
|
|
rc, out, err = _run('%s update' % APT)
|
|
|
|
|
|
|
|
module.exit_json(changed=changed, repo=repo, state=state)
|
|
|
|
|
|
|
|
# this is magic, see lib/ansible/module_common.py
|
|
|
|
#<<INCLUDE_ANSIBLE_MODULE_COMMON>>
|
|
|
|
|
|
|
|
main()
|