Merge branch 'exit_json' of https://github.com/psa/ansible into psa-exit_json
This commit is contained in:
commit
1c318fa3bd
3 changed files with 66 additions and 36 deletions
|
@ -217,6 +217,24 @@ def create_autoscaling_group(connection, module):
|
||||||
except BotoServerError, e:
|
except BotoServerError, e:
|
||||||
module.fail_json(msg=str(e))
|
module.fail_json(msg=str(e))
|
||||||
|
|
||||||
|
result = as_groups[0]
|
||||||
|
module.exit_json(changed=changed, name=result.name,
|
||||||
|
autoscaling_group_arn=result.autoscaling_group_arn,
|
||||||
|
availability_zones=result.availability_zones,
|
||||||
|
created_time=str(result.created_time),
|
||||||
|
default_cooldown=result.default_cooldown,
|
||||||
|
health_check_period=result.health_check_period,
|
||||||
|
health_check_type=result.health_check_type,
|
||||||
|
instance_id=result.instance_id,
|
||||||
|
instances=[instance.instance_id for instance in result.instances],
|
||||||
|
launch_config_name=result.launch_config_name,
|
||||||
|
load_balancers=result.load_balancers,
|
||||||
|
min_size=result.min_size, max_size=result.max_size,
|
||||||
|
placement_group=result.placement_group,
|
||||||
|
tags=result.tags,
|
||||||
|
termination_policies=result.termination_policies,
|
||||||
|
vpc_zone_identifier=result.vpc_zone_identifier)
|
||||||
|
|
||||||
|
|
||||||
def delete_autoscaling_group(connection, module):
|
def delete_autoscaling_group(connection, module):
|
||||||
group_name = module.params.get('name')
|
group_name = module.params.get('name')
|
||||||
|
|
|
@ -53,8 +53,8 @@ options:
|
||||||
- Determines how the threshold value is compared
|
- Determines how the threshold value is compared
|
||||||
required: false
|
required: false
|
||||||
options: ['<=','<','>','>=']
|
options: ['<=','<','>','>=']
|
||||||
threshold:
|
threshold:
|
||||||
description:
|
description:
|
||||||
- Sets the min/max bound for triggering the alarm
|
- Sets the min/max bound for triggering the alarm
|
||||||
required: false
|
required: false
|
||||||
period:
|
period:
|
||||||
|
@ -65,7 +65,7 @@ options:
|
||||||
description:
|
description:
|
||||||
- The number of times in which the metric is evaluated before final calculation
|
- The number of times in which the metric is evaluated before final calculation
|
||||||
required: false
|
required: false
|
||||||
unit:
|
unit:
|
||||||
description:
|
description:
|
||||||
- The threshold's unit of measurement
|
- The threshold's unit of measurement
|
||||||
required: false
|
required: false
|
||||||
|
@ -79,7 +79,7 @@ options:
|
||||||
- Describes to what the alarm is applied
|
- Describes to what the alarm is applied
|
||||||
required: false
|
required: false
|
||||||
alarm_actions:
|
alarm_actions:
|
||||||
description:
|
description:
|
||||||
- A list of the names action(s) taken when the alarm is in the 'alarm' status
|
- A list of the names action(s) taken when the alarm is in the 'alarm' status
|
||||||
required: false
|
required: false
|
||||||
insufficient_data_actions:
|
insufficient_data_actions:
|
||||||
|
@ -129,7 +129,7 @@ except ImportError:
|
||||||
|
|
||||||
|
|
||||||
def create_metric_alarm(connection, module):
|
def create_metric_alarm(connection, module):
|
||||||
|
|
||||||
name = module.params.get('name')
|
name = module.params.get('name')
|
||||||
metric = module.params.get('metric')
|
metric = module.params.get('metric')
|
||||||
namespace = module.params.get('namespace')
|
namespace = module.params.get('namespace')
|
||||||
|
@ -146,12 +146,12 @@ def create_metric_alarm(connection, module):
|
||||||
ok_actions = module.params.get('ok_actions')
|
ok_actions = module.params.get('ok_actions')
|
||||||
|
|
||||||
alarms = connection.describe_alarms(alarm_names=[name])
|
alarms = connection.describe_alarms(alarm_names=[name])
|
||||||
|
|
||||||
if not alarms:
|
if not alarms:
|
||||||
|
|
||||||
alm = MetricAlarm(
|
alm = MetricAlarm(
|
||||||
name=name,
|
name=name,
|
||||||
metric=metric,
|
metric=metric,
|
||||||
namespace=namespace,
|
namespace=namespace,
|
||||||
statistic=statistic,
|
statistic=statistic,
|
||||||
comparison=comparison,
|
comparison=comparison,
|
||||||
|
@ -165,9 +165,10 @@ def create_metric_alarm(connection, module):
|
||||||
insufficient_data_actions=insufficient_data_actions,
|
insufficient_data_actions=insufficient_data_actions,
|
||||||
ok_actions=ok_actions
|
ok_actions=ok_actions
|
||||||
)
|
)
|
||||||
try:
|
try:
|
||||||
connection.create_alarm(alm)
|
connection.create_alarm(alm)
|
||||||
module.exit_json(changed=True)
|
changed = True
|
||||||
|
alarms = connection.describe_alarms(alarm_names=[name])
|
||||||
except BotoServerError, e:
|
except BotoServerError, e:
|
||||||
module.fail_json(msg=str(e))
|
module.fail_json(msg=str(e))
|
||||||
|
|
||||||
|
@ -186,33 +187,51 @@ def create_metric_alarm(connection, module):
|
||||||
|
|
||||||
dim1 = module.params.get('dimensions')
|
dim1 = module.params.get('dimensions')
|
||||||
dim2 = alarm.dimensions
|
dim2 = alarm.dimensions
|
||||||
|
|
||||||
for keys in dim1:
|
for keys in dim1:
|
||||||
if not isinstance(dim1[keys], list):
|
if not isinstance(dim1[keys], list):
|
||||||
dim1[keys] = [dim1[keys]]
|
dim1[keys] = [dim1[keys]]
|
||||||
if dim1[keys] != dim2[keys]:
|
if dim1[keys] != dim2[keys]:
|
||||||
changed=True
|
changed=True
|
||||||
setattr(alarm, 'dimensions', dim1)
|
setattr(alarm, 'dimensions', dim1)
|
||||||
|
|
||||||
for attr in ('alarm_actions','insufficient_data_actions','ok_actions'):
|
for attr in ('alarm_actions','insufficient_data_actions','ok_actions'):
|
||||||
action = module.params.get(attr) or []
|
action = module.params.get(attr) or []
|
||||||
if getattr(alarm, attr) != action:
|
if getattr(alarm, attr) != action:
|
||||||
changed = True
|
changed = True
|
||||||
setattr(alarm, attr, module.params.get(attr))
|
setattr(alarm, attr, module.params.get(attr))
|
||||||
|
|
||||||
try:
|
try:
|
||||||
if changed:
|
if changed:
|
||||||
connection.create_alarm(alarm)
|
connection.create_alarm(alarm)
|
||||||
module.exit_json(changed=changed)
|
|
||||||
except BotoServerError, e:
|
except BotoServerError, e:
|
||||||
module.fail_json(msg=str(e))
|
module.fail_json(msg=str(e))
|
||||||
|
result = alarms[0]
|
||||||
|
module.exit_json(changed=changed, name=result.name,
|
||||||
|
actions_enabled=result.actions_enabled,
|
||||||
|
alarm_actions=result.alarm_actions,
|
||||||
|
alarm_arn=result.alarm_arn,
|
||||||
|
comparison=result.comparison,
|
||||||
|
description=result.description,
|
||||||
|
dimensions=result.dimensions,
|
||||||
|
evaluation_periods=result.evaluation_periods,
|
||||||
|
insufficient_data_actions=result.insufficient_data_actions,
|
||||||
|
last_updated=result.last_updated,
|
||||||
|
metric=result.metric,
|
||||||
|
namespace=result.namespace,
|
||||||
|
ok_actions=result.ok_actions,
|
||||||
|
period=result.period,
|
||||||
|
state_reason=result.state_reason,
|
||||||
|
state_value=result.state_value,
|
||||||
|
statistic=result.statistic,
|
||||||
|
threshold=result.threshold,
|
||||||
|
unit=result.unit)
|
||||||
|
|
||||||
def delete_metric_alarm(connection, module):
|
def delete_metric_alarm(connection, module):
|
||||||
name = module.params.get('name')
|
name = module.params.get('name')
|
||||||
|
|
||||||
alarms = connection.describe_alarms(alarm_names=[name])
|
alarms = connection.describe_alarms(alarm_names=[name])
|
||||||
|
|
||||||
if alarms:
|
if alarms:
|
||||||
try:
|
try:
|
||||||
connection.delete_alarms([name])
|
connection.delete_alarms([name])
|
||||||
|
|
|
@ -23,7 +23,7 @@ options:
|
||||||
- Name of the associated autoscaling group
|
- Name of the associated autoscaling group
|
||||||
required: true
|
required: true
|
||||||
adjustment_type:
|
adjustment_type:
|
||||||
desciption:
|
desciption:
|
||||||
- The type of change in capacity of the autoscaling group
|
- The type of change in capacity of the autoscaling group
|
||||||
required: false
|
required: false
|
||||||
choices: ['ChangeInCapacity','ExactCapacity','PercentChangeInCapacity']
|
choices: ['ChangeInCapacity','ExactCapacity','PercentChangeInCapacity']
|
||||||
|
@ -60,7 +60,7 @@ import sys
|
||||||
from ansible.module_utils.basic import *
|
from ansible.module_utils.basic import *
|
||||||
from ansible.module_utils.ec2 import *
|
from ansible.module_utils.ec2 import *
|
||||||
|
|
||||||
try:
|
try:
|
||||||
import boto.ec2.autoscale
|
import boto.ec2.autoscale
|
||||||
from boto.ec2.autoscale import ScalingPolicy
|
from boto.ec2.autoscale import ScalingPolicy
|
||||||
from boto.exception import BotoServerError
|
from boto.exception import BotoServerError
|
||||||
|
@ -77,9 +77,9 @@ def create_scaling_policy(connection, module):
|
||||||
scaling_adjustment = module.params.get('scaling_adjustment')
|
scaling_adjustment = module.params.get('scaling_adjustment')
|
||||||
min_adjustment_step = module.params.get('min_adjustment_step')
|
min_adjustment_step = module.params.get('min_adjustment_step')
|
||||||
cooldown = module.params.get('cooldown')
|
cooldown = module.params.get('cooldown')
|
||||||
|
|
||||||
scalingPolicies = connection.get_all_policies(as_group=asg_name,policy_names=[sp_name])
|
scalingPolicies = connection.get_all_policies(as_group=asg_name,policy_names=[sp_name])
|
||||||
|
|
||||||
if not scalingPolicies:
|
if not scalingPolicies:
|
||||||
sp = ScalingPolicy(
|
sp = ScalingPolicy(
|
||||||
name=sp_name,
|
name=sp_name,
|
||||||
|
@ -99,16 +99,17 @@ def create_scaling_policy(connection, module):
|
||||||
policy = scalingPolicies[0]
|
policy = scalingPolicies[0]
|
||||||
changed = False
|
changed = False
|
||||||
|
|
||||||
#min_adjustment_step attribute is only relevant if the adjustment_type
|
# min_adjustment_step attribute is only relevant if the adjustment_type
|
||||||
#is set to percentage change in capacity, so it is a special case
|
# is set to percentage change in capacity, so it is a special case
|
||||||
if getattr(policy, 'adjustment_type') == 'PercentChangeInCapacity':
|
if getattr(policy, 'adjustment_type') == 'PercentChangeInCapacity':
|
||||||
if getattr(policy, 'min_adjustment_step') != module.params.get('min_adjustment_step'):
|
if getattr(policy, 'min_adjustment_step') != module.params.get('min_adjustment_step'):
|
||||||
changed = True
|
changed = True
|
||||||
|
|
||||||
#set the min adjustment step incase the user decided to change their adjustment type to percentage
|
# set the min adjustment step incase the user decided to change their
|
||||||
|
# adjustment type to percentage
|
||||||
setattr(policy, 'min_adjustment_step', module.params.get('min_adjustment_step'))
|
setattr(policy, 'min_adjustment_step', module.params.get('min_adjustment_step'))
|
||||||
|
|
||||||
#check the remaining attributes
|
# check the remaining attributes
|
||||||
for attr in ('adjustment_type','scaling_adjustment','cooldown'):
|
for attr in ('adjustment_type','scaling_adjustment','cooldown'):
|
||||||
if getattr(policy, attr) != module.params.get(attr):
|
if getattr(policy, attr) != module.params.get(attr):
|
||||||
changed = True
|
changed = True
|
||||||
|
@ -118,8 +119,6 @@ def create_scaling_policy(connection, module):
|
||||||
if changed:
|
if changed:
|
||||||
connection.create_scaling_policy(policy)
|
connection.create_scaling_policy(policy)
|
||||||
policy = connection.get_all_policies(policy_names=[sp_name])[0]
|
policy = connection.get_all_policies(policy_names=[sp_name])[0]
|
||||||
module.exit_json(changed=changed, name=policy.name, arn=policy.policy_arn, as_name=policy.as_name, scaling_adjustment=policy.scaling_adjustment, cooldown=policy.cooldown, adjustment_type=policy.adjustment_type, min_adjustment_step=policy.min_adjustment_step)
|
|
||||||
policy = connection.get_all_policies(policy_names=[sp_name])[0]
|
|
||||||
module.exit_json(changed=changed, name=policy.name, arn=policy.policy_arn, as_name=policy.as_name, scaling_adjustment=policy.scaling_adjustment, cooldown=policy.cooldown, adjustment_type=policy.adjustment_type, min_adjustment_step=policy.min_adjustment_step)
|
module.exit_json(changed=changed, name=policy.name, arn=policy.policy_arn, as_name=policy.as_name, scaling_adjustment=policy.scaling_adjustment, cooldown=policy.cooldown, adjustment_type=policy.adjustment_type, min_adjustment_step=policy.min_adjustment_step)
|
||||||
except BotoServerError, e:
|
except BotoServerError, e:
|
||||||
module.fail_json(msg=str(e))
|
module.fail_json(msg=str(e))
|
||||||
|
@ -153,13 +152,13 @@ def main():
|
||||||
cooldown = dict(type='int'),
|
cooldown = dict(type='int'),
|
||||||
region = dict(aliases=['aws_region', 'ec2_region'], choices=AWS_REGIONS),
|
region = dict(aliases=['aws_region', 'ec2_region'], choices=AWS_REGIONS),
|
||||||
state=dict(default='present', choices=['present', 'absent']),
|
state=dict(default='present', choices=['present', 'absent']),
|
||||||
)
|
)
|
||||||
)
|
)
|
||||||
|
|
||||||
module = AnsibleModule(argument_spec=argument_spec)
|
module = AnsibleModule(argument_spec=argument_spec)
|
||||||
|
|
||||||
region, ec2_url, aws_connect_params = get_aws_connection_info(module)
|
region, ec2_url, aws_connect_params = get_aws_connection_info(module)
|
||||||
|
|
||||||
state = module.params.get('state')
|
state = module.params.get('state')
|
||||||
|
|
||||||
try:
|
try:
|
||||||
|
@ -176,9 +175,3 @@ def main():
|
||||||
|
|
||||||
|
|
||||||
main()
|
main()
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue