Created
January 21, 2015 05:05
-
-
Save adenot/fac2291011ba1efbb724 to your computer and use it in GitHub Desktop.
Ansible module find_ami - it returns image_id from an AMI name
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 | |
| # USAGE: | |
| # Save this as: library/find_ami | |
| # inside your ansible project folder | |
| # | |
| # chmod +x library/find_ami | |
| # | |
| # Playbook task: | |
| # - find_ami: | |
| # name: "ami_name_to_be_found" | |
| # region: "us-east-1" | |
| # register: ami | |
| # - debug: msg={{ami.image_id}} | |
| import datetime | |
| import sys | |
| import json | |
| import os | |
| import shlex | |
| import boto.ec2 | |
| # read the argument string from the arguments file | |
| args_file = sys.argv[1] | |
| args_data = file(args_file).read() | |
| name = "" | |
| region = "" | |
| arguments = shlex.split(args_data) | |
| for arg in arguments: | |
| # ignore any arguments without an equals in it | |
| if not "=" in arg: | |
| continue | |
| (key, value) = arg.split("=") | |
| if key == "name": | |
| name = value | |
| if key == "region": | |
| region = value | |
| connection = boto.ec2.connect_to_region(region) | |
| images = connection.get_all_images(owners=['self']) | |
| for image in images: | |
| if image.name == name: | |
| image_id = image.id | |
| print json.dumps({ | |
| "image_id": image_id | |
| }) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment