generated from ansible-collections/collection_template
-
Notifications
You must be signed in to change notification settings - Fork 20
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add functionality to delete role resources
- Loading branch information
Showing
7 changed files
with
140 additions
and
91 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
93 changes: 93 additions & 0 deletions
93
roles/ec2_instance_create/tasks/ec2_instance_create_operations.yml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,93 @@ | ||
--- | ||
- name: Verify that the instance and security group with same name does not exist | ||
block: | ||
- name: Get instane info with provided name | ||
amazon.aws.ec2_instance_info: | ||
region: "{{ ec2_instance_create_aws_region }}" | ||
filters: | ||
tag:Name: "{{ ec2_instance_create_instance_name }}" | ||
instance-state-name: ["running"] | ||
register: ec2_info_result | ||
|
||
- name: Print warning and exit | ||
ansible.builtin.fail: | ||
msg: "Instance with name {{ ec2_instance_create_instance_name }} already exists in {{ ec2_instance_create_aws_region }}. | ||
Please provide different name to avoid updating instance." | ||
when: ec2_info_result.instances | length >= 1 | ||
|
||
- name: Create EC2 instance with provided configuration | ||
amazon.aws.ec2_instance: | ||
region: "{{ ec2_instance_create_aws_region }}" | ||
name: "{{ ec2_instance_create_instance_name }}" | ||
instance_type: "{{ ec2_instance_create_instance_type }}" | ||
image_id: "{{ ec2_instance_create_ami_id }}" | ||
key_name: "{{ ec2_instance_create_key_name }}" | ||
vpc_subnet_id: "{{ ec2_instance_create_vpc_subnet_id }}" | ||
security_group: "{{ ec2_instance_create_external_sg_id | default(omit) }}" | ||
tags: "{{ ec2_instance_create_tags | default(omit) }}" | ||
wait: "{{ ec2_instance_create_wait_for_boot }}" | ||
register: ec2_instance | ||
|
||
- name: Create security group if enabled | ||
when: ec2_instance_create_associate_external_sg is true | ||
block: | ||
- name: Define security group | ||
amazon.aws.ec2_security_group: | ||
name: "{{ ec2_instance_create_external_sg_name | default('ec2_instance_create-default-external-sg') }}" | ||
description: "{{ ec2_instance_create_external_sg_description | default('Security group for external access') }}" | ||
vpc_id: "{{ ec2_instance_create_vpc_id }}" | ||
rules: "{{ ec2_instance_create_external_sg_rules }}" | ||
tags: "{{ ec2_instance_create_sg_tags | default(omit) }}" | ||
register: ec2_group_creation | ||
|
||
- name: Associate security group with EC2 instance | ||
amazon.aws.ec2_instance: | ||
instance_ids: | ||
- "{{ ec2_instance.instance_ids[0] }}" | ||
security_groups: | ||
- "{{ ec2_instance_create_external_sg_name | default('ec2_instance_create-default-external-sg') }}" | ||
vpc_subnet_id: "{{ ec2_instance_create_vpc_subnet_id }}" | ||
register: ec2_instance_associate_external_sg | ||
|
||
- name: Create and Attach Internet Gateway if enabled | ||
when: ec2_instance_create_associate_igw is true | ||
block: | ||
- name: Create an Internet Gateway | ||
amazon.aws.ec2_vpc_igw: | ||
region: "{{ ec2_instance_create_aws_region }}" | ||
vpc_id: "{{ ec2_instance_create_vpc_id }}" | ||
state: present | ||
tags: "{{ ec2_instance_create_igw_tags | default(omit) }}" | ||
register: internet_gateway | ||
|
||
- name: Modify the route table to route internet traffic to Internet Gateway | ||
amazon.aws.ec2_vpc_route_table: | ||
region: "{{ ec2_instance_create_aws_region }}" | ||
vpc_id: "{{ ec2_instance_create_vpc_id }}" | ||
routes: | ||
- dest: "0.0.0.0/0" | ||
gateway_id: "{{ internet_gateway.gateway_id }}" | ||
state: present | ||
|
||
- name: Create and associate Elastic IP if enabled | ||
when: ec2_instance_create_associate_eip is true | ||
block: | ||
- name: Allocate and associate Elastic IP | ||
amazon.aws.ec2_eip: | ||
device_id: "{{ ec2_instance.instance_ids[0] }}" | ||
state: present | ||
release_on_disassociation: true | ||
tags: "{{ ec2_instance_create_eip_tags | default(omit) }}" | ||
register: instance_eip | ||
|
||
- name: Get EC2 instance info | ||
amazon.aws.ec2_instance_info: | ||
instance_ids: "{{ ec2_instance.instance_ids[0] }}" | ||
region: "{{ ec2_instance_create_aws_region }}" | ||
register: _ec2_instance | ||
|
||
- name: Output details of the created EC2 instance | ||
ansible.builtin.debug: | ||
msg: | ||
- "EC2 instance {{ ec2_instance.instance_ids[0] }} created successfully" | ||
- "Instance details: {{ _ec2_instance.instances[0] }}" |
24 changes: 24 additions & 0 deletions
24
roles/ec2_instance_create/tasks/ec2_instance_delete_operations.yml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
--- | ||
- name: Terminate EC2 instance | ||
amazon.aws.ec2_instance: | ||
region: "{{ ec2_instance_create_aws_region }}" | ||
name: "{{ ec2_instance_create_instance_name }}" | ||
instance_type: "{{ ec2_instance_create_instance_type }}" | ||
image_id: "{{ ec2_instance_create_ami_id }}" | ||
key_name: "{{ ec2_instance_create_key_name }}" | ||
vpc_subnet_id: "{{ ec2_instance_create_vpc_subnet_id }}" | ||
security_group: "{{ ec2_instance_create_external_sg_id | default(omit) }}" | ||
tags: "{{ ec2_instance_create_tags | default(omit) }}" | ||
wait: "{{ ec2_instance_create_wait_for_boot }}" | ||
state: absent | ||
|
||
- name: Delete security group if created | ||
amazon.aws.ec2_security_group: | ||
name: "{{ ec2_instance_create_external_sg_name | default('ec2_instance_create-default-external-sg') }}" | ||
state: absent | ||
|
||
- name: Detach and delete Internet Gateway if created | ||
amazon.aws.ec2_vpc_igw: | ||
region: "{{ ec2_instance_create_aws_region }}" | ||
vpc_id: "{{ ec2_instance_create_vpc_id }}" | ||
state: absent |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,96 +1,13 @@ | ||
--- | ||
- name: Run 'ec2_instance_create' role | ||
- name: EC2 Instance creation or deletion based on operation | ||
module_defaults: | ||
group/aws: "{{ aws_setup_credentials__output }}" | ||
block: | ||
- name: Verify that the instance and security group with same name does not exist | ||
block: | ||
- name: Get instane info with provided name | ||
amazon.aws.ec2_instance_info: | ||
region: "{{ ec2_instance_create_aws_region }}" | ||
filters: | ||
tag:Name: "{{ ec2_instance_create_instance_name }}" | ||
register: ec2_info_result | ||
|
||
- name: Print warning and exit | ||
ansible.builtin.fail: | ||
msg: "Instance with name {{ ec2_instance_create_instance_name }} already exists in {{ ec2_instance_create_aws_region }}. | ||
Please provide different name to avoid updating instance." | ||
when: ec2_info_result.instances | length >= 1 | ||
|
||
- name: Create EC2 instance with provided configuration | ||
amazon.aws.ec2_instance: | ||
region: "{{ ec2_instance_create_aws_region }}" | ||
name: "{{ ec2_instance_create_instance_name }}" | ||
instance_type: "{{ ec2_instance_create_instance_type }}" | ||
image_id: "{{ ec2_instance_create_ami_id }}" | ||
key_name: "{{ ec2_instance_create_key_name }}" | ||
vpc_subnet_id: "{{ ec2_instance_create_vpc_subnet_id }}" | ||
security_group: "{{ ec2_instance_create_external_sg_id | default(omit) }}" | ||
tags: "{{ ec2_instance_create_tags | default(omit) }}" | ||
wait: "{{ ec2_instance_create_wait_for_boot }}" | ||
register: ec2_instance | ||
|
||
- name: Create security group if enabled | ||
when: ec2_instance_create_associate_external_sg is true | ||
block: | ||
- name: Define security group | ||
amazon.aws.ec2_security_group: | ||
name: "{{ ec2_instance_create_external_sg_name | default('ec2_instance_create-default-external-sg') }}" | ||
description: "{{ ec2_instance_create_external_sg_description | default('Security group for external access') }}" | ||
vpc_id: "{{ ec2_instance_create_vpc_id }}" | ||
rules: "{{ ec2_instance_create_external_sg_rules }}" | ||
tags: "{{ ec2_instance_create_sg_tags | default(omit) }}" | ||
register: ec2_group_creation | ||
|
||
- name: Associate security group with EC2 instance | ||
amazon.aws.ec2_instance: | ||
instance_ids: | ||
- "{{ ec2_instance.instance_ids[0] }}" | ||
security_groups: | ||
- "{{ ec2_instance_create_external_sg_name | default('ec2_instance_create-default-external-sg') }}" | ||
vpc_subnet_id: "{{ ec2_instance_create_vpc_subnet_id }}" | ||
register: ec2_instance_associate_external_sg | ||
|
||
- name: Create and Attach Internet Gateway if enabled | ||
when: ec2_instance_create_associate_igw is true | ||
block: | ||
- name: Create an Internet Gateway | ||
amazon.aws.ec2_vpc_igw: | ||
region: "{{ ec2_instance_create_aws_region }}" | ||
vpc_id: "{{ ec2_instance_create_vpc_id }}" | ||
state: present | ||
tags: "{{ ec2_instance_create_igw_tags | default(omit) }}" | ||
register: internet_gateway | ||
|
||
- name: Modify the route table to route internet traffic to Internet Gateway | ||
amazon.aws.ec2_vpc_route_table: | ||
region: "{{ ec2_instance_create_aws_region }}" | ||
vpc_id: "{{ ec2_instance_create_vpc_id }}" | ||
routes: | ||
- dest: "0.0.0.0/0" | ||
gateway_id: "{{ internet_gateway.gateway_id }}" | ||
state: present | ||
|
||
- name: Create and associate Elastic IP if enabled | ||
when: ec2_instance_create_associate_eip is true | ||
block: | ||
- name: Allocate and associate Elastic IP | ||
amazon.aws.ec2_eip: | ||
device_id: "{{ ec2_instance.instance_ids[0] }}" | ||
state: present | ||
release_on_disassociation: true | ||
tags: "{{ ec2_instance_create_eip_tags | default(omit) }}" | ||
register: instance_eip | ||
|
||
- name: Get EC2 instance info | ||
amazon.aws.ec2_instance_info: | ||
instance_ids: "{{ ec2_instance.instance_ids[0] }}" | ||
region: "{{ ec2_instance_create_aws_region }}" | ||
register: _ec2_instance | ||
block: | ||
- name: Include create operations | ||
include_tasks: ec2_instance_create_operations.yml | ||
when: ec2_instance_create_operation == 'create' | ||
|
||
- name: Output details of the created EC2 instance | ||
ansible.builtin.debug: | ||
msg: | ||
- "EC2 instance {{ ec2_instance.instance_ids[0] }} created successfully" | ||
- "Instance details: {{ _ec2_instance.instances[0] }}" | ||
- name: Include delete operations | ||
include_tasks: ec2_instance_delete_operations.yml | ||
when: ec2_instance_create_operation == 'delete' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters