Skip to content

Commit ff2efd7

Browse files
committed
Cleanup of Terraform tasks and variables
Signed-off-by: Jim Enright <[email protected]>
1 parent 1edcc79 commit ff2efd7

File tree

4 files changed

+35
-55
lines changed

4 files changed

+35
-55
lines changed

roles/infrastructure/tasks/setup_terraform.yml

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
ansible.builtin.copy:
2424
src: "{{ infra__terraform_template_dir }}/infra/"
2525
dest: "{{ infra__terraform_workspace_dir }}/workspace/infra"
26-
# when: not workdir.stat.exists # TODO: This won't override if any changes to .tf are made. Good or Bad?
26+
# when: not workdir.stat.exists # NOTE: When uncommented won't override workspace dir if any changes to .tf are made.
2727

2828
- name: Copy Terraform infra code to the artefact directory
2929
ansible.builtin.copy:
@@ -59,4 +59,26 @@
5959
ansible.builtin.file:
6060
path: "{{ infra__terraform_workspace_dir }}/workspace/infra"
6161
state: absent
62-
when: infra__terraform_state_storage in ['remote_s3']
62+
when: infra__terraform_state_storage in ['remote_s3']
63+
64+
# If created Utility Instance via Terraform then
65+
# need to get it's info and add to an Ansible host group
66+
- name: Add Utility Instance to host group
67+
when: infra__create_utility_service
68+
block:
69+
- name: Discover the Utility Instance details
70+
community.aws.ec2_instance_info:
71+
region: "{{ infra__region }}"
72+
filters: "{{ __filters | items2dict }}"
73+
vars:
74+
__filters:
75+
- key: "tag:Name"
76+
value: "{{ infra__namespace }}*"
77+
register: __infra_utility_compute_discovered
78+
79+
- name: Add discovered Utility Instance to host group
80+
ansible.builtin.add_host:
81+
hostname: "{{__infra_utility_compute_discovered.instances[0].public_ip_address}}"
82+
ansible_user: "{{ infra__dynamic_inventory_images_default[infra__type][infra__dynamic_inventory_os].user }}"
83+
ansible_ssh_private_key_file: "{{ (infra__private_key_file == '') | ternary(omit, infra__private_key_file) }}"
84+
groupname: cldr_utility

