Skip to content

Commit

Permalink
Add optional deletion of key pair
Browse files Browse the repository at this point in the history
  • Loading branch information
hakbailey committed Dec 5, 2024
1 parent 6a16f82 commit c8956bc
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 6 deletions.
5 changes: 3 additions & 2 deletions roles/ec2_instance_create_delete/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,15 +39,16 @@ The following variables can be set in the role to customize EC2 instance creatio
The name of the EC2 instance to be created.

* **ec2_instance_create_delete_instance_type**: (Optional)
The instance type for the EC2 instance (e.g., `t2.micro`, `m5.large`). Required when `ec2_instance_create_delete_operation` is `true`
The instance type for the EC2 instance (e.g., `t2.micro`, `m5.large`). Required when `ec2_instance_create_delete_operation` is `create`

* **ec2_instance_create_delete_ami_id**: (Optional)
The AMI ID for the EC2 instance. Required when `ec2_instance_create_delete_operation` is `true`
The AMI ID for the EC2 instance. Required when `ec2_instance_create_delete_operation` is `create`

* **ec2_instance_create_delete_key_name**: (Optional)
The name of the key pair to use for SSH access to the EC2 instance.
If the key does not exist, a key pair will be created with the name.
If not provided, instance will not be accessible via SSH.
If provided when `ec2_instance_create_delete_operation` is `delete`, the keypair will also be deleted.

* **ec2_instance_create_delete_vpc_subnet_id**: (Optional)
The ID of the VPC subnet in which the instance will be launched.
Expand Down
2 changes: 1 addition & 1 deletion roles/ec2_instance_create_delete/meta/argument_specs.yml
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ argument_specs:
type: str
ec2_instance_create_delete_key_name:
description:
- The name of the key pair to use for SSH access to the EC2 instance. If the key does not exist, a key pair will be created with the name. If not provided, instance will not be accessible via SSH.
- The name of the key pair to use for SSH access to the EC2 instance. If the key does not exist, a key pair will be created with the name. If not provided, instance will not be accessible via SSH. If provided when `ec2_instance_create_delete_operation` is `delete`, the keypair will also be deleted.
required: false
type: str
ec2_instance_create_delete_vpc_subnet_id:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,3 +21,9 @@
wait: "{{ ec2_instance_create_delete_wait_for_state }}"
instance_ids:
- "{{ ec2_info_result.instances[0].instance_id }}"

- name: Delete keypair if provided
when: ec2_instance_create_delete_key_name is defined and ec2_instance_create_delete_key_name | length > 0
amazon.aws.ec2_key:
name: "{{ ec2_instance_create_delete_key_name }}"
state: absent
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
ec2_instance_create_delete_vpc_subnet_id: "{{ subnet_id }}"
ec2_instance_create_delete_tags:
Environment: Testing
ec2_instance_create_delete_wait_for_state: false
ec2_instance_create_delete_wait_for_state: true
ec2_instance_create_delete_associate_security_groups:
- "{{ test_security_group_name }}"
ec2_instance_create_delete_associate_eip: true
Expand All @@ -36,7 +36,7 @@
- _ec2_instance.instances[0].key_name == test_ec2_key_name
- _ec2_instance.instances[0].subnet_id == subnet_id
- _ec2_instance.instances[0].tags.Environment == "Testing"
- _ec2_instance.instances[0].state.name in ["running", "pending"]
- _ec2_instance.instances[0].state.name == "running"
- _ec2_instance.instances[0].network_interfaces[0].groups[0].group_name == test_security_group_name
- _ec2_instance.instances[0].network_interfaces[0].association.public_ip is defined

Expand All @@ -46,6 +46,7 @@
vars:
ec2_instance_create_delete_operation: delete
ec2_instance_create_delete_instance_name: "{{ test_ec2_instance_name }}"
ec2_instance_create_delete_key_name: "{{ test_ec2_key_name }}"

- name: Get EC2 instance info
amazon.aws.ec2_instance_info:
Expand All @@ -59,6 +60,16 @@
- _deleted_ec2_instance.instances | length == 1
- _deleted_ec2_instance.instances[0].state.name == "terminated"

- name: Get key info
amazon.aws.ec2_key_info:
names:
- "{{ test_ec2_key_name }}"
register: _deleted_key

- name: Verify that key pair was deleted
ansible.builtin.assert:
that: _deleted_key.keypairs | length == 0

# cleanup leftover resources created by role
always:
- name: Terminate EC2 instance
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
ec2_instance_create_delete_instance_name: "{{ test_ec2_instance_name }}"
ec2_instance_create_delete_instance_type: "{{ test_ec2_instance_type }}"
ec2_instance_create_delete_ami_id: "{{ image_id }}"
ec2_instance_create_delete_wait_for_state: false

- name: Get EC2 instance info
amazon.aws.ec2_instance_info:
Expand All @@ -22,7 +23,7 @@
- _ec2_instance.instances | length == 1
- _ec2_instance.instances[0].instance_type == test_ec2_instance_type
- _ec2_instance.instances[0].image_id == image_id
- _ec2_instance.instances[0].state.name == "running"
- _ec2_instance.instances[0].state.name in ["pending", "running"]
- _ec2_instance.instances[0].network_interfaces[0].groups[0].group_name == "default"

- name: Delete created instance
Expand Down

0 comments on commit c8956bc

Please sign in to comment.