-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathaws_ec2_instance_status_from_id.py
46 lines (34 loc) · 1.34 KB
/
aws_ec2_instance_status_from_id.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# (c) 2015, Jon Hadfield <jon@lessknown.co.uk>
"""
Description: This lookup takes an AWS region and a 'Name' tag value
of and ec2 instance and returns the current state.
Example Usage:
{{ lookup('aws_ec2_instance_status_from_name', ('eu-west-1', 'server1') }}
"""
from __future__ import (absolute_import, division, print_function)
__metaclass__ = type
# import os
# import codecs
from ansible.errors import *
from ansible.plugins.lookup import LookupBase
try:
# import boto
# import boto.ec2
import boto3
import botocore
except ImportError:
raise AnsibleError("aws_ec2_instance_status_from_name lookup cannot be run without boto installed")
class LookupModule(LookupBase):
def run(self, terms, variables=None, **kwargs):
region=terms[0][0]
instance_id=terms[0][1]
session=boto3.session.Session(region_name=region)
try:
ec2_client=session.client('ec2')
except botocore.exceptions.NoRegionError:
raise AnsibleError("AWS region not specified")
instance_filter=[{'Name': 'instance-id', 'Values': [instance_id]}]
result=ec2_client.describe_instance_status(InstanceIds=[instance_id])
if result and result.get('InstanceStatuses'):
return [result.get('InstanceStatuses')[0].get('InstanceState').get('Name').encode('utf-8')]
return None