Created
July 8, 2015 13:57
-
-
Save juliedavila/2d3930692eeece5cc08f to your computer and use it in GitHub Desktop.
Filter to be able to specify a url for the ansible template module for Ansible 2.*
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from __future__ import (absolute_import, division, print_function) | |
__metaclass__ = type | |
import urllib2 | |
import tempfile | |
import os | |
from ansible.errors import AnsibleError | |
from ansible.plugins.lookup import LookupBase | |
from ansible.module_utils.urls import open_url, ConnectionError, SSLValidationError | |
from ansible.utils.unicode import to_unicode | |
from glob import glob | |
class LookupModule(LookupBase): | |
def run(self, terms, variables=None, **kwargs): | |
if isinstance(terms, basestring): | |
terms = [ terms ] | |
validate_certs = kwargs.get('validate_certs', True) | |
f_handle, templ_path = tempfile.mkstemp(prefix='ansible_template', dir='/tmp/') | |
temp_file = open(templ_path, 'w+r') | |
ret = [] | |
for term in terms: | |
try: | |
response = open_url(term, validate_certs=validate_certs) | |
except urllib2.URLError as e: | |
raise AnsibleError("Failed lookup url for %s : %s" % (term, str(e))) | |
except urllib2.HTTPError as e: | |
raise AnsibleError("Received HTTP error for %s : %s" % (term, str(e))) | |
except SSLValidationError as e: | |
raise AnsibleError("Error validating the server's certificate for %s: %s" % (term, str(e))) | |
except ConnectionError as e: | |
raise AnsibleError("Error connecting to %s: %s" % (term, str(e))) | |
for line in response.read().splitlines(): | |
temp_file.write(line) | |
ret.append(templ_path) | |
return ret | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment