2013-11-08 03:40:29 +01:00
|
|
|
#!/usr/bin/python
|
|
|
|
#
|
|
|
|
# (c) 2013, Nimbis Services
|
|
|
|
#
|
|
|
|
# This file is part of Ansible
|
|
|
|
#
|
|
|
|
# Ansible is free software: you can redistribute it and/or modify
|
|
|
|
# it under the terms of the GNU General Public License as published by
|
|
|
|
# the Free Software Foundation, either version 3 of the License, or
|
|
|
|
# (at your option) any later version.
|
|
|
|
#
|
|
|
|
# Ansible is distributed in the hope that it will be useful,
|
|
|
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
# GNU General Public License for more details.
|
|
|
|
#
|
|
|
|
# You should have received a copy of the GNU General Public License
|
|
|
|
# along with Ansible. If not, see <http://www.gnu.org/licenses/>.
|
2014-12-01 21:14:57 +01:00
|
|
|
|
2013-11-08 03:40:29 +01:00
|
|
|
DOCUMENTATION = '''
|
|
|
|
---
|
2013-11-09 20:56:02 +01:00
|
|
|
module: ec2_ami_search
|
2014-12-01 21:14:57 +01:00
|
|
|
short_description: Retrieve AWS AMI information for a given operating system.
|
2015-04-16 21:29:01 +02:00
|
|
|
deprecated: "in favor of the ec2_ami_find module"
|
2014-03-11 17:14:04 +01:00
|
|
|
version_added: "1.6"
|
2013-11-08 03:40:29 +01:00
|
|
|
description:
|
2013-11-09 20:56:02 +01:00
|
|
|
- Look up the most recent AMI on AWS for a given operating system.
|
2013-11-08 03:40:29 +01:00
|
|
|
- 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).
|
2014-03-11 17:14:04 +01:00
|
|
|
- Only supports images from cloud-images.ubuntu.com
|
2014-03-11 18:26:35 +01:00
|
|
|
- 'Example output: C({"ami": "ami-69f5a900", "changed": false, "aki": "aki-88aa75e1", "tag": "release", "ari": null, "serial": "20131024"})'
|
|
|
|
version_added: "1.6"
|
2013-11-08 03:40:29 +01:00
|
|
|
options:
|
2013-11-09 20:56:02 +01:00
|
|
|
distro:
|
|
|
|
description: Linux distribution (e.g., C(ubuntu))
|
|
|
|
required: true
|
|
|
|
choices: ["ubuntu"]
|
2013-11-08 03:40:29 +01:00
|
|
|
release:
|
|
|
|
description: short name of the release (e.g., C(precise))
|
|
|
|
required: true
|
|
|
|
stream:
|
|
|
|
description: Type of release.
|
|
|
|
required: false
|
|
|
|
default: "server"
|
|
|
|
choices: ["server", "desktop"]
|
|
|
|
store:
|
|
|
|
description: Back-end store for instance
|
|
|
|
required: false
|
|
|
|
default: "ebs"
|
2014-06-29 22:53:52 +02:00
|
|
|
choices: ["ebs", "ebs-io1", "ebs-ssd", "instance-store"]
|
2013-11-08 03:40:29 +01:00
|
|
|
arch:
|
|
|
|
description: CPU architecture
|
|
|
|
required: false
|
|
|
|
default: "amd64"
|
|
|
|
choices: ["i386", "amd64"]
|
|
|
|
region:
|
|
|
|
description: EC2 region
|
|
|
|
required: false
|
|
|
|
default: us-east-1
|
|
|
|
choices: ["ap-northeast-1", "ap-southeast-1", "ap-southeast-2",
|
2014-11-01 03:44:44 +01:00
|
|
|
"eu-central-1", "eu-west-1", "sa-east-1", "us-east-1",
|
|
|
|
"us-west-1", "us-west-2", "us-gov-west-1"]
|
2013-11-08 03:40:29 +01:00
|
|
|
virt:
|
|
|
|
description: virutalization type
|
|
|
|
required: false
|
|
|
|
default: paravirtual
|
|
|
|
choices: ["paravirtual", "hvm"]
|
2015-07-20 22:38:37 +02:00
|
|
|
validate_certs:
|
|
|
|
description:
|
|
|
|
- If C(no), SSL certificates will not be validated. This should only
|
|
|
|
be set to C(no) used on personally controlled sites using self-signed
|
|
|
|
certificates. Prior to 1.9.3 the code defaulted to C(no).
|
|
|
|
required: false
|
|
|
|
default: 'yes'
|
|
|
|
choices: ['yes', 'no']
|
|
|
|
version_added: '1.9.3'
|
2013-11-08 03:40:29 +01:00
|
|
|
|
|
|
|
author: Lorin Hochstein
|
|
|
|
'''
|
|
|
|
|
|
|
|
EXAMPLES = '''
|
2014-03-15 07:16:14 +01:00
|
|
|
- name: Launch an Ubuntu 12.04 (Precise Pangolin) EC2 instance
|
2013-11-08 03:40:29 +01:00
|
|
|
hosts: 127.0.0.1
|
|
|
|
connection: local
|
|
|
|
tasks:
|
|
|
|
- name: Get the Ubuntu precise AMI
|
2013-11-09 20:56:02 +01:00
|
|
|
ec2_ami_search: distro=ubuntu release=precise region=us-west-1 store=instance-store
|
2013-11-08 03:40:29 +01:00
|
|
|
register: ubuntu_image
|
|
|
|
- name: Start the EC2 instance
|
|
|
|
ec2: image={{ ubuntu_image.ami }} instance_type=m1.small key_name=mykey
|
|
|
|
'''
|
|
|
|
|
|
|
|
import csv
|
|
|
|
import json
|
|
|
|
import urllib2
|
|
|
|
import urlparse
|
|
|
|
|
2013-11-09 20:56:02 +01:00
|
|
|
SUPPORTED_DISTROS = ['ubuntu']
|
|
|
|
|
2013-11-08 03:40:29 +01:00
|
|
|
AWS_REGIONS = ['ap-northeast-1',
|
|
|
|
'ap-southeast-1',
|
|
|
|
'ap-southeast-2',
|
2014-11-01 03:44:44 +01:00
|
|
|
'eu-central-1',
|
2013-11-08 03:40:29 +01:00
|
|
|
'eu-west-1',
|
|
|
|
'sa-east-1',
|
|
|
|
'us-east-1',
|
|
|
|
'us-west-1',
|
2014-11-01 03:44:44 +01:00
|
|
|
'us-west-2',
|
|
|
|
"us-gov-west-1"]
|
2013-11-08 03:40:29 +01:00
|
|
|
|
|
|
|
|
|
|
|
def get_url(module, url):
|
|
|
|
""" Get url and return response """
|
2015-07-20 22:38:37 +02:00
|
|
|
|
|
|
|
r, info = fetch_url(module, url)
|
|
|
|
if info['status'] != 200:
|
|
|
|
# Backwards compat
|
|
|
|
info['status_code'] = info['status']
|
|
|
|
module.fail_json(**info)
|
2013-11-08 03:40:29 +01:00
|
|
|
return r
|
|
|
|
|
|
|
|
|
2013-11-09 20:56:02 +01:00
|
|
|
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
|
2013-11-08 03:40:29 +01:00
|
|
|
|
|
|
|
table: an iterable that returns a row of
|
|
|
|
(release, stream, tag, serial, region, ami, aki, ari, virt)
|
|
|
|
release: ubuntu release name
|
|
|
|
stream: 'server' or 'desktop'
|
2014-06-29 22:53:52 +02:00
|
|
|
store: 'ebs', 'ebs-io1', 'ebs-ssd' or 'instance-store'
|
2013-11-08 03:40:29 +01:00
|
|
|
arch: 'i386' or 'amd64'
|
|
|
|
region: EC2 region
|
|
|
|
virt: 'paravirtual' or 'hvm'
|
|
|
|
|
|
|
|
Returns (ami, aki, ari, tag, serial)"""
|
|
|
|
expected = (release, stream, store, arch, region, virt)
|
|
|
|
|
|
|
|
for row in table:
|
|
|
|
(actual_release, actual_stream, tag, serial,
|
|
|
|
actual_store, actual_arch, actual_region, ami, aki, ari,
|
|
|
|
actual_virt) = row
|
|
|
|
actual = (actual_release, actual_stream, actual_store, actual_arch,
|
|
|
|
actual_region, actual_virt)
|
|
|
|
if actual == expected:
|
|
|
|
# aki and ari are sometimes blank
|
|
|
|
if aki == '':
|
|
|
|
aki = None
|
|
|
|
if ari == '':
|
|
|
|
ari = None
|
|
|
|
return (ami, aki, ari, tag, serial)
|
|
|
|
|
|
|
|
raise KeyError()
|
|
|
|
|
|
|
|
|
|
|
|
def get_ubuntu_url(release, stream):
|
|
|
|
url = "https://cloud-images.ubuntu.com/query/%s/%s/released.current.txt"
|
|
|
|
return url % (release, stream)
|
|
|
|
|
|
|
|
|
|
|
|
def main():
|
|
|
|
arg_spec = dict(
|
2013-11-09 20:56:02 +01:00
|
|
|
distro=dict(required=True, choices=SUPPORTED_DISTROS),
|
2013-11-08 03:40:29 +01:00
|
|
|
release=dict(required=True),
|
|
|
|
stream=dict(required=False, default='server',
|
|
|
|
choices=['desktop', 'server']),
|
|
|
|
store=dict(required=False, default='ebs',
|
2014-06-29 22:53:52 +02:00
|
|
|
choices=['ebs', 'ebs-io1', 'ebs-ssd', 'instance-store']),
|
2013-11-08 03:40:29 +01:00
|
|
|
arch=dict(required=False, default='amd64',
|
|
|
|
choices=['i386', 'amd64']),
|
|
|
|
region=dict(required=False, default='us-east-1', choices=AWS_REGIONS),
|
|
|
|
virt=dict(required=False, default='paravirtual',
|
2015-07-20 22:38:37 +02:00
|
|
|
choices=['paravirtual', 'hvm']),
|
|
|
|
validate_certs = dict(required=False, default=True, type='bool'),
|
2013-11-08 03:40:29 +01:00
|
|
|
)
|
|
|
|
module = AnsibleModule(argument_spec=arg_spec)
|
2013-11-09 20:56:02 +01:00
|
|
|
distro = module.params['distro']
|
2013-11-08 03:40:29 +01:00
|
|
|
|
2013-11-09 20:56:02 +01:00
|
|
|
if distro == 'ubuntu':
|
|
|
|
ubuntu(module)
|
|
|
|
else:
|
|
|
|
module.fail_json(msg="Unsupported distro: %s" % distro)
|
2013-11-08 03:40:29 +01:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
# this is magic, see lib/ansible/module_common.py
|
2015-05-18 20:59:20 +02:00
|
|
|
from ansible.module_utils.basic import *
|
2015-07-20 22:38:37 +02:00
|
|
|
from ansible.module_utils.urls import *
|
2013-11-08 03:40:29 +01:00
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
main()
|