Skip to content

Commit

Permalink
create key pair if does not exist
Browse files Browse the repository at this point in the history
  • Loading branch information
mandar242 committed Nov 22, 2024
1 parent 2f68c4b commit ec7de80
Show file tree
Hide file tree
Showing 5 changed files with 19 additions and 4 deletions.
3 changes: 2 additions & 1 deletion roles/ec2_instance_create/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ The following variables can be set in the role to customize EC2 instance creatio

### Role operation

* **ec2_instance_create_operation**: (Required)
* **ec2_instance_create_operation**: (Optional)
Whether to create or delete resources using the role. Default is `create`.
Choices are `create` and `delete`.

Expand All @@ -32,6 +32,7 @@ The following variables can be set in the role to customize EC2 instance creatio

* **ec2_instance_create_key_name**: (Required)
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.

* **ec2_instance_create_vpc_subnet_id**: (Required)
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/meta/argument_specs.yml
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ argument_specs:
type: dict
ec2_instance_create_associate_igw:
description:
- Whether to create and associate a internal gateway.
- Whether to create and associate an internal gateway.
required: false
default: false
type: bool
Expand Down
15 changes: 15 additions & 0 deletions roles/ec2_instance_create/tasks/ec2_instance_create_operations.yml
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,21 @@
Please provide different name to avoid updating instance."
when: ec2_info_result.instances | length >= 1

- name: Create a key pair if required
block:
- name: Get key pair info
amazon.aws.ec2_key_info:
names:
- "{{ ec2_instance_create_key_name }}"
register: key_info_result

- name: Create new key pair
amazon.aws.ec2_key:
name: "{{ ec2_instance_create_key_name }}"
state: present
region: "{{ ec2_instance_create_aws_region }}"
when: key_info_result.keypairs | length == 0

- name: Create EC2 instance with provided configuration
amazon.aws.ec2_instance:
region: "{{ ec2_instance_create_aws_region }}"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ aws_security_token: "{{ security_token | default(omit) }}"

# VPC and Subnet Configuration
vpc_name: "{{ resource_prefix }}-vpc"
test_vpc_name: 'vpc-{{ resource_prefix }}'
test_vpc_cidr: '101.{{ 255 | random(seed=resource_prefix) }}.0.0/16'
test_subnet_cidr: '101.{{ 255 | random(seed=resource_prefix) }}.0.0/24'

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
---
- block:
- name: Create EC2 instance with no external SG, no IGW, no EIP
- name: Create EC2 instance with external SG, IGW, EIP
ansible.builtin.include_role:
name: cloud.aws_ops.ec2_instance_create
vars:
Expand Down

0 comments on commit ec7de80

Please sign in to comment.