From 7ea6650d6c3d48ddaac435d76a4608f5633d1ddf Mon Sep 17 00:00:00 2001 From: Jeff Gonzalez Date: Tue, 3 Feb 2015 19:08:23 -0600 Subject: [PATCH] Refactored code to use module utility fetch_url function. --- lib/ansible/modules/system/authorized_key.py | 19 +++++++++++-------- 1 file changed, 11 insertions(+), 8 deletions(-) diff --git a/lib/ansible/modules/system/authorized_key.py b/lib/ansible/modules/system/authorized_key.py index c5d19521813..e1ac18a701d 100644 --- a/lib/ansible/modules/system/authorized_key.py +++ b/lib/ansible/modules/system/authorized_key.py @@ -122,7 +122,6 @@ import os.path import tempfile import re import shlex -import urllib2 class keydict(dict): @@ -337,19 +336,22 @@ def enforce_state(module, params): manage_dir = params.get("manage_dir", True) state = params.get("state", "present") key_options = params.get("key_options", None) + error_msg = "Error getting key from: %s" + # if the key is a url, request it and use it as key source if key.startswith("http"): - try: - gh_key = urllib2.urlopen(key).read() - except urllib2.URLError, e: - module.fail_json(msg="no key found at: %s" % key) - - key = gh_key + try: + resp, info = fetch_url(module, key) + if info['status'] != 200: + module.fail_json(msg=error_msg % key) + else: + key = resp.read() + except Exception: + module.fail_json(msg=error_msg % key) # extract individual keys into an array, skipping blank lines and comments key = [s for s in key.splitlines() if s and not s.startswith('#')] - # check current state -- just get the filename, don't create file do_write = False params["keyfile"] = keyfile(module, user, do_write, path, manage_dir) @@ -431,4 +433,5 @@ def main(): # import module snippets from ansible.module_utils.basic import * +from ansible.module_utils.urls import * main()