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
42e26987
Commit
42e26987
authored
Jun 19, 2014
by
James Cammarata
Browse files
Options
Browse Files
Download
Plain Diff
Merge branch 'ec2_asg_info' of
https://github.com/dataxu/ansible
into dataxu-ec2_asg_info
parents
aac194e6
3e875c78
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
58 additions
and
12 deletions
+58
-12
library/cloud/ec2_asg
+58
-12
No files found.
library/cloud/ec2_asg
View file @
42e26987
...
...
@@ -67,6 +67,11 @@ options:
- List of VPC subnets to use
required: false
default: None
tags:
description:
- List of tag dictionaries to use. Required keys are 'key', 'value'. Optional key is 'propagate_at_launch', which defaults to true.
required: false
default: None
extends_documentation_fragment: aws
"""
...
...
@@ -80,6 +85,11 @@ EXAMPLES = '''
max_size: 10
desired_capacity: 5
vpc_zone_identifier: 'subnet-abcd1234,subnet-1a2b3c4d'
tags:
- key: environment
value: production
propagate_at_launch: no
'''
import
sys
...
...
@@ -90,12 +100,14 @@ from ansible.module_utils.ec2 import *
try
:
import
boto.ec2.autoscale
from
boto.ec2.autoscale
import
AutoScaleConnection
,
AutoScalingGroup
from
boto.ec2.autoscale
import
AutoScaleConnection
,
AutoScalingGroup
,
Tag
from
boto.exception
import
BotoServerError
except
ImportError
:
print
"failed=True msg='boto required for this module'"
sys
.
exit
(
1
)
ASG_ATTRIBUTES
=
(
'launch_config_name'
,
'max_size'
,
'min_size'
,
'desired_capacity'
,
'vpc_zone_identifier'
,
'availability_zones'
)
def
enforce_required_arguments
(
module
):
''' As many arguments are not required for autoscale group deletion
...
...
@@ -109,8 +121,15 @@ def enforce_required_arguments(module):
module
.
fail_json
(
msg
=
"Missing required arguments for autoscaling group create/update:
%
s"
%
","
.
join
(
missing_args
))
def
get_properties
(
autoscaling_group
):
properties
=
dict
((
attr
,
getattr
(
autoscaling_group
,
attr
))
for
attr
in
ASG_ATTRIBUTES
)
if
autoscaling_group
.
instances
:
properties
[
'instances'
]
=
[
i
.
instance_id
for
i
in
autoscaling_group
.
instances
]
properties
[
'load_balancers'
]
=
autoscaling_group
.
load_balancers
return
properties
def
create_autoscaling_group
(
connection
,
module
):
enforce_required_arguments
(
module
)
group_name
=
module
.
params
.
get
(
'name'
)
load_balancers
=
module
.
params
[
'load_balancers'
]
...
...
@@ -120,8 +139,7 @@ def create_autoscaling_group(connection, module):
max_size
=
module
.
params
[
'max_size'
]
desired_capacity
=
module
.
params
.
get
(
'desired_capacity'
)
vpc_zone_identifier
=
module
.
params
.
get
(
'vpc_zone_identifier'
)
launch_configs
=
connection
.
get_all_launch_configurations
(
names
=
[
launch_config_name
])
set_tags
=
module
.
params
.
get
(
'tags'
)
as_groups
=
connection
.
get_all_groups
(
names
=
[
group_name
])
...
...
@@ -131,9 +149,19 @@ def create_autoscaling_group(connection, module):
ec2_connection
=
connect_to_aws
(
boto
.
ec2
,
region
,
**
aws_connect_params
)
except
boto
.
exception
.
NoAuthHandlerFound
,
e
:
module
.
fail_json
(
msg
=
str
(
e
))
availability_zones
=
module
.
params
[
'availability_zones'
]
=
[
zone
.
name
for
zone
in
ec2_connection
.
get_all_zones
()]
asg_tags
=
[]
for
tag
in
set_tags
:
asg_tags
.
append
(
Tag
(
key
=
tag
.
get
(
'key'
),
value
=
tag
.
get
(
'value'
),
propagate_at_launch
=
bool
(
tag
.
get
(
'propagate_at_launch'
,
True
)),
resource_id
=
group_name
))
if
not
as_groups
:
if
not
vpc_zone_identifier
and
not
availability_zones
:
availability_zones
=
module
.
params
[
'availability_zones'
]
=
[
zone
.
name
for
zone
in
ec2_connection
.
get_all_zones
()]
enforce_required_arguments
(
module
)
launch_configs
=
connection
.
get_all_launch_configurations
(
names
=
[
launch_config_name
])
ag
=
AutoScalingGroup
(
group_name
=
group_name
,
load_balancers
=
load_balancers
,
...
...
@@ -143,31 +171,48 @@ def create_autoscaling_group(connection, module):
max_size
=
max_size
,
desired_capacity
=
desired_capacity
,
vpc_zone_identifier
=
vpc_zone_identifier
,
connection
=
connection
)
connection
=
connection
,
tags
=
asg_tags
)
try
:
connection
.
create_auto_scaling_group
(
ag
)
module
.
exit_json
(
changed
=
True
)
asg_properties
=
get_properties
(
ag
)
module
.
exit_json
(
changed
=
True
,
**
asg_properties
)
except
BotoServerError
,
e
:
module
.
fail_json
(
msg
=
str
(
e
))
else
:
as_group
=
as_groups
[
0
]
changed
=
False
for
attr
in
(
'launch_config_name'
,
'max_size'
,
'min_size'
,
'desired_capacity'
,
'vpc_zone_identifier'
,
'availability_zones'
):
if
getattr
(
as_group
,
attr
)
!=
module
.
params
.
get
(
attr
):
for
attr
in
ASG_ATTRIBUTES
:
if
module
.
params
.
get
(
attr
)
and
getattr
(
as_group
,
attr
)
!=
module
.
params
.
get
(
attr
):
changed
=
True
setattr
(
as_group
,
attr
,
module
.
params
.
get
(
attr
))
if
len
(
set_tags
)
>
0
:
existing_tags
=
as_group
.
tags
existing_tag_map
=
dict
((
tag
.
key
,
tag
)
for
tag
in
existing_tags
)
for
tag
in
set_tags
:
if
(
not
tag
[
'key'
]
in
existing_tag_map
or
existing_tag_map
[
tag
[
'key'
]]
.
value
!=
tag
[
'value'
]
or
(
'propagate_at_launch'
in
tag
and
existing_tag_map
[
tag
[
'key'
]]
.
propagate_at_launch
!=
tag
[
'propagate_at_launch'
])
):
changed
=
True
continue
if
changed
:
connection
.
create_or_update_tags
(
asg_tags
)
# handle loadbalancers separately because None != []
load_balancers
=
module
.
params
.
get
(
'load_balancers'
)
or
[]
if
as_group
.
load_balancers
!=
load_balancers
:
if
load_balancers
and
as_group
.
load_balancers
!=
load_balancers
:
changed
=
True
as_group
.
load_balancers
=
module
.
params
.
get
(
'load_balancers'
)
try
:
if
changed
:
as_group
.
update
()
module
.
exit_json
(
changed
=
changed
)
asg_properties
=
get_properties
(
as_group
)
module
.
exit_json
(
changed
=
changed
,
**
asg_properties
)
except
BotoServerError
,
e
:
module
.
fail_json
(
msg
=
str
(
e
))
...
...
@@ -207,6 +252,7 @@ def main():
desired_capacity
=
dict
(
type
=
'int'
),
vpc_zone_identifier
=
dict
(
type
=
'str'
),
state
=
dict
(
default
=
'present'
,
choices
=
[
'present'
,
'absent'
]),
tags
=
dict
(
type
=
'list'
,
default
=
[]),
)
)
module
=
AnsibleModule
(
argument_spec
=
argument_spec
)
...
...
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