Skip to content

Commit

Permalink
improve(ecs): support create spot instance
Browse files Browse the repository at this point in the history
  • Loading branch information
lixue323 authored and xiaozhu36 committed Nov 12, 2019
1 parent 7c9043c commit 5f05a33
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 2 deletions.
30 changes: 28 additions & 2 deletions lib/ansible/modules/cloud/alicloud/ali_instance.py
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,15 @@
ram_role_name:
description:
- The name of the instance RAM role.
spot_price_limit:
description:
- The maximum hourly price for the preemptible instance. This parameter supports a maximum of three decimal
places and takes effect when the SpotStrategy parameter is set to SpotWithPriceLimit.
spot_strategy:
description:
- The bidding mode of the pay-as-you-go instance. This parameter is valid when InstanceChargeType is set to
PostPaid.
choices: ['NoSpot', 'SpotWithPriceLimit', 'SpotAsPriceGo']
author:
- "He Guimin (@xiaozhu36)"
requirements:
Expand Down Expand Up @@ -516,6 +525,18 @@
returned: always
type: dict
sample: vpc-0011223344
spot_price_limit:
description:
- The maximum hourly price for the preemptible instance.
returned: always
type: float
sample: 0.97
spot_strategy:
description:
- The bidding mode of the pay-as-you-go instance.
returned: always
type: string
sample: NoSpot
ids:
description: List of ECS instance IDs
returned: always
Expand Down Expand Up @@ -577,6 +598,8 @@ def create_instance(module, ecs, exact_count):
user_data = module.params['user_data']
key_name = module.params['key_name']
ram_role_name = module.params['ram_role_name']
spot_price_limit = module.params['spot_price_limit']
spot_strategy = module.params['spot_strategy']
# check whether the required parameter passed or not
if not image_id:
module.fail_json(msg='image_id is required for new instance')
Expand All @@ -601,7 +624,8 @@ def create_instance(module, ecs, exact_count):
count=exact_count, allocate_public_ip=allocate_public_ip,
instance_charge_type=instance_charge_type, period=period, period_unit="Month",
auto_renew=auto_renew, auto_renew_period=auto_renew_period, key_pair_name=key_name,
user_data=user_data, client_token=client_token, ram_role_name=ram_role_name)
user_data=user_data, client_token=client_token, ram_role_name=ram_role_name,
spot_price_limit=spot_price_limit, spot_strategy=spot_strategy)

except Exception as e:
module.fail_json(msg='Unable to create instance, error: {0}'.format(e))
Expand Down Expand Up @@ -674,7 +698,9 @@ def main():
auto_renew_period=dict(type='int', choices=[1, 2, 3, 6, 12]),
key_name=dict(type='str', aliases=['keypair']),
user_data=dict(type='str'),
ram_role_name=dict(type='str')
ram_role_name=dict(type='str'),
spot_price_limit=dict(type='float'),
spot_strategy=dict(type='str', default='NoSpot', choices=['NoSpot', 'SpotWithPriceLimit', 'SpotAsPriceGo'])
)
)
module = AnsibleModule(argument_spec=argument_spec)
Expand Down
3 changes: 3 additions & 0 deletions tests/group_vars/all
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,9 @@ system_disk_size: 50
number_of_instances: 2
ram_role_name: ansible-test-vpc

spot_price_limit: 0.97
spot_strategy: "SpotWithPriceLimit"

# create ECS disk parameters
disk_name: "Disk_From_Ansible"
disk_category: "cloud_ssd"
Expand Down
3 changes: 3 additions & 0 deletions tests/roles/instance/tasks/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,9 @@
tags: '{{ tags }}'
ram_role_name: '{{ ram_role_name }}'
assume_role_policy: '{{ assume_role_policy }}'

spot_price_limit: '{{ spot_price_limit }}'
spot_strategy: "{{ spot_strategy }}"
when: not instances.instances

- name: Get the existing ECS instances
Expand Down

0 comments on commit 5f05a33

Please sign in to comment.