148 lines
4.5 KiB
YAML
148 lines
4.5 KiB
YAML
- name: Ansible delete file glob
|
|
find:
|
|
paths: /tmp/
|
|
file_type: directory
|
|
patterns: "files-{{ group_names[0] }}"
|
|
register: files_to_delete
|
|
|
|
- name: Ansible remove file glob
|
|
file:
|
|
path: "{{ item.path }}"
|
|
state: absent
|
|
with_items: "{{ files_to_delete.files }}"
|
|
|
|
- name: Copy files
|
|
copy:
|
|
src: "roles/main_infra/files/"
|
|
dest: "/tmp/files-{{ group_names[0] }}/"
|
|
|
|
- name: Local or remote backend selector (remote)
|
|
template:
|
|
src: roles/main_infra/templates/remote-backend-selector.tf.j2
|
|
dest: "/tmp/files-{{ group_names[0] }}/remote-backend-selector.tf"
|
|
when:
|
|
- backend|bool == true
|
|
|
|
- name: Local or remote backend selector (local)
|
|
file:
|
|
state: absent
|
|
dest: "/tmp/files-{{ group_names[0] }}/"
|
|
when:
|
|
- not backend | default ('false') | bool
|
|
|
|
- name: Generating variables file
|
|
template:
|
|
src: roles/main_infra/templates/terraform.tfvars.j2
|
|
dest: "/tmp/files-{{ group_names[0] }}/terraform.tfvars"
|
|
|
|
- name: Generating backend file
|
|
template:
|
|
src: roles/main_infra/templates/backend.tfvars.j2
|
|
dest: "/tmp/files-{{ group_names[0] }}/backend.tfvars"
|
|
when: backend | bool
|
|
|
|
# This is due to the TF0.11-12 bug which do not allow to completely destroy resources if interpolation syntax is used in outputs.tf at edge cases
|
|
- name: Check if outputs.tf exists
|
|
stat:
|
|
path: "/tmp/files-{{ group_names[0] }}/outputs.tf"
|
|
register: outputs_stat
|
|
|
|
- name: Temporarily remove outputs.tf file
|
|
command: "mv /tmp/files-{{ group_names[0] }}/outputs.tf /tmp/files-{{ group_names[0] }}/outputs.tf.backup"
|
|
when: outputs_stat.stat.exists
|
|
|
|
- name: Check if .terraform folder exists
|
|
stat:
|
|
path: "/tmp/files-{{ group_names[0] }}/.terraform/"
|
|
register: stat_result
|
|
|
|
- name: Remove .terraform folder
|
|
file:
|
|
path: "/tmp/files-{{ group_names[0] }}/.terraform/"
|
|
state: absent
|
|
when: stat_result.stat.exists
|
|
|
|
- name: Terraform plan to destroy main infra
|
|
shell: "echo yes | {{ terraform_location }} {{ item }}"
|
|
args:
|
|
chdir: "/tmp/files-{{ group_names[0] }}/"
|
|
with_items:
|
|
- "init {{ '-backend-config=backend.tfvars' if backend|bool else '' }}"
|
|
- plan -destroy -out terraform.tfplan
|
|
- show -no-color terraform.tfplan
|
|
register: tf_plan
|
|
|
|
- name: Terraform show destroy plan
|
|
debug:
|
|
var: tf_plan.results[2].stdout_lines
|
|
|
|
- name: User prompt
|
|
pause:
|
|
prompt: "Are you absolutely sure you want to execute the destruction plan shown above? [False]"
|
|
register: user_answer
|
|
until: user_answer.user_input | lower in conditional
|
|
retries: 10000
|
|
delay: 1
|
|
vars:
|
|
conditional: ['yes','no','true','false']
|
|
when: inventory_hostname == groups['all'][0]
|
|
|
|
- name: Terraform destroy
|
|
shell: "{{ terraform_location }} destroy -auto-approve"
|
|
args:
|
|
chdir: "/tmp/files-{{ group_names[0] }}"
|
|
when: hostvars[groups['all'][0]].user_answer.user_input | bool
|
|
|
|
- name: Delete vars from parameter store
|
|
include: parameter_store.yml
|
|
|
|
- name: Check if outputs.tf.backup exists
|
|
stat:
|
|
path: "/tmp/files-{{ group_names[0] }}/outputs.tf.backup"
|
|
register: outputs_backup_stat
|
|
|
|
- name: Get back outputs.tf file
|
|
command: "mv /tmp/files-{{ group_names[0] }}/outputs.tf.backup /tmp/files-{{ group_names[0] }}/outputs.tf"
|
|
when: outputs_backup_stat.stat.exists
|
|
|
|
- name: User prompt
|
|
pause:
|
|
prompt: "Do you want to delete S3 bucket with state file and DynamoDB attached to it also? [Yes/No] Default: No"
|
|
register: user_answer
|
|
until: user_answer.user_input | lower in conditional
|
|
retries: 10000
|
|
delay: 1
|
|
vars:
|
|
conditional: ['yes','no','true','false']
|
|
when: inventory_hostname == groups['all'][0]
|
|
|
|
- name: Destroy S3 bucket
|
|
s3_bucket:
|
|
name: "{{ prefix }}-{{ bucket }}"
|
|
state: absent
|
|
force: yes
|
|
profile: "{{ profile }}"
|
|
aws_access_key: "{{ access_key }}"
|
|
aws_secret_key: "{{ secret_key }}"
|
|
region: "{{ region }}"
|
|
vars:
|
|
access_key: "{{ aws_access_key|default(omit) }}"
|
|
secret_key: "{{ aws_secret_key|default(omit) }}"
|
|
profile: "{{ aws_profile|default(omit) }}"
|
|
region: "{{ aws_region|default(omit) }}"
|
|
when: hostvars[groups['all'][0]].user_answer.user_input | bool
|
|
|
|
- dynamodb_table:
|
|
name: "{{ prefix }}-{{ dynamodb_table }}"
|
|
state: absent
|
|
profile: "{{ profile }}"
|
|
aws_access_key: "{{ access_key }}"
|
|
aws_secret_key: "{{ secret_key }}"
|
|
region: "{{ region }}"
|
|
vars:
|
|
access_key: "{{ aws_access_key|default(omit) }}"
|
|
secret_key: "{{ aws_secret_key|default(omit) }}"
|
|
profile: "{{ aws_profile|default(omit) }}"
|
|
region: "{{ aws_region|default(omit) }}"
|
|
when: hostvars[groups['all'][0]].user_answer.user_input | bool
|