1
0
Fork 0
mirror of https://github.com/ansible-collections/community.general.git synced 2024-09-14 20:13:21 +02:00

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.
This commit is contained in:
Lorin Hochstein 2013-11-09 14:56:02 -05:00
parent 99fc134881
commit 0df1195fb9

View file

@ -18,16 +18,18 @@
# along with Ansible. If not, see <http://www.gnu.org/licenses/>.
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