From 29c3099c847f35ce9527ea30ee656e9b51adb696 Mon Sep 17 00:00:00 2001 From: Matt Wright Date: Tue, 7 Aug 2012 16:39:31 -0400 Subject: [PATCH] add apt_repository module --- apt_repository | 71 ++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 71 insertions(+) create mode 100755 apt_repository diff --git a/apt_repository b/apt_repository new file mode 100755 index 00000000000..1f8591bcbb8 --- /dev/null +++ b/apt_repository @@ -0,0 +1,71 @@ +#!/usr/bin/python + +import platform + +APT = "/usr/bin/apt-get" +ADD_APT_REPOSITORY = None + + +def _find_binary(): + 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(): + arg_spec = dict( + repo=dict(required=True), + state=dict(default='present', choices=['present', 'absent']) + ) + + module = AnsibleModule(argument_spec=arg_spec) + + global ADD_APT_REPOSITORY + + ADD_APT_REPOSITORY = _find_binary() + + 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) + else: + module.exit_json(changed=True, repo=repo) + + 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: + _run('%s update' % APT) + + module.exit_json(changed=changed, existed=existed, repo=repo, cmd=cmd) + +# this is magic, see lib/ansible/module_common.py +#<> + +main()