From e1bbfa6210762c1819446027913ddfc13254e03d Mon Sep 17 00:00:00 2001 From: Lorin Hochstein Date: Sat, 9 Nov 2013 14:56:02 -0500 Subject: [PATCH] Genericize module to support multiple distros Make the module implementatino more generic to support distributions other than Ubuntu in the future. Adds distro as a new parameter. --- cloud/ec2_ami_search | 66 +++++++++++++++++++++++++++----------------- 1 file changed, 41 insertions(+), 25 deletions(-) diff --git a/cloud/ec2_ami_search b/cloud/ec2_ami_search index 062971ab25f..0bb3fab0078 100644 --- a/cloud/ec2_ami_search +++ b/cloud/ec2_ami_search @@ -18,16 +18,18 @@ # along with Ansible. If not, see . DOCUMENTATION = ''' --- -module: ec2_ubuntu_ami -short_description: Retrieve AWS AMIs for official Ubuntu images +module: ec2_ami_search +short_description: Retrieve AWS AMI for a given operating system. description: - - The Ubuntu project maintains a list of the latest version of Ubuntu images on EC2 accessible via http. - - This module retrieves the AMI for a given Ubuntu release by making an http query against the appropriate cloud-images.ubuntu.com url and parsing the output. - - For example: https://cloud-images.ubuntu.com/query/precise/server/released.current.txt has information about Ubuntu 12.04 (precise pangolin) release, server edition. + - Look up the most recent AMI on AWS for a given operating system. - Returns C(ami), C(aki), C(ari), C(serial), C(tag) - If there is no AKI or ARI associated with an image, these will be C(null). - Example output: C({"ami": "ami-69f5a900", "changed": false, "aki": "aki-88aa75e1", "tag": "release", "ari": null, "serial": "20131024"}) options: + distro: + description: Linux distribution (e.g., C(ubuntu)) + required: true + choices: ["ubuntu"] release: description: short name of the release (e.g., C(precise)) required: true @@ -67,7 +69,7 @@ EXAMPLES = ''' connection: local tasks: - name: Get the Ubuntu precise AMI - ec2_ubuntu_ami: release=precise region=us-west-1 store=instance-store + ec2_ami_search: distro=ubuntu release=precise region=us-west-1 store=instance-store register: ubuntu_image - name: Start the EC2 instance ec2: image={{ ubuntu_image.ami }} instance_type=m1.small key_name=mykey @@ -78,6 +80,8 @@ import json import urllib2 import urlparse +SUPPORTED_DISTROS = ['ubuntu'] + AWS_REGIONS = ['ap-northeast-1', 'ap-southeast-1', 'ap-southeast-2', @@ -98,9 +102,31 @@ def get_url(module, url): return r -def get_ami(table, release, stream, store, - arch, region, virt): - """ Get the Ubuntu AMI that matches query given a table of AMIs +def ubuntu(module): + """ Get the ami for ubuntu """ + + release = module.params['release'] + stream = module.params['stream'] + store = module.params['store'] + arch = module.params['arch'] + region = module.params['region'] + virt = module.params['virt'] + + url = get_ubuntu_url(release, stream) + + req = get_url(module, url) + reader = csv.reader(req, delimiter='\t') + try: + ami, aki, ari, tag, serial = lookup_ubuntu_ami(reader, release, stream, + store, arch, region, virt) + module.exit_json(changed=False, ami=ami, aki=aki, ari=ari, tag=tag, + serial=serial) + except KeyError: + module.fail_json(msg="No matching AMI found") + + +def lookup_ubuntu_ami(table, release, stream, store, arch, region, virt): + """ Look up the Ubuntu AMI that matches query given a table of AMIs table: an iterable that returns a row of (release, stream, tag, serial, region, ami, aki, ari, virt) @@ -138,6 +164,7 @@ def get_ubuntu_url(release, stream): def main(): arg_spec = dict( + distro=dict(required=True, choices=SUPPORTED_DISTROS), release=dict(required=True), stream=dict(required=False, default='server', choices=['desktop', 'server']), @@ -150,24 +177,13 @@ def main(): choices=['paravirtual', 'hvm']) ) module = AnsibleModule(argument_spec=arg_spec) - release = module.params['release'] - stream = module.params['stream'] - store = module.params['store'] - arch = module.params['arch'] - region = module.params['region'] - virt = module.params['virt'] + distro = module.params['distro'] - url = get_ubuntu_url(release, stream) + if distro == 'ubuntu': + ubuntu(module) + else: + module.fail_json(msg="Unsupported distro: %s" % distro) - req = get_url(module, url) - reader = csv.reader(req, delimiter='\t') - try: - ami, aki, ari, tag, serial = get_ami(reader, release, stream, store, - arch, region, virt) - module.exit_json(changed=False, ami=ami, aki=aki, ari=ari, tag=tag, - serial=serial) - except KeyError: - module.fail_json(msg="No matching AMI found") # this is magic, see lib/ansible/module_common.py