Skip to content

Instantly share code, notes, and snippets.

@pixie79
Created July 7, 2014 06:12
Show Gist options
  • Save pixie79/968cb375fe2ec072476a to your computer and use it in GitHub Desktop.
Save pixie79/968cb375fe2ec072476a to your computer and use it in GitHub Desktop.
# -*- coding: utf-8 -*-
# -*- mode: python -*-
DOCUMENTATION = '''
---
module: gce_meta
short_description: Google Compute Engine Meta Service Lookup.
description:
- The service returns meta information found from the Google Meta service
about the GCE Instance on which it is running for the meta key given.
options:
gcemeta:
description:
- The gcemeta key to lookup.
required: True
default: None
author: Mark Olliver
'''
EXAMPLES = '''
# Ideally this should be run as a pretask to populate a variable for later
# use, this task looks up a key called 'dc'
pre_tasks:
- gce_meta: gcemeta=dc
'''
import urllib2
import sys
import json
import shlex
import os
def get_meta(module):
if module.params.get('gcemeta') is None:
return '{ "ansible_facts": '' }'
metadata = module.params.get('gcemeta').split(",")
url = "http://metadata.google.internal"
path = "/computeMetadata/v1/instance/attributes/"
headers = {"Metadata-Flavor": "Google"}
content = {}
for x in range(0, len(metadata)):
req = urllib2.Request(url + path + 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