roles/infrastructure/template/aws/infra_aws_storage.tf.j2

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,16 @@
44
resource "aws_s3_bucket" "{{ __aws_storage_location_item }}" {
55
bucket = "{{ __aws_storage_location_item }}"
66
acl = "private"
7+
8+
{% if infra__teardown_deletes_data %}
79
force_destroy = true
10+
{% else %}
11+
{# TODO: How to skip teardown of this resource if infra__teardown_deletes_data is False #}
12+
lifecycle {
13+
# A Terraform destroy of this resource will result in an error message.
14+
prevent_destroy = true
15+
}
16+
{% endif %}
817

918
tags = merge(var.env_tags,{Name = "{{ __aws_storage_location_item }}"})
1019
}

roles/platform/tasks/initialize_setup_aws.yml

Lines changed: 1 addition & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -58,10 +58,6 @@
5858
"tag:Name": "{{ plat__vpc_name }}"
5959
register: __aws_vpc_info
6060

61-
- name: jenright Debug Print Discover AWS VPC
62-
ansible.builtin.debug:
63-
var: __aws_vpc_info
64-
6561
- name: Set fact for AWS VPC ID
6662
when: __aws_vpc_info is defined
6763
ansible.builtin.set_fact:
@@ -72,18 +68,6 @@
7268
ansible.builtin.set_fact:
7369
plat__aws_vpc_id: "{{ infra__aws_vpc_id }}"
7470

75-
76-
- name: jenright Debug Print Set AWS VPC ID by assignment
77-
ansible.builtin.debug:
78-
msg:
79-
- "infra__aws_vpc_id = {{ infra__aws_vpc_id | default('THIS_IS_UNDEFINED')}}"
80-
- "plat__aws_vpc_id = {{ plat__aws_vpc_id }}"
81-
82-
- name: Prompt added by jenright
83-
pause:
84-
prompt: "Check value of plat__aws_vpc_id"
85-
when: debug_terraform | default(false) | bool
86-
8771
- name: Discover AWS VPC Subnets
8872
when: infra__aws_subnet_ids is undefined
8973
block:
@@ -95,7 +79,6 @@
9579
"tag:Name": "{{ plat__namespace }}*"
9680
register: __aws_subnets_info
9781

98-
# NOTE: jenright I had to change this for Terraform created subnets, why?
9982
- name: Set fact for AWS Subnet IDs
10083
when: __aws_subnets_info is defined
10184
ansible.builtin.set_fact:
@@ -110,30 +93,12 @@
11093
ansible.builtin.set_fact:
11194
plat__aws_subnet_ids: "{{ infra__aws_subnet_ids }}"
11295

113-
- name: jenright Debug value of plat__aws_subnet_ids
114-
ansible.builtin.debug:
115-
msg:
116-
- "infra__aws_subnet_ids: {{ infra__aws_subnet_ids | default('BLANK') }}"
117-
- "__aws_subnets_info: {{ __aws_subnets_info | default('BLANK') }}"
118-
- "plat__aws_subnet_ids: {{ plat__aws_subnet_ids | default('BLANK') }}"
119-
120-
- name: Prompt added by jenright
121-
pause:
122-
prompt: "Check value of plat__aws_subnet_ids"
123-
when: debug_terraform | default(false) | bool
124-
12596
- name: Set public subnets for public endpoint access
12697
when: plat__public_endpoint_access
12798
block:
12899
- name: Discover AWS Public Subnets
129100
when: infra__aws_public_subnet_ids is not defined
130-
block:
131-
- name: Print infra__vpc_public_subnets_info names
132-
debug:
133-
msg:
134-
- "{{ item }}"
135-
loop: "{{ infra__vpc_public_subnets_info | map(attribute='name' ) }}"
136-
101+
block:
137102
# TODO: Change infra__vpc_public_subnets_info to plat__vpc_public_subnets_info
138103
- name: Query AWS Public Subnets
139104
amazon.aws.ec2_vpc_subnet_info:
@@ -143,15 +108,8 @@
143108
loop: "{{ infra__vpc_public_subnets_info | map(attribute='name' ) }}"
144109
register: __aws_public_subnet_info
145110

146-
- name: jenright Debug Print Discover AWS Public Subnets
147-
ansible.builtin.debug:
148-
msg:
149-
# - "{{ __aws_public_subnet_info }}"
150-
- "{{ __aws_public_subnet_info.results | community.general.json_query('[*].subnets[*].id') | flatten }}"
151-
152111
- name: Set fact for AWS Public Subnets
153112
ansible.builtin.set_fact:
154-
# plat__aws_public_subnet_ids: "{{ __aws_public_subnet_info.subnets|map(attribute='id')| list }}"
155113
plat__aws_public_subnet_ids: "{{ __aws_public_subnet_info.results | community.general.json_query('[*].subnets[*].id') | flatten }}"
156114
plat__endpoint_access_scheme: "PUBLIC"
157115

@@ -161,15 +119,6 @@
161119
plat__aws_public_subnet_ids: "{{ infra__aws_public_subnet_ids }}"
162120
plat__endpoint_access_scheme: "PUBLIC"
163121

164-
- name: jenright Debug Final Value of plat__aws_public_subnet_ids
165-
ansible.builtin.debug:
166-
var: plat__aws_public_subnet_ids
167-
168-
- name: Prompt added by jenright
169-
pause:
170-
prompt: "Check value of AWS Public Subnets"
171-
when: debug_terraform | default(false) | bool
172-
173122
- name: Discover AWS Security Group for Knox
174123
when: infra__aws_security_group_knox_id is undefined
175124
block:

roles/platform/tasks/setup_aws_terraform_authz.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
ansible.builtin.copy:
2424
src: "{{ plat__terraform_template_dir }}/plat/"
2525
dest: "{{ plat__terraform_workspace_dir }}/workspace/plat"
26-
# when: not workdir.stat.exists # TODO: This won't override if any changes to .tf are made. Good or Bad?
26+
# when: not workdir.stat.exists # NOTE: When uncommented won't override workspace dir if any changes to .tf are made.
2727

2828
- name: Copy Terraform plat code to the artefact directory
2929
ansible.builtin.copy:

0 commit comments

Comments
 (0)