pkaramol
pkaramol

Reputation: 19352

ansible-ec2: Enforce instance type change

I am running an (so far) idempotent ansible script to create ec2 instances.

So far, I had a dictionary with my ec2 parameters in the default section of my role, more or less as follows.

 my_instances:
 - instance_type: "t2.micro"
   exact_count: "1"
   assign_public_ip: "no"
 - instance_type: "t2.micro"
   exact_count: "1"
   assign_public_ip: "no"

and running the following task based on those vars:

- name: Create my instances
  ec2:
    instance_type: "{{ item.ec2_instance_type }}"
    region: "us-east-1"
    image: "some-image"
    group_id: "some-group-id"
    vpc_subnet_id: "some-subnet-id"
    instance_tags:
  with_items: "{{ my_instances }}"

Those instances are there and running so the script as I said is idempotent.

I want to re-run the script, having one of the instance types changed (from t2.micro --> t2.small), e.g. as follows:

 - instance_type: "t2.small"

However, when I perform the change in the above variable, the script runs idempotently again, although the output yields:

ok: [localhost] => (item={instance_type': u't2.small'})

(output trimmed).

The instance type remains t2.micro

Why is this happening?

Upvotes: 2

Views: 806

Answers (1)

Konstantin Suvorov
Konstantin Suvorov

Reputation: 68269

As far as I know, resize is still not supported out of the box. Here's a patch for ec2.py we use in our project:

@@ -1416,6 +1381,13 @@
                 inst.modify_attribute('disableApiTermination', termination_protection)
                 changed = True

+            # Check "instance_type" attribute
+            if instance_type and inst.get_attribute('instanceType')['instanceType'] != instance_type:
+                if inst.state != 'stopped':
+                    module.fail_json(msg='Unable to resize running instance {}'.format(inst.id))
+                inst.modify_attribute('instanceType', instance_type)
+                changed = True
+
             # Check instance state
             if inst.state != state:
                 instance_dict_array.append(get_instance_info(inst))

Hope I will find some time to submit pull-request.

Upvotes: 1

Related Questions