Skip to content

Commit

Permalink
[FEATURE] ec2 to ec2_instance (#129)
Browse files Browse the repository at this point in the history
  • Loading branch information
FredButler authored Feb 24, 2023
1 parent a763982 commit 2fb0cd1
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 20 deletions.
6 changes: 3 additions & 3 deletions clean/tasks/aws.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,18 @@
- name: clean/aws | clean vms
block:
- name: clean/aws | Remove instances termination protection
ec2:
ec2_instance:
aws_access_key: "{{cluster_vars[buildenv].aws_access_key}}"
aws_secret_key: "{{cluster_vars[buildenv].aws_secret_key}}"
region: "{{ cluster_vars.region }}"
state: "{{ item.instance_state }}"
termination_protection: "no"
termination_protection: false
instance_ids: ["{{ item.instance_id }}"]
ec2_url: "{{ cluster_vars.aws_endpoint_url | default(omit) }}"
with_items: "{{ hosts_to_clean | json_query(\"[].{instance_id:instance_id, instance_state: instance_state}\") | default([]) }}"

- name: clean/aws | Delete VMs
ec2:
ec2_instance:
aws_access_key: "{{cluster_vars[buildenv].aws_access_key}}"
aws_secret_key: "{{cluster_vars[buildenv].aws_secret_key}}"
region: "{{ cluster_vars.region }}"
Expand Down
2 changes: 1 addition & 1 deletion cluster_hosts/tasks/get_cluster_hosts_state_aws.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
ec2_instance_info:
filters:
"tag:cluster_name": "{{cluster_name}}"
"instance-state-name": ["running", "stopped"]
"instance-state-name": ["running", "pending", "stopped"]
aws_access_key: "{{cluster_vars[buildenv].aws_access_key}}"
aws_secret_key: "{{cluster_vars[buildenv].aws_secret_key}}"
region: "{{cluster_vars.region}}"
Expand Down
2 changes: 1 addition & 1 deletion config/tasks/create_dns_a.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

- block:
- name: config/dns/a/nsupdate | create/update A records in bind (nsupdate)
nsupdate:
community.general.nsupdate:
key_name: "{{cluster_vars[buildenv].nsupdate_cfg.key_name | default(bind9[buildenv].key_name)}}"
key_secret: "{{cluster_vars[buildenv].nsupdate_cfg.key_secret | default(bind9[buildenv].key_secret)}}"
server: "{{cluster_vars[buildenv].nsupdate_cfg.server | default(bind9[buildenv].server)}}"
Expand Down
45 changes: 31 additions & 14 deletions create/tasks/create_aws.yml
Original file line number Diff line number Diff line change
Expand Up @@ -33,28 +33,45 @@
loop: "{{ cluster_hosts_target_denormalised_by_volume | selectattr('auto_volume.src', 'defined') | list }}"

- name: create/aws | Create EC2 VMs asynchronously
ec2:
amazon.aws.ec2_instance:
aws_access_key: "{{cluster_vars[buildenv].aws_access_key}}"
aws_secret_key: "{{cluster_vars[buildenv].aws_secret_key}}"
region: "{{cluster_vars.region}}"
key_name: "{{cluster_vars[buildenv].key_name}}"
instance_type: "{{item.flavor}}"
instance_profile_name: "{{cluster_vars[buildenv].hosttype_vars[item.hosttype].instance_profile_name | default(cluster_vars.instance_profile_name | default(omit))}}"
instance_role: "{{cluster_vars[buildenv].hosttype_vars[item.hosttype].instance_profile_name | default(cluster_vars.instance_profile_name | default(omit))}}"
instance_initiated_shutdown_behavior: "{{cluster_vars[buildenv].hosttype_vars[item.hosttype].instance_initiated_shutdown_behavior | default(omit)}}"
spot_price: "{{cluster_vars[buildenv].hosttype_vars[item.hosttype].spot.spot_price | default(omit)}}"
spot_wait_timeout: "{{cluster_vars[buildenv].hosttype_vars[item.hosttype].spot.spot_wait_timeout | default(10800)}}" #3 hours
spot_launch_group: "{{cluster_vars[buildenv].hosttype_vars[item.hosttype].spot.spot_launch_group | default(omit)}}"
spot_type: "{{cluster_vars[buildenv].hosttype_vars[item.hosttype].spot.spot_type | default('persistent')}}"
image: "{{ item.image }}"
# spot_price: "{{cluster_vars[buildenv].hosttype_vars[item.hosttype].spot.spot_price | default(omit)}}" # spot instance tpyes cannot be used with the ec2_instance module at this time
# spot_wait_timeout: "{{cluster_vars[buildenv].hosttype_vars[item.hosttype].spot.spot_wait_timeout | default(10800)}}" #3 hours
# spot_launch_group: "{{cluster_vars[buildenv].hosttype_vars[item.hosttype].spot.spot_launch_group | default(omit)}}"
# spot_type: "{{cluster_vars[buildenv].hosttype_vars[item.hosttype].spot.spot_type | default('persistent')}}"
image_id: "{{ item.image }}"
vpc_subnet_id: "{{item.vpc_subnet_id}}"
assign_public_ip: "{{cluster_vars.assign_public_ip}}"
group: "{{ cluster_vars.secgroups_existing }} {%- if cluster_vars.secgroup_new | length > 0 -%} + {{ ([r__ec2_group.group_name | default()] | default())}} {%- endif -%}"
network:
assign_public_ip: "{{cluster_vars.assign_public_ip | bool}}"
security_groups: "{{ cluster_vars.secgroups_existing }} {%- if cluster_vars.secgroup_new | length > 0 -%} + {{ ([r__ec2_group.group_name | default()] | default())}} {%- endif -%}"
wait: yes
instance_tags: "{{ _instance_tags | combine(cluster_vars.custom_tagslabels | default({})) }}"
state: running
tags: "{{ _instance_tags | combine(cluster_vars.custom_tagslabels | default({})) }}"
termination_protection: "{{cluster_vars[buildenv].termination_protection}}"
user_data: "{{ cluster_vars.user_data | default(omit) }}"
volumes: "{{ item.auto_volumes | selectattr('src', 'undefined') | list | default([]) }}"
count_tag: { Name: "{{item.hostname}}" }
volumes: |
{%- set res = [] -%}
{%- for vol in item.auto_volumes -%}
{%- if 'src' not in vol -%}
{%- if 'volume_type' in vol and vol.volume_type == 'ephemeral' -%}
{%- set _dummy = res.append({ 'device_name': vol.device_name, 'volume_name': vol.ephemeral }) -%}
{%- else -%}
{%- set _dummy = res.append({ 'device_name': vol.device_name, 'ebs': {'volume_type': vol.volume_type, 'volume_size': vol.volume_size } }) -%}
{%- if 'iops' in vol -%} {%- set _dummy = res[res|length-1].ebs.update({'iops': vol.iops}) -%} {%- endif -%}
{%- if 'snapshot' in vol -%} {%- set _dummy = res[res|length-1].ebs.update({'snapshot_id': vol.snapshot}) -%} {%- endif -%}
{%- if 'encrypted' in vol -%} {%- set _dummy = res[res|length-1].ebs.update({'encrypted': vol.encrypted}) -%} {%- endif -%}
{%- if 'delete_on_termination' in vol -%} {%- set _dummy = res[res|length-1].ebs.update({'delete_on_termination': vol.delete_on_termination}) -%} {%- endif -%}
{%- endif -%}
{%- endif -%}
{%- endfor %}
{{ res }}
filters: { "tag:Name": "{{item.hostname}}", "instance-state-name": ["running", "pending"] }
exact_count: 1
ec2_url: "{{ cluster_vars.aws_endpoint_url | default(omit) }}"
vars:
Expand Down Expand Up @@ -82,8 +99,8 @@
retries: 300
with_items: "{{r__ec2.results}}"

# - name: create/aws | r__async_status__ec2.results
# debug: msg={{r__async_status__ec2.results}}
- name: create/aws | r__async_status__ec2.results
debug: msg={{r__async_status__ec2.results}}

- name: create/aws | Set a fact containing the newly-created hosts
set_fact:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
- block:
- name: Preflight check | get ec2_instance_info for current disk information
ec2_instance_info:
filters: { "instance-state-name": [ "running", "stopped" ], "tag:cluster_name": "{{cluster_name}}", "tag:lifecycle_state": "current" }
filters: { "instance-state-name": [ "running", "pending", "stopped" ], "tag:cluster_name": "{{cluster_name}}", "tag:lifecycle_state": "current" }
aws_access_key: "{{cluster_vars[buildenv].aws_access_key}}"
aws_secret_key: "{{cluster_vars[buildenv].aws_secret_key}}"
region: "{{cluster_vars.region}}"
Expand Down

0 comments on commit 2fb0cd1

Please sign in to comment.