From ec7de80ad06a9c8dee1adfdf650cb3411c4677f5 Mon Sep 17 00:00:00 2001 From: Mandar Kulkarni Date: Fri, 22 Nov 2024 15:49:18 -0800 Subject: [PATCH] create key pair if does not exist --- roles/ec2_instance_create/README.md | 3 ++- roles/ec2_instance_create/meta/argument_specs.yml | 2 +- .../tasks/ec2_instance_create_operations.yml | 15 +++++++++++++++ .../test_ec2_instance_create/defaults/main.yml | 1 - .../tasks/test_ec2_with_igw_sg_eip.yml | 2 +- 5 files changed, 19 insertions(+), 4 deletions(-) diff --git a/roles/ec2_instance_create/README.md b/roles/ec2_instance_create/README.md index bd8cfded..aa2b4ace 100644 --- a/roles/ec2_instance_create/README.md +++ b/roles/ec2_instance_create/README.md @@ -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`. @@ -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. diff --git a/roles/ec2_instance_create/meta/argument_specs.yml b/roles/ec2_instance_create/meta/argument_specs.yml index 96ec1d49..135a01dc 100644 --- a/roles/ec2_instance_create/meta/argument_specs.yml +++ b/roles/ec2_instance_create/meta/argument_specs.yml @@ -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 diff --git a/roles/ec2_instance_create/tasks/ec2_instance_create_operations.yml b/roles/ec2_instance_create/tasks/ec2_instance_create_operations.yml index 3cd2ce18..a26833b6 100644 --- a/roles/ec2_instance_create/tasks/ec2_instance_create_operations.yml +++ b/roles/ec2_instance_create/tasks/ec2_instance_create_operations.yml @@ -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 }}" diff --git a/tests/integration/targets/test_ec2_instance_create/defaults/main.yml b/tests/integration/targets/test_ec2_instance_create/defaults/main.yml index 958fe7fc..48e80666 100644 --- a/tests/integration/targets/test_ec2_instance_create/defaults/main.yml +++ b/tests/integration/targets/test_ec2_instance_create/defaults/main.yml @@ -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' diff --git a/tests/integration/targets/test_ec2_instance_create/tasks/test_ec2_with_igw_sg_eip.yml b/tests/integration/targets/test_ec2_instance_create/tasks/test_ec2_with_igw_sg_eip.yml index 2bcc9f21..30e482e8 100644 --- a/tests/integration/targets/test_ec2_instance_create/tasks/test_ec2_with_igw_sg_eip.yml +++ b/tests/integration/targets/test_ec2_instance_create/tasks/test_ec2_with_igw_sg_eip.yml @@ -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: