Skip to content

Commit

Permalink
Rename role to manage_ec2_instance
Browse files Browse the repository at this point in the history
  • Loading branch information
hakbailey committed Dec 6, 2024
1 parent c8956bc commit 79e661d
Show file tree
Hide file tree
Showing 16 changed files with 109 additions and 109 deletions.
4 changes: 0 additions & 4 deletions roles/ec2_instance_create_delete/defaults/main.yml

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
# ec2_instance_create
# manage_ec2_instance

A role to create an EC2 instance in AWS.
A role to create or delete an EC2 instance in AWS.

Users can specify various parameters for instance configuration, including instance type, AMI ID, key pair, tags, VPC/subnet configuration, and whether to associate an EIP. You can choose to wait for the EC2 instance to finish booting before continuing.
Users can specify various parameters for instance configuration, including instance type, AMI ID, key pair, tags, VPC/subnet configuration, and whether to associate an EIP. You can choose to wait for the EC2 instance to finish booting/terminating before continuing.

This role can be combined with the [cloud.aws_ops.ec2_networking_resources role](../ec2_networking_resources/README.md) to create networking resources for the instance, see [examples](#examples).
This role can be combined with the [cloud.aws_ops.ec2_networking_resources role](../ec2_networking_resources/README.md) to create/delete networking resources for the instance, see [examples](#examples).

## Requirements

Expand Down Expand Up @@ -32,42 +32,42 @@ An AWS account with the following permissions:

The following variables can be set in the role to customize EC2 instance creation and networking configurations:

* **ec2_instance_create_delete_operation**: (Optional)
* **manage_ec2_instance_operation**: (Optional)
Target operation for the ec2 instance role. Choices are ["create", "delete"]. Defaults to "create".

* **ec2_instance_create_delete_instance_name**: (Required)
The name of the EC2 instance to be created.
* **manage_ec2_instance_instance_name**: (Required)
The name of the EC2 instance to be created/deleted.

* **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 `create`
* **manage_ec2_instance_instance_type**: (Optional)
The instance type for the EC2 instance (e.g., `t2.micro`, `m5.large`). Required when `manage_ec2_instance_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 `create`
* **manage_ec2_instance_ami_id**: (Optional)
The AMI ID for the EC2 instance. Required when `manage_ec2_instance_operation` is `create`

* **ec2_instance_create_delete_key_name**: (Optional)
* **manage_ec2_instance_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.
If provided when `manage_ec2_instance_operation` is `delete`, the keypair will also be deleted.

* **ec2_instance_create_delete_vpc_subnet_id**: (Optional)
* **manage_ec2_instance_vpc_subnet_id**: (Optional)
The ID of the VPC subnet in which the instance will be launched.
If not provided, instance will be created in the default subnet for the default VPC in the AWS region if present.

* **ec2_instance_create_delete_tags**: (Optional)
* **manage_ec2_instance_tags**: (Optional)
A dictionary of tags to assign to the EC2 instance.

* **ec2_instance_create_delete_wait_for_state**: (Optional)
* **manage_ec2_instance_wait_for_state**: (Optional)
Whether to wait for the EC2 instance to be in the "running" (if creating an instance) or "terminated" (if deleting an instance) state before continuing. Default is `true`.

* **ec2_instance_create_delete_associate_security_groups**: (Optional)
* **manage_ec2_instance_associate_security_groups**: (Optional)
List of security group IDs to associate with the EC2 instance.

* **ec2_instance_create_delete_associate_eip**: (Optional)
* **manage_ec2_instance_associate_eip**: (Optional)
Whether to create an Elastic IP (EIP) and associate it with the EC2 instance. Default is `false`.
If true, EC2 instance must be launched in a VPC with an Internet Gateway (IGW) attached, otherwise this will fail. Use [cloud.aws_ops.ec2_networking_resources role](../ec2_networking_resources/README.md) to create the necessary networking resources.

* **ec2_instance_create_delete_eip_tags**: (Optional)
* **manage_ec2_instance_eip_tags**: (Optional)
Tags to assign to the elastic IP.

## Dependencies
Expand All @@ -84,18 +84,18 @@ Using the role on its own in a playbook:
hosts: localhost
gather_facts: false
roles:
- role: cloud.aws_ops.ec2_instance_create
- role: cloud.aws_ops.manage_ec2_instance
vars:
ec2_instance_create_delete_operation: present
ec2_instance_create_delete_aws_region: us-west-2
ec2_instance_create_delete_instance_name: my-test-instance
ec2_instance_create_delete_instance_type: t2.micro
ec2_instance_create_delete_ami_id: ami-066a7fbaa12345678
ec2_instance_create_delete_vpc_subnet_id: subnet-071443aa123456789
ec2_instance_create_delete_tags:
manage_ec2_instance_operation: create
manage_ec2_instance_aws_region: us-west-2
manage_ec2_instance_instance_name: my-test-instance
manage_ec2_instance_instance_type: t2.micro
manage_ec2_instance_ami_id: ami-066a7fbaa12345678
manage_ec2_instance_vpc_subnet_id: subnet-071443aa123456789
manage_ec2_instance_tags:
Component: my-test-instance
Environment: Testing
ec2_instance_create_delete_wait_for_state: true
manage_ec2_instance_wait_for_state: true
```
Combining the role with [cloud.aws_ops.ec2_networking_resources](../ec2_networking_resources/README.md):
Expand All @@ -114,17 +114,17 @@ Combining the role with [cloud.aws_ops.ec2_networking_resources](../ec2_networki
ec2_networking_resources_sg_internal_name: my-internal-sg
ec2_networking_resources_sg_external_name: my-external-sg
ec2_networking_resources_create_igw: true
- role: cloud.aws_ops.ec2_instance_create
- role: cloud.aws_ops.manage_ec2_instance
vars:
ec2_instance_create_delete_operation: present
ec2_instance_create_delete_instance_name: my-test-instance
ec2_instance_create_delete_instance_type: t2.micro
ec2_instance_create_delete_ami_id: ami-066a7fbaa12345678
ec2_instance_create_delete_vpc_subnet_id: "{{ ec2_networking_resources_subnet_result.subnet.id }}"
ec2_instance_create_delete_associate_security_groups:
manage_ec2_instance_operation: present
manage_ec2_instance_instance_name: my-test-instance
manage_ec2_instance_instance_type: t2.micro
manage_ec2_instance_ami_id: ami-066a7fbaa12345678
manage_ec2_instance_vpc_subnet_id: "{{ ec2_networking_resources_subnet_result.subnet.id }}"
manage_ec2_instance_associate_security_groups:
- my-internal-sg
- my-external-sg
ec2_instance_create_delete_associate_eip: true
manage_ec2_instance_associate_eip: true
```
Deleting an EC2 instance:
Expand All @@ -135,11 +135,11 @@ Deleting an EC2 instance:
hosts: localhost
gather_facts: false
roles:
- role: cloud.aws_ops.ec2_instance_create_delete
- role: cloud.aws_ops.manage_ec2_instance
vars:
ec2_instance_create_delete_operation: delete
ec2_instance_create_delete_instance_name: my-test-instance
ec2_instance_create_delete_wait_for_state: true
manage_ec2_instance_operation: delete
manage_ec2_instance_instance_name: my-test-instance
manage_ec2_instance_wait_for_state: true
```
## License
Expand Down
4 changes: 4 additions & 0 deletions roles/manage_ec2_instance/defaults/main.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
---
manage_ec2_instance_operation: create
manage_ec2_instance_wait_for_state: true
manage_ec2_instance_associate_eip: false
Original file line number Diff line number Diff line change
@@ -1,68 +1,68 @@
---
argument_specs:
main:
short_description: A role to create an EC2 instance with optional networking resources.
short_description: A role to create or delete an EC2 instance with optional networking resources.
description:
- A role to create an EC2 instance.
- A role to create or delete an EC2 instance.
- Can optionally attach security groups and associate an Elastic IP with the instance.
- Supports custom configurations for instance settings including instance type, AMI, key pair, tags, VPC/subnet, and networking configurations.
options:
ec2_instance_create_delete_operation:
manage_ec2_instance_operation:
description:
- Whether to create or delete resources using the role.
required: false
type: str
default: create
choices: [create, delete]
ec2_instance_create_delete_instance_name:
manage_ec2_instance_instance_name:
description:
- The name of the EC2 instance to be created.
required: true
type: str
ec2_instance_create_delete_instance_type:
manage_ec2_instance_instance_type:
description:
- The instance type for the EC2 instance. Required when `ec2_instance_create_delete_operation` is `true`.
- The instance type for the EC2 instance. Required when `manage_ec2_instance_operation` is `create`.
required: false
type: str
ec2_instance_create_delete_ami_id:
manage_ec2_instance_ami_id:
description:
- 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 `manage_ec2_instance_operation` is `create`.
required: false
type: str
ec2_instance_create_delete_key_name:
manage_ec2_instance_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. If provided when `ec2_instance_create_delete_operation` is `delete`, the keypair will also be deleted.
- 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 `manage_ec2_instance_operation` is `delete`, the keypair will also be deleted.
required: false
type: str
ec2_instance_create_delete_vpc_subnet_id:
manage_ec2_instance_vpc_subnet_id:
description:
- The ID of the VPC subnet in which the instance will be launched. If not provided, instance will be created in the default subnet for the default VPC in the AWS region, if present.
required: false
type: str
ec2_instance_create_delete_tags:
manage_ec2_instance_tags:
description:
- A dictionary of tags to assign to the EC2 instance.
required: false
type: dict
ec2_instance_create_delete_wait_for_state:
manage_ec2_instance_wait_for_state:
description:
- Whether to wait for the EC2 instance to be in the running/terminated state before continuing.
required: false
default: true
type: bool
ec2_instance_create_delete_associate_security_groups:
manage_ec2_instance_associate_security_groups:
description:
- List of security group names or IDs to associate with the EC2 instance.
required: false
type: list
elements: str
ec2_instance_create_delete_associate_eip:
manage_ec2_instance_associate_eip:
description:
- Whether to create and associate an Elastic IP (EIP) with the EC2 instance.
required: false
default: false
type: bool
ec2_instance_create_delete_eip_tags:
manage_ec2_instance_eip_tags:
description:
- Tags to assign to the Elastic IP.
required: false
Expand Down
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -4,46 +4,46 @@
- name: Get instance info with provided name
amazon.aws.ec2_instance_info:
filters:
tag:Name: "{{ ec2_instance_create_delete_instance_name }}"
tag:Name: "{{ manage_ec2_instance_instance_name }}"
instance-state-name: ["pending", "running", "stopping", "stopped"]
register: ec2_info_result

- name: Print warning and exit if instance exists
ansible.builtin.fail:
msg: "Instance with name {{ ec2_instance_create_delete_instance_name }} already exists in {{ aws_region }}. Please provide a different name to avoid updating the existing instance."
msg: "Instance with name {{ manage_ec2_instance_instance_name }} already exists in {{ aws_region }}. Please provide a different name to avoid updating the existing instance."
when: ec2_info_result.instances | length > 0

- name: Create a key pair if required
when: ec2_instance_create_delete_key_name is defined and ec2_instance_create_delete_key_name | length > 0
when: manage_ec2_instance_key_name is defined and manage_ec2_instance_key_name | length > 0
block:
- name: Get key pair info
amazon.aws.ec2_key_info:
names:
- "{{ ec2_instance_create_delete_key_name }}"
- "{{ manage_ec2_instance_key_name }}"
register: key_info_result

- name: Create new key pair
amazon.aws.ec2_key:
name: "{{ ec2_instance_create_delete_key_name }}"
name: "{{ manage_ec2_instance_key_name }}"
state: present
when: key_info_result.keypairs | length == 0
register: new_key_pair_result

- name: Create EC2 instance with provided configuration
amazon.aws.ec2_instance:
state: running
name: "{{ ec2_instance_create_delete_instance_name }}"
instance_type: "{{ ec2_instance_create_delete_instance_type }}"
image_id: "{{ ec2_instance_create_delete_ami_id }}"
key_name: "{{ ec2_instance_create_delete_key_name | default(omit) }}"
security_groups: "{{ ec2_instance_create_delete_associate_security_groups | default(omit, true) }}"
vpc_subnet_id: "{{ ec2_instance_create_delete_vpc_subnet_id | default(omit) }}"
tags: "{{ ec2_instance_create_delete_tags | default(omit) }}"
wait: "{{ ec2_instance_create_delete_wait_for_state }}"
name: "{{ manage_ec2_instance_instance_name }}"
instance_type: "{{ manage_ec2_instance_instance_type }}"
image_id: "{{ manage_ec2_instance_ami_id }}"
key_name: "{{ manage_ec2_instance_key_name | default(omit) }}"
security_groups: "{{ manage_ec2_instance_associate_security_groups | default(omit, true) }}"
vpc_subnet_id: "{{ manage_ec2_instance_vpc_subnet_id | default(omit) }}"
tags: "{{ manage_ec2_instance_tags | default(omit) }}"
wait: "{{ manage_ec2_instance_wait_for_state }}"
register: ec2_instance

- name: Allocate and associate Elastic IP if enabled
when: ec2_instance_create_delete_associate_eip is true
when: manage_ec2_instance_associate_eip is true
amazon.aws.ec2_eip:
device_id: "{{ ec2_instance.instance_ids[0] }}"
state: present
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
- name: Get instance info with provided name
amazon.aws.ec2_instance_info:
filters:
tag:Name: "{{ ec2_instance_create_delete_instance_name }}"
tag:Name: "{{ manage_ec2_instance_instance_name }}"
instance-state-name: ["pending", "running", "stopping", "stopped"]
register: ec2_info_result

Expand All @@ -18,12 +18,12 @@
when: ec2_info_result.instances | length > 0
amazon.aws.ec2_instance:
state: terminated
wait: "{{ ec2_instance_create_delete_wait_for_state }}"
wait: "{{ manage_ec2_instance_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
when: manage_ec2_instance_key_name is defined and manage_ec2_instance_key_name | length > 0
amazon.aws.ec2_key:
name: "{{ ec2_instance_create_delete_key_name }}"
name: "{{ manage_ec2_instance_key_name }}"
state: absent
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@
block:
- name: Include create operations
ansible.builtin.include_tasks: ec2_instance_create_operations.yml
when: ec2_instance_create_delete_operation == 'create'
when: manage_ec2_instance_operation == 'create'

- name: Include delete operations
ansible.builtin.include_tasks: ec2_instance_delete_operations.yml
when: ec2_instance_create_delete_operation == 'delete'
when: manage_ec2_instance_operation == 'delete'

This file was deleted.

2 changes: 2 additions & 0 deletions tests/integration/targets/test_manage_ec2_instance/aliases
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
cloud/aws
role/manage_ec2_instance
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
---
- name: Integration tests for ec2_networking_resources role
- name: Integration tests for manage_ec2_instance role
module_defaults:
group/aws:
aws_access_key: "{{ aws_access_key }}"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
amazon.aws.ec2_security_group:
vpc_id: "{{ vpc.vpc.id }}"
name: "{{ test_security_group_name }}"
description: "Test security group for cloud.aws_ops.ec2_instance_create_delete role"
description: "Test security group for cloud.aws_ops.manage_ec2_instance role"

- name: Create internet gateway
amazon.aws.ec2_vpc_igw:
Expand Down
Loading

0 comments on commit 79e661d

Please sign in to comment.