From 5f05a337b557e0950f8a5db7882d70d3eb3111d4 Mon Sep 17 00:00:00 2001 From: lx <1120722567@qq.com> Date: Tue, 12 Nov 2019 18:03:43 +0800 Subject: [PATCH] improve(ecs): support create spot instance --- .../modules/cloud/alicloud/ali_instance.py | 30 +++++++++++++++++-- tests/group_vars/all | 3 ++ tests/roles/instance/tasks/main.yml | 3 ++ 3 files changed, 34 insertions(+), 2 deletions(-) diff --git a/lib/ansible/modules/cloud/alicloud/ali_instance.py b/lib/ansible/modules/cloud/alicloud/ali_instance.py index c364643f..c6640394 100644 --- a/lib/ansible/modules/cloud/alicloud/ali_instance.py +++ b/lib/ansible/modules/cloud/alicloud/ali_instance.py @@ -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: @@ -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 @@ -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') @@ -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)) @@ -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) diff --git a/tests/group_vars/all b/tests/group_vars/all index e15f37cd..3e3a2e55 100644 --- a/tests/group_vars/all +++ b/tests/group_vars/all @@ -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" diff --git a/tests/roles/instance/tasks/main.yml b/tests/roles/instance/tasks/main.yml index 1bacb80f..08bdee78 100644 --- a/tests/roles/instance/tasks/main.yml +++ b/tests/roles/instance/tasks/main.yml @@ -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