Created
July 1, 2014 20:00
-
-
Save pixie79/d5399bbdc9df7e68fb4e to your computer and use it in GitHub Desktop.
gce_meta module
This file contains hidden or 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
#!/usr/bin/env python | |
import urllib2 | |
import sys | |
import json | |
import shlex | |
import os | |
def get_meta(module): | |
metadata = module.params.get('gcemeta').split(",") | |
url = "http://metadata.google.internal/computeMetadata/v1/instance/attributes/" | |
headers = { "Metadata-Flavor" : "Google" } | |
content = {} | |
for x in range (0, len(metadata)): | |
req = urllib2.Request(url + metadata[x], None, headers) | |
try: | |
response = urllib2.urlopen(req) | |
content['gcemeta_' + metadata[x]] = (response.read()) | |
except urllib2.HTTPError, e: | |
pass | |
return '{ "ansible_facts": ' + json.dumps(content) + ' }' | |
def main(): | |
module = AnsibleModule( | |
argument_spec = dict( | |
gcemeta = dict() | |
) | |
) | |
out = get_meta(module) | |
module.exit_json(**json.loads(out)) | |
from ansible.module_utils.basic import * | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment