Skip to content

Instantly share code, notes, and snippets.

@ddbenson
Created August 17, 2014 22:08
Show Gist options
  • Save ddbenson/c29986e29b61c41fd466 to your computer and use it in GitHub Desktop.
Save ddbenson/c29986e29b61c41fd466 to your computer and use it in GitHub Desktop.
#!/usr/bin/python
try:
import boto.ec2
except ImportError:
print "failed=True msg='boto required for this module'"
sys.exit(1)
def main():
argument_spec = ec2_argument_spec()
argument_spec.update(dict(
region = dict(required=True),
name = dict(required=True),
)
)
module = AnsibleModule(
argument_spec=argument_spec,
)
name = module.params.get('name')
ec2 = ec2_connect(module)
filter = {}
filter['name'] = name
image = ec2.get_all_images(filters=filter)
if len(image) == 0:
module.fail_json(msg="No AMIs matched the name: %s" % name)
elif len(image) > 1:
module.fail_json(msg="More than one AMI matched the name: %s" % name)
else:
module.exit_json(image_id=image[0].id)
# import module snippets
from ansible.module_utils.basic import *
from ansible.module_utils.ec2 import *
if __name__ == '__main__':
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment