Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
A
ansible
Overview
Overview
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
OpenEdx
ansible
Commits
b2ea24bc
Commit
b2ea24bc
authored
Dec 12, 2013
by
Nicholas DeClario
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Added ability to start and stop existing EC2 instances.
parent
bddaace9
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
126 additions
and
2 deletions
+126
-2
library/cloud/ec2
+126
-2
No files found.
library/cloud/ec2
View file @
b2ea24bc
...
@@ -17,9 +17,9 @@
...
@@ -17,9 +17,9 @@
DOCUMENTATION
=
'''
DOCUMENTATION
=
'''
---
---
module: ec2
module: ec2
short_description: create
or terminate
an instance in ec2, return instanceid
short_description: create
, terminate, start or stop
an instance in ec2, return instanceid
description:
description:
- Creates or terminates ec2 instances. When created optionally waits for it to be 'running'. This module has a dependency on python-boto >= 2.5
- Creates or terminates ec2 instances. When created optionally waits for it to be 'running'. This module has a dependency on python-boto >= 2.5
version_added: "0.9"
version_added: "0.9"
options:
options:
key_name:
key_name:
...
@@ -289,6 +289,49 @@ local_action:
...
@@ -289,6 +289,49 @@ local_action:
state: 'absent'
state: 'absent'
instance_ids: {{ec2.instance_ids}}
instance_ids: {{ec2.instance_ids}}
# 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:
- 'i-xxxxxx'
- 'i-xxxxxx'
- 'i-xxxxxx'
region: us-east-1
tasks:
- name: Start the sandbox instances
local_action:
module: ec2
instance_ids: '{{ instance_ids }}'
region: '{{ region }}'
state: running
wait: True
role:
- do_neat_stuff
- do_more_neat_stuff
- name: Stop sandbox instances
hosts: localhost
gather_facts: false
connection: local
vars:
instance_ids:
- 'i-xxxxxx'
- 'i-xxxxxx'
- 'i-xxxxxx'
region: us-east-1
tasks:
- name: Stop the sanbox instances
local_action:
module: e2
instance_ids: '{{ instance_ids }}'
region: {'{ region }}'
state: stopped
wait: True
'''
'''
import
sys
import
sys
...
@@ -621,6 +664,80 @@ def terminate_instances(module, ec2, instance_ids):
...
@@ -621,6 +664,80 @@ def terminate_instances(module, ec2, instance_ids):
return
(
changed
,
instance_dict_array
,
terminated_instance_ids
)
return
(
changed
,
instance_dict_array
,
terminated_instance_ids
)
def
startstop_instances
(
module
,
ec2
,
instance_ids
):
"""
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>}, ..]
Returns a dictionary of instance information
about the instances started.
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'
)
dest_state
=
module
.
params
.
get
(
'state'
)
dest_state_ec2
=
'stopped'
if
dest_state
==
'stopped'
else
'running'
# 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
:
if
not
inst
.
state
==
dest_state_ec2
:
instance_dict_array
.
append
(
get_instance_info
(
inst
))
try
:
if
dest_state
==
'running'
:
inst
.
start
()
else
:
inst
.
stop
()
except
EC2ResponseError
as
e
:
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
instids
=
[
i
.
id
for
i
in
res
.
instances
]
this_res
=
[]
num_running
=
0
wait_timeout
=
time
.
time
()
+
wait_timeout
while
wait_timeout
>
time
.
time
()
and
num_running
<
len
(
instids
):
res_list
=
res
.
connection
.
get_all_instances
(
instids
)
if
len
(
res_list
)
>
0
:
this_res
=
res_list
[
0
]
num_running
=
len
([
i
for
i
in
this_res
.
instances
if
i
.
state
==
dest_state_ec2
])
else
:
# 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
if
wait
and
wait_timeout
<=
time
.
time
():
# waiting took too long
module
.
fail_json
(
msg
=
"wait for instances running timeout on
%
s"
%
time
.
asctime
())
for
inst
in
this_res
.
instances
:
running_instances_array
.
append
(
inst
.
id
)
return
(
changed
,
instance_dict_array
,
running_instances_array
)
def
main
():
def
main
():
module
=
AnsibleModule
(
module
=
AnsibleModule
(
argument_spec
=
dict
(
argument_spec
=
dict
(
...
@@ -679,6 +796,13 @@ def main():
...
@@ -679,6 +796,13 @@ def main():
(
changed
,
instance_dict_array
,
new_instance_ids
)
=
terminate_instances
(
module
,
ec2
,
instance_ids
)
(
changed
,
instance_dict_array
,
new_instance_ids
)
=
terminate_instances
(
module
,
ec2
,
instance_ids
)
elif
module
.
params
.
get
(
'state'
)
==
'running'
or
module
.
params
.
get
(
'state'
)
==
'stopped'
:
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
)
(
changed
,
instance_dict_array
,
new_instance_ids
)
=
startstop_instances
(
module
,
ec2
,
instance_ids
)
elif
module
.
params
.
get
(
'state'
)
==
'present'
:
elif
module
.
params
.
get
(
'state'
)
==
'present'
:
# Changed is always set to true when provisioning new instances
# Changed is always set to true when provisioning new instances
if
not
module
.
params
.
get
(
'key_name'
):
if
not
module
.
params
.
get
(
'key_name'
):
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment