mirror of https://github.com/ansible/ansible.git
You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
1200 lines
41 KiB
Python
1200 lines
41 KiB
Python
12 years ago
|
#!/usr/bin/python
|
||
12 years ago
|
# 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/>.
|
||
|
|
||
|
DOCUMENTATION = '''
|
||
|
---
|
||
|
module: ec2
|
||
11 years ago
|
short_description: create, terminate, start or stop an instance in ec2, return instanceid
|
||
12 years ago
|
description:
|
||
11 years ago
|
- Creates or terminates ec2 instances. When created optionally waits for it to be 'running'. This module has a dependency on python-boto >= 2.5
|
||
12 years ago
|
version_added: "0.9"
|
||
|
options:
|
||
12 years ago
|
key_name:
|
||
12 years ago
|
description:
|
||
12 years ago
|
- key pair to use on the instance
|
||
11 years ago
|
required: false
|
||
12 years ago
|
default: null
|
||
12 years ago
|
aliases: ['keypair']
|
||
12 years ago
|
id:
|
||
|
description:
|
||
11 years ago
|
- identifier for this instance or set of instances, so that the module will be idempotent with respect to EC2 instances. This identifier is valid for at least 24 hours after the termination of the instance, and should not be reused for another call later on. For details, see the description of client token at U(http://docs.aws.amazon.com/AWSEC2/latest/UserGuide/Run_Instance_Idempotency.html).
|
||
12 years ago
|
required: false
|
||
|
default: null
|
||
|
aliases: []
|
||
12 years ago
|
group:
|
||
|
description:
|
||
12 years ago
|
- security group (or list of groups) to use with the instance
|
||
12 years ago
|
required: false
|
||
12 years ago
|
default: null
|
||
12 years ago
|
aliases: [ 'groups' ]
|
||
12 years ago
|
group_id:
|
||
|
version_added: "1.1"
|
||
|
description:
|
||
11 years ago
|
- security group id (or list of ids) to use with the instance
|
||
12 years ago
|
required: false
|
||
|
default: null
|
||
12 years ago
|
aliases: []
|
||
12 years ago
|
region:
|
||
|
version_added: "1.2"
|
||
|
description:
|
||
11 years ago
|
- The AWS region to use. Must be specified if ec2_url is not used. If not specified then the value of the EC2_REGION environment variable, if any, is used.
|
||
12 years ago
|
required: false
|
||
|
default: null
|
||
11 years ago
|
aliases: [ 'aws_region', 'ec2_region' ]
|
||
12 years ago
|
zone:
|
||
|
version_added: "1.2"
|
||
|
description:
|
||
11 years ago
|
- AWS availability zone in which to launch the instance
|
||
12 years ago
|
required: false
|
||
|
default: null
|
||
11 years ago
|
aliases: [ 'aws_zone', 'ec2_zone' ]
|
||
12 years ago
|
instance_type:
|
||
|
description:
|
||
|
- instance type to use for the instance
|
||
|
required: true
|
||
|
default: null
|
||
|
aliases: []
|
||
11 years ago
|
spot_price:
|
||
11 years ago
|
version_added: "1.5"
|
||
11 years ago
|
description:
|
||
|
- Maximum spot price to bid, If not set a regular on-demand instance is requested. A spot request is made with this maximum bid. When it is filled, the instance is started.
|
||
|
required: false
|
||
|
default: null
|
||
|
aliases: []
|
||
12 years ago
|
image:
|
||
|
description:
|
||
12 years ago
|
- I(emi) (or I(ami)) to use for the instance
|
||
12 years ago
|
required: true
|
||
|
default: null
|
||
|
aliases: []
|
||
|
kernel:
|
||
|
description:
|
||
12 years ago
|
- kernel I(eki) to use for the instance
|
||
12 years ago
|
required: false
|
||
|
default: null
|
||
|
aliases: []
|
||
|
ramdisk:
|
||
|
description:
|
||
12 years ago
|
- ramdisk I(eri) to use for the instance
|
||
12 years ago
|
required: false
|
||
|
default: null
|
||
|
aliases: []
|
||
|
wait:
|
||
|
description:
|
||
|
- wait for the instance to be in state 'running' before returning
|
||
12 years ago
|
required: false
|
||
12 years ago
|
default: "no"
|
||
|
choices: [ "yes", "no" ]
|
||
12 years ago
|
aliases: []
|
||
12 years ago
|
wait_timeout:
|
||
12 years ago
|
description:
|
||
12 years ago
|
- how long before wait gives up, in seconds
|
||
12 years ago
|
default: 300
|
||
|
aliases: []
|
||
11 years ago
|
spot_wait_timeout:
|
||
11 years ago
|
version_added: "1.5"
|
||
11 years ago
|
description:
|
||
|
- how long to wait for the spot instance request to be fulfilled
|
||
|
default: 600
|
||
|
aliases: []
|
||
12 years ago
|
count:
|
||
|
description:
|
||
|
- number of instances to launch
|
||
|
required: False
|
||
|
default: 1
|
||
|
aliases: []
|
||
11 years ago
|
monitoring:
|
||
12 years ago
|
version_added: "1.1"
|
||
12 years ago
|
description:
|
||
|
- enable detailed monitoring (CloudWatch) for instance
|
||
12 years ago
|
required: false
|
||
12 years ago
|
default: null
|
||
|
aliases: []
|
||
12 years ago
|
user_data:
|
||
|
version_added: "0.9"
|
||
|
description:
|
||
|
- opaque blob of data which is made available to the ec2 instance
|
||
12 years ago
|
required: false
|
||
12 years ago
|
default: null
|
||
|
aliases: []
|
||
12 years ago
|
instance_tags:
|
||
|
version_added: "1.0"
|
||
|
description:
|
||
12 years ago
|
- a hash/dictionary of tags to add to the new instance; '{"key":"value"}' and '{"key":"value","key":"value"}'
|
||
|
required: false
|
||
12 years ago
|
default: null
|
||
|
aliases: []
|
||
12 years ago
|
placement_group:
|
||
|
version_added: "1.3"
|
||
|
description:
|
||
|
- placement group for the instance when using EC2 Clustered Compute
|
||
|
required: false
|
||
|
default: null
|
||
|
aliases: []
|
||
12 years ago
|
vpc_subnet_id:
|
||
12 years ago
|
version_added: "1.1"
|
||
|
description:
|
||
|
- the subnet ID in which to launch the instance (VPC)
|
||
|
required: false
|
||
|
default: null
|
||
|
aliases: []
|
||
11 years ago
|
assign_public_ip:
|
||
11 years ago
|
version_added: "1.5"
|
||
11 years ago
|
description:
|
||
|
- when provisioning within vpc, assign a public IP address. Boto library must be 2.13.0+
|
||
|
required: false
|
||
|
default: null
|
||
|
aliases: []
|
||
12 years ago
|
private_ip:
|
||
12 years ago
|
version_added: "1.2"
|
||
12 years ago
|
description:
|
||
|
- the private ip address to assign the instance (from the vpc subnet)
|
||
|
required: false
|
||
10 years ago
|
default: null
|
||
12 years ago
|
aliases: []
|
||
11 years ago
|
instance_profile_name:
|
||
|
version_added: "1.3"
|
||
|
description:
|
||
11 years ago
|
- Name of the IAM instance profile to use. Boto library must be 2.5.0+
|
||
11 years ago
|
required: false
|
||
|
default: null
|
||
|
aliases: []
|
||
12 years ago
|
instance_ids:
|
||
12 years ago
|
version_added: "1.3"
|
||
|
description:
|
||
11 years ago
|
- "list of instance ids, currently used for states: absent, running, stopped"
|
||
12 years ago
|
required: false
|
||
|
default: null
|
||
|
aliases: []
|
||
11 years ago
|
source_dest_check:
|
||
11 years ago
|
version_added: "1.6"
|
||
11 years ago
|
description:
|
||
|
- Enable or Disable the Source/Destination checks (for NAT instances and Virtual Routers)
|
||
|
required: false
|
||
|
default: true
|
||
12 years ago
|
state:
|
||
|
version_added: "1.3"
|
||
|
description:
|
||
|
- create or terminate instances
|
||
|
required: false
|
||
|
default: 'present'
|
||
|
aliases: []
|
||
10 years ago
|
choices: ['present', 'absent', 'running', 'stopped']
|
||
11 years ago
|
volumes:
|
||
|
version_added: "1.5"
|
||
|
description:
|
||
|
- a list of volume dicts, each containing device name and optionally ephemeral id or snapshot id. Size and type (and number of iops for io device type) must be specified for a new volume or a root volume, and may be passed for a snapshot volume. For any volume, a volume size less than 1 will be interpreted as a request not to create the volume.
|
||
|
required: false
|
||
|
default: null
|
||
|
aliases: []
|
||
11 years ago
|
ebs_optimized:
|
||
|
version_added: "1.6"
|
||
|
description:
|
||
|
- whether instance is using optimized EBS volumes, see U(http://docs.aws.amazon.com/AWSEC2/latest/UserGuide/EBSOptimized.html)
|
||
|
required: false
|
||
|
default: false
|
||
11 years ago
|
exact_count:
|
||
|
version_added: "1.5"
|
||
|
description:
|
||
|
- An integer value which indicates how many instances that match the 'count_tag' parameter should be running. Instances are either created or terminated based on this value.
|
||
|
required: false
|
||
|
default: null
|
||
|
aliases: []
|
||
|
count_tag:
|
||
|
version_added: "1.5"
|
||
|
description:
|
||
|
- Used with 'exact_count' to determine how many nodes based on a specific tag criteria should be running. This can be expressed in multiple ways and is shown in the EXAMPLES section. For instance, one can request 25 servers that are tagged with "class=webserver".
|
||
|
required: false
|
||
|
default: null
|
||
|
aliases: []
|
||
12 years ago
|
|
||
12 years ago
|
author: Seth Vidal, Tim Gerla, Lester Wade
|
||
11 years ago
|
extends_documentation_fragment: aws
|
||
12 years ago
|
'''
|
||
|
|
||
12 years ago
|
EXAMPLES = '''
|
||
11 years ago
|
# Note: None of these examples set aws_access_key, aws_secret_key, or region.
|
||
|
# It is assumed that their matching environment variables are set.
|
||
|
|
||
12 years ago
|
# Basic provisioning example
|
||
12 years ago
|
- local_action:
|
||
|
module: ec2
|
||
11 years ago
|
key_name: mykey
|
||
12 years ago
|
instance_type: c1.medium
|
||
|
image: emi-40603AD1
|
||
|
wait: yes
|
||
|
group: webserver
|
||
12 years ago
|
count: 3
|
||
|
|
||
|
# Advanced example with tagging and CloudWatch
|
||
12 years ago
|
- local_action:
|
||
|
module: ec2
|
||
11 years ago
|
key_name: mykey
|
||
12 years ago
|
group: databases
|
||
|
instance_type: m1.large
|
||
|
image: ami-6e649707
|
||
|
wait: yes
|
||
|
wait_timeout: 500
|
||
|
count: 5
|
||
11 years ago
|
instance_tags:
|
||
|
db: postgres
|
||
|
monitoring: yes
|
||
12 years ago
|
|
||
11 years ago
|
# Single instance with additional IOPS volume from snapshot and volume delete on termination
|
||
11 years ago
|
local_action:
|
||
|
module: ec2
|
||
11 years ago
|
key_name: mykey
|
||
11 years ago
|
group: webserver
|
||
|
instance_type: m1.large
|
||
|
image: ami-6e649707
|
||
|
wait: yes
|
||
|
wait_timeout: 500
|
||
|
volumes:
|
||
|
- device_name: /dev/sdb
|
||
|
snapshot: snap-abcdef12
|
||
|
device_type: io1
|
||
|
iops: 1000
|
||
|
volume_size: 100
|
||
11 years ago
|
delete_on_termination: true
|
||
11 years ago
|
monitoring: yes
|
||
11 years ago
|
|
||
12 years ago
|
# Multiple groups example
|
||
12 years ago
|
local_action:
|
||
|
module: ec2
|
||
11 years ago
|
key_name: mykey
|
||
12 years ago
|
group: ['databases', 'internal-services', 'sshable', 'and-so-forth']
|
||
12 years ago
|
instance_type: m1.large
|
||
|
image: ami-6e649707
|
||
|
wait: yes
|
||
|
wait_timeout: 500
|
||
|
count: 5
|
||
11 years ago
|
instance_tags:
|
||
|
db: postgres
|
||
|
monitoring: yes
|
||
12 years ago
|
|
||
11 years ago
|
# Multiple instances with additional volume from snapshot
|
||
|
local_action:
|
||
|
module: ec2
|
||
11 years ago
|
key_name: mykey
|
||
11 years ago
|
group: webserver
|
||
|
instance_type: m1.large
|
||
|
image: ami-6e649707
|
||
|
wait: yes
|
||
|
wait_timeout: 500
|
||
|
count: 5
|
||
|
volumes:
|
||
|
- device_name: /dev/sdb
|
||
|
snapshot: snap-abcdef12
|
||
|
volume_size: 10
|
||
11 years ago
|
monitoring: yes
|
||
11 years ago
|
|
||
12 years ago
|
# VPC example
|
||
12 years ago
|
- local_action:
|
||
|
module: ec2
|
||
11 years ago
|
key_name: mykey
|
||
12 years ago
|
group_id: sg-1dc53f72
|
||
|
instance_type: m1.small
|
||
|
image: ami-6e649707
|
||
|
wait: yes
|
||
11 years ago
|
vpc_subnet_id: subnet-29e63245
|
||
11 years ago
|
assign_public_ip: yes
|
||
12 years ago
|
|
||
11 years ago
|
# Spot instance example
|
||
|
- local_action:
|
||
|
module: ec2
|
||
|
spot_price: 0.24
|
||
|
spot_wait_timeout: 600
|
||
|
keypair: mykey
|
||
|
group_id: sg-1dc53f72
|
||
|
instance_type: m1.small
|
||
|
image: ami-6e649707
|
||
|
wait: yes
|
||
|
vpc_subnet_id: subnet-29e63245
|
||
|
assign_public_ip: yes
|
||
|
|
||
12 years ago
|
# Launch instances, runs some tasks
|
||
|
# and then terminate them
|
||
|
|
||
|
|
||
|
- name: Create a sandbox instance
|
||
|
hosts: localhost
|
||
|
gather_facts: False
|
||
|
vars:
|
||
11 years ago
|
key_name: my_keypair
|
||
12 years ago
|
instance_type: m1.small
|
||
|
security_group: my_securitygroup
|
||
|
image: my_ami_id
|
||
|
region: us-east-1
|
||
|
tasks:
|
||
|
- name: Launch instance
|
||
11 years ago
|
local_action: ec2 key_name={{ keypair }} group={{ security_group }} instance_type={{ instance_type }} image={{ image }} wait=true region={{ region }}
|
||
12 years ago
|
register: ec2
|
||
|
- name: Add new instance to host group
|
||
11 years ago
|
local_action: add_host hostname={{ item.public_ip }} groupname=launched
|
||
|
with_items: ec2.instances
|
||
12 years ago
|
- name: Wait for SSH to come up
|
||
11 years ago
|
local_action: wait_for host={{ item.public_dns_name }} port=22 delay=60 timeout=320 state=started
|
||
|
with_items: ec2.instances
|
||
12 years ago
|
|
||
|
- name: Configure instance(s)
|
||
|
hosts: launched
|
||
|
sudo: True
|
||
|
gather_facts: True
|
||
|
roles:
|
||
|
- my_awesome_role
|
||
|
- my_awesome_test
|
||
|
|
||
|
- name: Terminate instances
|
||
|
hosts: localhost
|
||
|
connection: local
|
||
|
tasks:
|
||
|
- name: Terminate instances that were previously launched
|
||
|
local_action:
|
||
|
module: ec2
|
||
12 years ago
|
state: 'absent'
|
||
11 years ago
|
instance_ids: '{{ ec2.instance_ids }}'
|
||
12 years ago
|
|
||
11 years ago
|
# Start a few existing instances, run some tasks
|
||
|
# and stop the instances
|
||
|
|
||
|
- name: Start sandbox instances
|
||
|
hosts: localhost
|
||
|
gather_facts: false
|
||
|
connection: local
|
||
|
vars:
|
||
|
instance_ids:
|
||
11 years ago
|
- 'i-xxxxxx'
|
||
|
- 'i-xxxxxx'
|
||
|
- 'i-xxxxxx'
|
||
11 years ago
|
region: us-east-1
|
||
|
tasks:
|
||
|
- name: Start the sandbox instances
|
||
|
local_action:
|
||
11 years ago
|
module: ec2
|
||
|
instance_ids: '{{ instance_ids }}'
|
||
|
region: '{{ region }}'
|
||
|
state: running
|
||
|
wait: True
|
||
11 years ago
|
role:
|
||
|
- do_neat_stuff
|
||
|
- do_more_neat_stuff
|
||
|
|
||
|
- name: Stop sandbox instances
|
||
|
hosts: localhost
|
||
|
gather_facts: false
|
||
|
connection: local
|
||
|
vars:
|
||
|
instance_ids:
|
||
11 years ago
|
- 'i-xxxxxx'
|
||
|
- 'i-xxxxxx'
|
||
|
- 'i-xxxxxx'
|
||
11 years ago
|
region: us-east-1
|
||
|
tasks:
|
||
|
- name: Stop the sanbox instances
|
||
|
local_action:
|
||
11 years ago
|
module: ec2
|
||
11 years ago
|
instance_ids: '{{ instance_ids }}'
|
||
11 years ago
|
region: '{{ region }}'
|
||
11 years ago
|
state: stopped
|
||
11 years ago
|
wait: True
|
||
11 years ago
|
|
||
|
#
|
||
|
# Enforce that 5 instances with a tag "foo" are running
|
||
|
#
|
||
|
|
||
|
- local_action:
|
||
|
module: ec2
|
||
11 years ago
|
key_name: mykey
|
||
11 years ago
|
instance_type: c1.medium
|
||
|
image: emi-40603AD1
|
||
|
wait: yes
|
||
|
group: webserver
|
||
|
instance_tags:
|
||
|
foo: bar
|
||
|
exact_count: 5
|
||
|
count_tag: foo
|
||
|
|
||
|
#
|
||
11 years ago
|
# Enforce that 5 running instances named "database" with a "dbtype" of "postgres"
|
||
11 years ago
|
#
|
||
|
|
||
|
- local_action:
|
||
|
module: ec2
|
||
11 years ago
|
key_name: mykey
|
||
11 years ago
|
instance_type: c1.medium
|
||
|
image: emi-40603AD1
|
||
|
wait: yes
|
||
|
group: webserver
|
||
|
instance_tags:
|
||
11 years ago
|
Name: database
|
||
|
dbtype: postgres
|
||
11 years ago
|
exact_count: 5
|
||
|
count_tag:
|
||
11 years ago
|
Name: database
|
||
|
dbtype: postgres
|
||
11 years ago
|
|
||
|
#
|
||
|
# count_tag complex argument examples
|
||
|
#
|
||
|
|
||
|
# instances with tag foo
|
||
|
count_tag:
|
||
|
foo:
|
||
|
|
||
|
# instances with tag foo=bar
|
||
|
count_tag:
|
||
|
foo: bar
|
||
|
|
||
|
# instances with tags foo=bar & baz
|
||
|
count_tag:
|
||
|
foo: bar
|
||
|
baz:
|
||
|
|
||
|
# instances with tags foo & bar & baz=bang
|
||
|
count_tag:
|
||
|
- foo
|
||
|
- bar
|
||
|
- baz: bang
|
||
|
|
||
12 years ago
|
'''
|
||
|
|
||
12 years ago
|
import sys
|
||
12 years ago
|
import time
|
||
11 years ago
|
from ast import literal_eval
|
||
12 years ago
|
|
||
12 years ago
|
try:
|
||
12 years ago
|
import boto.ec2
|
||
11 years ago
|
from boto.ec2.blockdevicemapping import BlockDeviceType, BlockDeviceMapping
|
||
12 years ago
|
from boto.exception import EC2ResponseError
|
||
12 years ago
|
except ImportError:
|
||
|
print "failed=True msg='boto required for this module'"
|
||
|
sys.exit(1)
|
||
|
|
||
10 years ago
|
def find_running_instances_by_count_tag(module, ec2, count_tag, zone=None):
|
||
11 years ago
|
|
||
|
# get reservations for instances that match tag(s) and are running
|
||
10 years ago
|
reservations = get_reservations(module, ec2, tags=count_tag, state="running", zone=zone)
|
||
11 years ago
|
|
||
|
instances = []
|
||
|
for res in reservations:
|
||
|
if hasattr(res, 'instances'):
|
||
|
for inst in res.instances:
|
||
|
instances.append(inst)
|
||
|
|
||
|
return reservations, instances
|
||
|
|
||
|
|
||
|
def _set_none_to_blank(dictionary):
|
||
|
result = dictionary
|
||
|
for k in result.iterkeys():
|
||
|
if type(result[k]) == dict:
|
||
|
result[k] = _set_non_to_blank(result[k])
|
||
|
elif not result[k]:
|
||
|
result[k] = ""
|
||
|
return result
|
||
|
|
||
|
|
||
10 years ago
|
def get_reservations(module, ec2, tags=None, state=None, zone=None):
|
||
11 years ago
|
|
||
|
# TODO: filters do not work with tags that have underscores
|
||
|
filters = dict()
|
||
|
|
||
|
if tags is not None:
|
||
|
|
||
|
if type(tags) is str:
|
||
|
try:
|
||
|
tags = literal_eval(tags)
|
||
|
except:
|
||
|
pass
|
||
|
|
||
|
# if string, we only care that a tag of that name exists
|
||
|
if type(tags) is str:
|
||
|
filters.update({"tag-key": tags})
|
||
|
|
||
|
# if list, append each item to filters
|
||
|
if type(tags) is list:
|
||
|
for x in tags:
|
||
|
if type(x) is dict:
|
||
|
x = _set_none_to_blank(x)
|
||
|
filters.update(dict(("tag:"+tn, tv) for (tn,tv) in x.iteritems()))
|
||
|
else:
|
||
|
filters.update({"tag-key": x})
|
||
|
|
||
|
# if dict, add the key and value to the filter
|
||
|
if type(tags) is dict:
|
||
|
tags = _set_none_to_blank(tags)
|
||
|
filters.update(dict(("tag:"+tn, tv) for (tn,tv) in tags.iteritems()))
|
||
|
|
||
|
if state:
|
||
|
# http://stackoverflow.com/questions/437511/what-are-the-valid-instancestates-for-the-amazon-ec2-api
|
||
|
filters.update({'instance-state-name': state})
|
||
|
|
||
10 years ago
|
if zone:
|
||
|
filters.update({'availability-zone': zone})
|
||
|
|
||
11 years ago
|
results = ec2.get_all_instances(filters=filters)
|
||
|
|
||
|
return results
|
||
12 years ago
|
|
||
|
def get_instance_info(inst):
|
||
|
"""
|
||
|
Retrieves instance information from an instance
|
||
|
ID and returns it as a dictionary
|
||
|
"""
|
||
11 years ago
|
instance_info = {'id': inst.id,
|
||
|
'ami_launch_index': inst.ami_launch_index,
|
||
|
'private_ip': inst.private_ip_address,
|
||
|
'private_dns_name': inst.private_dns_name,
|
||
|
'public_ip': inst.ip_address,
|
||
|
'dns_name': inst.dns_name,
|
||
|
'public_dns_name': inst.public_dns_name,
|
||
|
'state_code': inst.state_code,
|
||
|
'architecture': inst.architecture,
|
||
|
'image_id': inst.image_id,
|
||
|
'key_name': inst.key_name,
|
||
|
'placement': inst.placement,
|
||
11 years ago
|
'region': inst.placement[:-1],
|
||
11 years ago
|
'kernel': inst.kernel,
|
||
|
'ramdisk': inst.ramdisk,
|
||
|
'launch_time': inst.launch_time,
|
||
|
'instance_type': inst.instance_type,
|
||
|
'root_device_type': inst.root_device_type,
|
||
|
'root_device_name': inst.root_device_name,
|
||
|
'state': inst.state,
|
||
11 years ago
|
'hypervisor': inst.hypervisor}
|
||
11 years ago
|
try:
|
||
|
instance_info['virtualization_type'] = getattr(inst,'virtualization_type')
|
||
|
except AttributeError:
|
||
|
instance_info['virtualization_type'] = None
|
||
|
|
||
11 years ago
|
try:
|
||
|
instance_info['ebs_optimized'] = getattr(inst, 'ebs_optimized')
|
||
|
except AttributeError:
|
||
|
instance_info['ebs_optimized'] = False
|
||
|
|
||
11 years ago
|
return instance_info
|
||
|
|
||
11 years ago
|
def boto_supports_associate_public_ip_address(ec2):
|
||
|
"""
|
||
|
Check if Boto library has associate_public_ip_address in the NetworkInterfaceSpecification
|
||
|
class. Added in Boto 2.13.0
|
||
|
|
||
|
ec2: authenticated ec2 connection object
|
||
|
|
||
|
Returns:
|
||
|
True if Boto library accepts associate_public_ip_address argument, else false
|
||
|
"""
|
||
|
|
||
|
try:
|
||
|
network_interface = boto.ec2.networkinterface.NetworkInterfaceSpecification()
|
||
|
getattr(network_interface, "associate_public_ip_address")
|
||
|
return True
|
||
|
except AttributeError:
|
||
|
return False
|
||
|
|
||
11 years ago
|
def boto_supports_profile_name_arg(ec2):
|
||
|
"""
|
||
|
Check if Boto library has instance_profile_name argument. instance_profile_name has been added in Boto 2.5.0
|
||
|
|
||
|
ec2: authenticated ec2 connection object
|
||
12 years ago
|
|
||
11 years ago
|
Returns:
|
||
|
True if Boto library accept instance_profile_name argument, else false
|
||
|
"""
|
||
|
run_instances_method = getattr(ec2, 'run_instances')
|
||
|
return 'instance_profile_name' in run_instances_method.func_code.co_varnames
|
||
12 years ago
|
|
||
11 years ago
|
def create_block_device(module, ec2, volume):
|
||
|
# Not aware of a way to determine this programatically
|
||
|
# http://aws.amazon.com/about-aws/whats-new/2013/10/09/ebs-provisioned-iops-maximum-iops-gb-ratio-increased-to-30-1/
|
||
|
MAX_IOPS_TO_SIZE_RATIO = 30
|
||
|
if 'snapshot' not in volume and 'ephemeral' not in volume:
|
||
|
if 'volume_size' not in volume:
|
||
|
module.fail_json(msg = 'Size must be specified when creating a new volume or modifying the root volume')
|
||
|
if 'snapshot' in volume:
|
||
|
if 'device_type' in volume and volume.get('device_type') == 'io1' and 'iops' not in volume:
|
||
|
module.fail_json(msg = 'io1 volumes must have an iops value set')
|
||
|
if 'iops' in volume:
|
||
|
snapshot = ec2.get_all_snapshots(snapshot_ids=[volume['snapshot']])[0]
|
||
|
size = volume.get('volume_size', snapshot.volume_size)
|
||
|
if int(volume['iops']) > MAX_IOPS_TO_SIZE_RATIO * size:
|
||
|
module.fail_json(msg = 'IOPS must be at most %d times greater than size' % MAX_IOPS_TO_SIZE_RATIO)
|
||
|
if 'ephemeral' in volume:
|
||
|
if 'snapshot' in volume:
|
||
|
module.fail_json(msg = 'Cannot set both ephemeral and snapshot')
|
||
|
return BlockDeviceType(snapshot_id=volume.get('snapshot'),
|
||
|
ephemeral_name=volume.get('ephemeral'),
|
||
|
size=volume.get('volume_size'),
|
||
|
volume_type=volume.get('device_type'),
|
||
|
delete_on_termination=volume.get('delete_on_termination', False),
|
||
|
iops=volume.get('iops'))
|
||
12 years ago
|
|
||
11 years ago
|
def boto_supports_param_in_spot_request(ec2, param):
|
||
|
"""
|
||
|
Check if Boto library has a <param> in its request_spot_instances() method. For example, the placement_group parameter wasn't added until 2.3.0.
|
||
|
|
||
|
ec2: authenticated ec2 connection object
|
||
|
|
||
|
Returns:
|
||
|
True if boto library has the named param as an argument on the request_spot_instances method, else False
|
||
|
"""
|
||
|
method = getattr(ec2, 'request_spot_instances')
|
||
|
return param in method.func_code.co_varnames
|
||
11 years ago
|
|
||
|
def enforce_count(module, ec2):
|
||
|
|
||
|
exact_count = module.params.get('exact_count')
|
||
|
count_tag = module.params.get('count_tag')
|
||
10 years ago
|
zone = module.params.get('zone')
|
||
11 years ago
|
|
||
10 years ago
|
reservations, instances = find_running_instances_by_count_tag(module, ec2, count_tag, zone)
|
||
11 years ago
|
|
||
|
changed = None
|
||
|
checkmode = False
|
||
10 years ago
|
instance_dict_array = []
|
||
11 years ago
|
changed_instance_ids = None
|
||
|
|
||
|
if len(instances) == exact_count:
|
||
|
changed = False
|
||
|
elif len(instances) < exact_count:
|
||
|
changed = True
|
||
|
to_create = exact_count - len(instances)
|
||
|
if not checkmode:
|
||
|
(instance_dict_array, changed_instance_ids, changed) \
|
||
|
= create_instances(module, ec2, override_count=to_create)
|
||
11 years ago
|
|
||
|
for inst in instance_dict_array:
|
||
|
instances.append(inst)
|
||
11 years ago
|
elif len(instances) > exact_count:
|
||
|
changed = True
|
||
|
to_remove = len(instances) - exact_count
|
||
|
if not checkmode:
|
||
|
all_instance_ids = sorted([ x.id for x in instances ])
|
||
|
remove_ids = all_instance_ids[0:to_remove]
|
||
11 years ago
|
|
||
|
instances = [ x for x in instances if x.id not in remove_ids]
|
||
|
|
||
11 years ago
|
(changed, instance_dict_array, changed_instance_ids) \
|
||
|
= terminate_instances(module, ec2, remove_ids)
|
||
|
terminated_list = []
|
||
|
for inst in instance_dict_array:
|
||
|
inst['state'] = "terminated"
|
||
|
terminated_list.append(inst)
|
||
|
instance_dict_array = terminated_list
|
||
11 years ago
|
|
||
|
# ensure all instances are dictionaries
|
||
|
all_instances = []
|
||
|
for inst in instances:
|
||
|
if type(inst) is not dict:
|
||
|
inst = get_instance_info(inst)
|
||
|
all_instances.append(inst)
|
||
|
|
||
|
return (all_instances, instance_dict_array, changed_instance_ids, changed)
|
||
11 years ago
|
|
||
|
|
||
|
def create_instances(module, ec2, override_count=None):
|
||
12 years ago
|
"""
|
||
|
Creates new instances
|
||
|
|
||
11 years ago
|
module : AnsibleModule object
|
||
12 years ago
|
ec2: authenticated ec2 connection object
|
||
|
|
||
|
Returns:
|
||
|
A list of dictionaries with instance information
|
||
|
about the instances that were launched
|
||
|
"""
|
||
12 years ago
|
|
||
12 years ago
|
key_name = module.params.get('key_name')
|
||
12 years ago
|
id = module.params.get('id')
|
||
12 years ago
|
group_name = module.params.get('group')
|
||
|
group_id = module.params.get('group_id')
|
||
12 years ago
|
zone = module.params.get('zone')
|
||
12 years ago
|
instance_type = module.params.get('instance_type')
|
||
11 years ago
|
spot_price = module.params.get('spot_price')
|
||
12 years ago
|
image = module.params.get('image')
|
||
11 years ago
|
if override_count:
|
||
|
count = override_count
|
||
|
else:
|
||
|
count = module.params.get('count')
|
||
12 years ago
|
monitoring = module.params.get('monitoring')
|
||
12 years ago
|
kernel = module.params.get('kernel')
|
||
|
ramdisk = module.params.get('ramdisk')
|
||
|
wait = module.params.get('wait')
|
||
12 years ago
|
wait_timeout = int(module.params.get('wait_timeout'))
|
||
11 years ago
|
spot_wait_timeout = int(module.params.get('spot_wait_timeout'))
|
||
12 years ago
|
placement_group = module.params.get('placement_group')
|
||
12 years ago
|
user_data = module.params.get('user_data')
|
||
12 years ago
|
instance_tags = module.params.get('instance_tags')
|
||
12 years ago
|
vpc_subnet_id = module.params.get('vpc_subnet_id')
|
||
11 years ago
|
assign_public_ip = module.boolean(module.params.get('assign_public_ip'))
|
||
12 years ago
|
private_ip = module.params.get('private_ip')
|
||
11 years ago
|
instance_profile_name = module.params.get('instance_profile_name')
|
||
11 years ago
|
volumes = module.params.get('volumes')
|
||
11 years ago
|
ebs_optimized = module.params.get('ebs_optimized')
|
||
11 years ago
|
exact_count = module.params.get('exact_count')
|
||
|
count_tag = module.params.get('count_tag')
|
||
11 years ago
|
source_dest_check = module.boolean(module.params.get('source_dest_check'))
|
||
12 years ago
|
|
||
11 years ago
|
# group_id and group_name are exclusive of each other
|
||
12 years ago
|
if group_id and group_name:
|
||
|
module.fail_json(msg = str("Use only one type of parameter (group_name) or (group_id)"))
|
||
|
sys.exit(1)
|
||
12 years ago
|
|
||
12 years ago
|
try:
|
||
12 years ago
|
# Here we try to lookup the group id from the security group name - if group is set.
|
||
|
if group_name:
|
||
|
grp_details = ec2.get_all_security_groups()
|
||
12 years ago
|
if type(group_name) == list:
|
||
11 years ago
|
group_id = [ str(grp.id) for grp in grp_details if str(grp.name) in group_name ]
|
||
12 years ago
|
elif type(group_name) == str:
|
||
|
for grp in grp_details:
|
||
|
if str(group_name) in str(grp):
|
||
|
group_id = [str(grp.id)]
|
||
|
group_name = [group_name]
|
||
12 years ago
|
# Now we try to lookup the group id testing if group exists.
|
||
|
elif group_id:
|
||
11 years ago
|
#wrap the group_id in a list if it's not one already
|
||
|
if type(group_id) == str:
|
||
|
group_id = [group_id]
|
||
12 years ago
|
grp_details = ec2.get_all_security_groups(group_ids=group_id)
|
||
|
grp_item = grp_details[0]
|
||
12 years ago
|
group_name = [grp_item.name]
|
||
12 years ago
|
except boto.exception.NoAuthHandlerFound, e:
|
||
|
module.fail_json(msg = str(e))
|
||
12 years ago
|
|
||
12 years ago
|
# Lookup any instances that much our run id.
|
||
12 years ago
|
|
||
12 years ago
|
running_instances = []
|
||
|
count_remaining = int(count)
|
||
12 years ago
|
|
||
12 years ago
|
if id != None:
|
||
|
filter_dict = {'client-token':id, 'instance-state-name' : 'running'}
|
||
12 years ago
|
previous_reservations = ec2.get_all_instances(None, filter_dict)
|
||
12 years ago
|
for res in previous_reservations:
|
||
|
for prev_instance in res.instances:
|
||
|
running_instances.append(prev_instance)
|
||
12 years ago
|
count_remaining = count_remaining - len(running_instances)
|
||
|
|
||
12 years ago
|
# Both min_count and max_count equal count parameter. This means the launch request is explicit (we want count, or fail) in how many instances we want.
|
||
12 years ago
|
|
||
11 years ago
|
if count_remaining == 0:
|
||
|
changed = False
|
||
|
else:
|
||
|
changed = True
|
||
12 years ago
|
try:
|
||
12 years ago
|
params = {'image_id': image,
|
||
12 years ago
|
'key_name': key_name,
|
||
|
'monitoring_enabled': monitoring,
|
||
12 years ago
|
'placement': zone,
|
||
12 years ago
|
'instance_type': instance_type,
|
||
|
'kernel_id': kernel,
|
||
|
'ramdisk_id': ramdisk,
|
||
11 years ago
|
'user_data': user_data}
|
||
|
|
||
11 years ago
|
if ebs_optimized:
|
||
|
params['ebs_optimized'] = ebs_optimized
|
||
|
|
||
11 years ago
|
if boto_supports_profile_name_arg(ec2):
|
||
|
params['instance_profile_name'] = instance_profile_name
|
||
|
else:
|
||
|
if instance_profile_name is not None:
|
||
|
module.fail_json(
|
||
11 years ago
|
msg="instance_profile_name parameter requires Boto version 2.5.0 or higher")
|
||
12 years ago
|
|
||
11 years ago
|
if assign_public_ip:
|
||
|
if not boto_supports_associate_public_ip_address(ec2):
|
||
|
module.fail_json(
|
||
|
msg="assign_public_ip parameter requires Boto version 2.13.0 or higher.")
|
||
|
elif not vpc_subnet_id:
|
||
|
module.fail_json(
|
||
|
msg="assign_public_ip only available with vpc_subnet_id")
|
||
|
|
||
|
else:
|
||
11 years ago
|
if private_ip:
|
||
|
interface = boto.ec2.networkinterface.NetworkInterfaceSpecification(
|
||
|
subnet_id=vpc_subnet_id,
|
||
|
private_ip_address=private_ip,
|
||
|
groups=group_id,
|
||
|
associate_public_ip_address=assign_public_ip)
|
||
|
else:
|
||
|
interface = boto.ec2.networkinterface.NetworkInterfaceSpecification(
|
||
|
subnet_id=vpc_subnet_id,
|
||
|
groups=group_id,
|
||
|
associate_public_ip_address=assign_public_ip)
|
||
11 years ago
|
interfaces = boto.ec2.networkinterface.NetworkInterfaceCollection(interface)
|
||
|
params['network_interfaces'] = interfaces
|
||
12 years ago
|
else:
|
||
11 years ago
|
params['subnet_id'] = vpc_subnet_id
|
||
|
if vpc_subnet_id:
|
||
|
params['security_group_ids'] = group_id
|
||
|
else:
|
||
|
params['security_groups'] = group_name
|
||
12 years ago
|
|
||
11 years ago
|
if volumes:
|
||
|
bdm = BlockDeviceMapping()
|
||
|
for volume in volumes:
|
||
|
if 'device_name' not in volume:
|
||
|
module.fail_json(msg = 'Device name must be set for volume')
|
||
|
# Minimum volume size is 1GB. We'll use volume size explicitly set to 0
|
||
|
# to be a signal not to create this volume
|
||
|
if 'volume_size' not in volume or int(volume['volume_size']) > 0:
|
||
|
bdm[volume['device_name']] = create_block_device(module, ec2, volume)
|
||
|
|
||
|
params['block_device_map'] = bdm
|
||
|
|
||
11 years ago
|
# check to see if we're using spot pricing first before starting instances
|
||
11 years ago
|
if not spot_price:
|
||
11 years ago
|
if assign_public_ip and private_ip:
|
||
|
params.update(dict(
|
||
|
min_count = count_remaining,
|
||
|
max_count = count_remaining,
|
||
|
client_token = id,
|
||
|
placement_group = placement_group,
|
||
|
))
|
||
|
else:
|
||
|
params.update(dict(
|
||
|
min_count = count_remaining,
|
||
|
max_count = count_remaining,
|
||
|
client_token = id,
|
||
|
placement_group = placement_group,
|
||
|
private_ip_address = private_ip,
|
||
|
))
|
||
|
|
||
11 years ago
|
res = ec2.run_instances(**params)
|
||
|
instids = [ i.id for i in res.instances ]
|
||
|
while True:
|
||
|
try:
|
||
|
ec2.get_all_instances(instids)
|
||
|
break
|
||
|
except boto.exception.EC2ResponseError as e:
|
||
|
if "<Code>InvalidInstanceID.NotFound</Code>" in str(e):
|
||
|
# there's a race between start and get an instance
|
||
|
continue
|
||
|
else:
|
||
|
module.fail_json(msg = str(e))
|
||
|
else:
|
||
|
if private_ip:
|
||
|
module.fail_json(
|
||
|
msg='private_ip only available with on-demand (non-spot) instances')
|
||
11 years ago
|
if boto_supports_param_in_spot_request(ec2, placement_group):
|
||
|
params['placement_group'] = placement_group
|
||
|
elif placement_group :
|
||
|
module.fail_json(
|
||
|
msg="placement_group parameter requires Boto version 2.3.0 or higher.")
|
||
|
|
||
11 years ago
|
params.update(dict(
|
||
|
count = count_remaining,
|
||
|
))
|
||
11 years ago
|
res = ec2.request_spot_instances(spot_price, **params)
|
||
11 years ago
|
|
||
|
# Now we have to do the intermediate waiting
|
||
11 years ago
|
if wait:
|
||
|
spot_req_inst_ids = dict()
|
||
|
spot_wait_timeout = time.time() + spot_wait_timeout
|
||
|
while spot_wait_timeout > time.time():
|
||
|
reqs = ec2.get_all_spot_instance_requests()
|
||
|
for sirb in res:
|
||
|
if sirb.id in spot_req_inst_ids:
|
||
|
continue
|
||
|
for sir in reqs:
|
||
|
if sir.id == sirb.id and sir.instance_id is not None:
|
||
|
spot_req_inst_ids[sirb.id] = sir.instance_id
|
||
|
if len(spot_req_inst_ids) < count:
|
||
|
time.sleep(5)
|
||
|
else:
|
||
|
break
|
||
|
if spot_wait_timeout <= time.time():
|
||
|
module.fail_json(msg = "wait for spot requests timeout on %s" % time.asctime())
|
||
|
instids = spot_req_inst_ids.values()
|
||
12 years ago
|
except boto.exception.BotoServerError, e:
|
||
11 years ago
|
module.fail_json(msg = "Instance creation failed => %s: %s" % (e.error_code, e.error_message))
|
||
12 years ago
|
|
||
10 years ago
|
# The instances returned through run_instances can be in
|
||
|
# terminated state due to idempotency.
|
||
|
terminated_instances = [ str(instance.id) for instance in res.instances
|
||
|
if instance.state == 'terminated' ]
|
||
|
if terminated_instances:
|
||
|
module.fail_json(msg = "Instances with id(s) %s " % terminated_instances +
|
||
|
"were created previously but have since been terminated - " +
|
||
|
"use a (possibly different) 'instanceid' parameter")
|
||
|
|
||
12 years ago
|
# wait here until the instances are up
|
||
|
num_running = 0
|
||
|
wait_timeout = time.time() + wait_timeout
|
||
11 years ago
|
while wait_timeout > time.time() and num_running < len(instids):
|
||
11 years ago
|
try:
|
||
|
res_list = ec2.get_all_instances(instids)
|
||
10 years ago
|
except boto.exception.BotoServerError, e:
|
||
11 years ago
|
if e.error_code == 'InvalidInstanceID.NotFound':
|
||
|
time.sleep(1)
|
||
|
continue
|
||
|
else:
|
||
|
raise
|
||
|
|
||
11 years ago
|
num_running = 0
|
||
|
for res in res_list:
|
||
|
num_running += len([ i for i in res.instances if i.state=='running' ])
|
||
|
if len(res_list) <= 0:
|
||
11 years ago
|
# got a bad response of some sort, possibly due to
|
||
|
# stale/cached data. Wait a second and then try again
|
||
|
time.sleep(1)
|
||
|
continue
|
||
|
if wait and num_running < len(instids):
|
||
|
time.sleep(5)
|
||
|
else:
|
||
|
break
|
||
|
|
||
12 years ago
|
if wait and wait_timeout <= time.time():
|
||
|
# waiting took too long
|
||
|
module.fail_json(msg = "wait for instances running timeout on %s" % time.asctime())
|
||
12 years ago
|
|
||
11 years ago
|
#We do this after the loop ends so that we end up with one list
|
||
|
for res in res_list:
|
||
|
running_instances.extend(res.instances)
|
||
12 years ago
|
|
||
11 years ago
|
# Enabled by default by Amazon
|
||
|
if not source_dest_check:
|
||
|
for inst in res.instances:
|
||
|
inst.modify_attribute('sourceDestCheck', False)
|
||
|
|
||
11 years ago
|
# Leave this as late as possible to try and avoid InvalidInstanceID.NotFound
|
||
|
if instance_tags:
|
||
|
try:
|
||
|
ec2.create_tags(instids, instance_tags)
|
||
|
except boto.exception.EC2ResponseError, e:
|
||
|
module.fail_json(msg = "Instance tagging failed => %s: %s" % (e.error_code, e.error_message))
|
||
|
|
||
12 years ago
|
instance_dict_array = []
|
||
12 years ago
|
created_instance_ids = []
|
||
12 years ago
|
for inst in running_instances:
|
||
12 years ago
|
d = get_instance_info(inst)
|
||
12 years ago
|
created_instance_ids.append(inst.id)
|
||
12 years ago
|
instance_dict_array.append(d)
|
||
12 years ago
|
|
||
11 years ago
|
return (instance_dict_array, created_instance_ids, changed)
|
||
12 years ago
|
|
||
|
|
||
12 years ago
|
def terminate_instances(module, ec2, instance_ids):
|
||
12 years ago
|
"""
|
||
|
Terminates a list of instances
|
||
|
|
||
|
module: Ansible module object
|
||
|
ec2: authenticated ec2 connection object
|
||
|
termination_list: a list of instances to terminate in the form of
|
||
|
[ {id: <inst-id>}, ..]
|
||
|
|
||
|
Returns a dictionary of instance information
|
||
|
about the instances terminated.
|
||
|
|
||
|
If the instance to be terminated is running
|
||
|
"changed" will be set to False.
|
||
|
|
||
|
"""
|
||
|
|
||
11 years ago
|
# Whether to wait for termination to complete before returning
|
||
|
wait = module.params.get('wait')
|
||
|
wait_timeout = int(module.params.get('wait_timeout'))
|
||
|
|
||
12 years ago
|
changed = False
|
||
|
instance_dict_array = []
|
||
12 years ago
|
|
||
|
if not isinstance(instance_ids, list) or len(instance_ids) < 1:
|
||
|
module.fail_json(msg='instance_ids should be a list of instances, aborting')
|
||
|
|
||
|
terminated_instance_ids = []
|
||
12 years ago
|
for res in ec2.get_all_instances(instance_ids):
|
||
|
for inst in res.instances:
|
||
11 years ago
|
if inst.state == 'running' or inst.state == 'stopped':
|
||
12 years ago
|
terminated_instance_ids.append(inst.id)
|
||
12 years ago
|
instance_dict_array.append(get_instance_info(inst))
|
||
|
try:
|
||
|
ec2.terminate_instances([inst.id])
|
||
11 years ago
|
except EC2ResponseError, e:
|
||
12 years ago
|
module.fail_json(msg='Unable to terminate instance {0}, error: {1}'.format(inst.id, e))
|
||
|
changed = True
|
||
|
|
||
11 years ago
|
# wait here until the instances are 'terminated'
|
||
|
if wait:
|
||
|
num_terminated = 0
|
||
|
wait_timeout = time.time() + wait_timeout
|
||
|
while wait_timeout > time.time() and num_terminated < len(terminated_instance_ids):
|
||
|
response = ec2.get_all_instances( \
|
||
|
instance_ids=terminated_instance_ids, \
|
||
|
filters={'instance-state-name':'terminated'})
|
||
|
try:
|
||
|
num_terminated = len(response.pop().instances)
|
||
|
except Exception, e:
|
||
|
# got a bad response of some sort, possibly due to
|
||
|
# stale/cached data. Wait a second and then try again
|
||
|
time.sleep(1)
|
||
|
continue
|
||
|
|
||
|
if num_terminated < len(terminated_instance_ids):
|
||
|
time.sleep(5)
|
||
|
|
||
|
# waiting took too long
|
||
|
if wait_timeout < time.time() and num_terminated < len(terminated_instance_ids):
|
||
|
module.fail_json(msg = "wait for instance termination timeout on %s" % time.asctime())
|
||
12 years ago
|
|
||
11 years ago
|
return (changed, instance_dict_array, terminated_instance_ids)
|
||
12 years ago
|
|
||
|
|
||
11 years ago
|
def startstop_instances(module, ec2, instance_ids, state):
|
||
11 years ago
|
"""
|
||
|
Starts or stops a list of existing instances
|
||
|
|
||
|
module: Ansible module object
|
||
|
ec2: authenticated ec2 connection object
|
||
|
instance_ids: The list of instances to start in the form of
|
||
|
[ {id: <inst-id>}, ..]
|
||
11 years ago
|
state: Intended state ("running" or "stopped")
|
||
11 years ago
|
|
||
|
Returns a dictionary of instance information
|
||
11 years ago
|
about the instances started/stopped.
|
||
11 years ago
|
|
||
|
If the instance was not able to change state,
|
||
|
"changed" will be set to False.
|
||
|
|
||
|
"""
|
||
|
|
||
|
wait = module.params.get('wait')
|
||
|
wait_timeout = int(module.params.get('wait_timeout'))
|
||
|
changed = False
|
||
|
instance_dict_array = []
|
||
|
|
||
|
if not isinstance(instance_ids, list) or len(instance_ids) < 1:
|
||
|
module.fail_json(msg='instance_ids should be a list of instances, aborting')
|
||
|
|
||
|
# Check that our instances are not in the state we want to take them to
|
||
|
# and change them to our desired state
|
||
|
running_instances_array = []
|
||
|
for res in ec2.get_all_instances(instance_ids):
|
||
|
for inst in res.instances:
|
||
11 years ago
|
if inst.state != state:
|
||
11 years ago
|
instance_dict_array.append(get_instance_info(inst))
|
||
|
try:
|
||
11 years ago
|
if state == 'running':
|
||
11 years ago
|
inst.start()
|
||
|
else:
|
||
|
inst.stop()
|
||
11 years ago
|
except EC2ResponseError, e:
|
||
11 years ago
|
module.fail_json(msg='Unable to change state for instance {0}, error: {1}'.format(inst.id, e))
|
||
|
changed = True
|
||
|
|
||
|
## Wait for all the instances to finish starting or stopping
|
||
|
wait_timeout = time.time() + wait_timeout
|
||
11 years ago
|
while wait and wait_timeout > time.time():
|
||
10 years ago
|
instance_dict_array = []
|
||
11 years ago
|
matched_instances = []
|
||
|
for res in ec2.get_all_instances(instance_ids):
|
||
|
for i in res.instances:
|
||
|
if i.state == state:
|
||
10 years ago
|
instance_dict_array.append(get_instance_info(i))
|
||
11 years ago
|
matched_instances.append(i)
|
||
|
if len(matched_instances) < len(instance_ids):
|
||
11 years ago
|
time.sleep(5)
|
||
|
else:
|
||
|
break
|
||
|
|
||
|
if wait and wait_timeout <= time.time():
|
||
|
# waiting took too long
|
||
|
module.fail_json(msg = "wait for instances running timeout on %s" % time.asctime())
|
||
|
|
||
11 years ago
|
return (changed, instance_dict_array, instance_ids)
|
||
11 years ago
|
|
||
|
|
||
12 years ago
|
def main():
|
||
11 years ago
|
argument_spec = ec2_argument_spec()
|
||
|
argument_spec.update(dict(
|
||
12 years ago
|
key_name = dict(aliases = ['keypair']),
|
||
|
id = dict(),
|
||
|
group = dict(type='list'),
|
||
11 years ago
|
group_id = dict(type='list'),
|
||
11 years ago
|
zone = dict(aliases=['aws_zone', 'ec2_zone']),
|
||
12 years ago
|
instance_type = dict(aliases=['type']),
|
||
11 years ago
|
spot_price = dict(),
|
||
12 years ago
|
image = dict(),
|
||
|
kernel = dict(),
|
||
11 years ago
|
count = dict(type='int', default='1'),
|
||
11 years ago
|
monitoring = dict(type='bool', default=False),
|
||
12 years ago
|
ramdisk = dict(),
|
||
11 years ago
|
wait = dict(type='bool', default=False),
|
||
12 years ago
|
wait_timeout = dict(default=300),
|
||
11 years ago
|
spot_wait_timeout = dict(default=600),
|
||
12 years ago
|
placement_group = dict(),
|
||
|
user_data = dict(),
|
||
11 years ago
|
instance_tags = dict(type='dict'),
|
||
12 years ago
|
vpc_subnet_id = dict(),
|
||
11 years ago
|
assign_public_ip = dict(type='bool', default=False),
|
||
12 years ago
|
private_ip = dict(),
|
||
11 years ago
|
instance_profile_name = dict(),
|
||
12 years ago
|
instance_ids = dict(type='list'),
|
||
11 years ago
|
source_dest_check = dict(type='bool', default=True),
|
||
12 years ago
|
state = dict(default='present'),
|
||
11 years ago
|
exact_count = dict(type='int', default=None),
|
||
11 years ago
|
count_tag = dict(),
|
||
11 years ago
|
volumes = dict(type='list'),
|
||
11 years ago
|
ebs_optimized = dict(type='bool', default=False),
|
||
12 years ago
|
)
|
||
|
)
|
||
11 years ago
|
|
||
|
module = AnsibleModule(
|
||
|
argument_spec=argument_spec,
|
||
|
mutually_exclusive = [
|
||
|
['exact_count', 'count'],
|
||
|
['exact_count', 'state'],
|
||
|
['exact_count', 'instance_ids']
|
||
|
],
|
||
|
)
|
||
12 years ago
|
|
||
11 years ago
|
ec2 = ec2_connect(module)
|
||
12 years ago
|
|
||
11 years ago
|
tagged_instances = []
|
||
11 years ago
|
|
||
11 years ago
|
state = module.params.get('state')
|
||
|
|
||
|
if state == 'absent':
|
||
12 years ago
|
instance_ids = module.params.get('instance_ids')
|
||
|
if not isinstance(instance_ids, list):
|
||
12 years ago
|
module.fail_json(msg='termination_list needs to be a list of instances to terminate')
|
||
|
|
||
12 years ago
|
(changed, instance_dict_array, new_instance_ids) = terminate_instances(module, ec2, instance_ids)
|
||
|
|
||
11 years ago
|
elif state in ('running', 'stopped'):
|
||
11 years ago
|
instance_ids = module.params.get('instance_ids')
|
||
|
if not isinstance(instance_ids, list):
|
||
|
module.fail_json(msg='running list needs to be a list of instances to run: %s' % instance_ids)
|
||
|
|
||
11 years ago
|
(changed, instance_dict_array, new_instance_ids) = startstop_instances(module, ec2, instance_ids, state)
|
||
11 years ago
|
|
||
11 years ago
|
elif state == 'present':
|
||
12 years ago
|
# Changed is always set to true when provisioning new instances
|
||
|
if not module.params.get('image'):
|
||
|
module.fail_json(msg='image parameter is required for new instance')
|
||
11 years ago
|
|
||
|
if module.params.get('exact_count') is None:
|
||
11 years ago
|
(instance_dict_array, new_instance_ids, changed) = create_instances(module, ec2)
|
||
11 years ago
|
else:
|
||
|
(tagged_instances, instance_dict_array, new_instance_ids, changed) = enforce_count(module, ec2)
|
||
12 years ago
|
|
||
11 years ago
|
module.exit_json(changed=changed, instance_ids=new_instance_ids, instances=instance_dict_array, tagged_instances=tagged_instances)
|
||
12 years ago
|
|
||
11 years ago
|
# import module snippets
|
||
|
from ansible.module_utils.basic import *
|
||
|
from ansible.module_utils.ec2 import *
|
||
12 years ago
|
|
||
|
main()
|