From 9aeeb0757cbe8004fd58c96da9822f62afc6ee9f Mon Sep 17 00:00:00 2001 From: Arsenii Petrovich Date: Tue, 26 Feb 2019 21:53:54 +0300 Subject: [PATCH] fixed bugs for attached RDS --- .gitignore | 2 +- README.md | 10 +++++++++- attach_existing_rds.yml | 4 ++++ group_vars/all.yml.example | 1 - roles/attach_existing_rds/tasks/main.yml | 4 ++-- roles/main_infra/files/rds.tf | 1 - roles/main_infra/files/variables.tf | 1 - roles/main_infra/tasks/main.yml | 4 ++-- roles/main_infra/templates/terraform.tfvars.j2 | 1 - 9 files changed, 18 insertions(+), 10 deletions(-) diff --git a/.gitignore b/.gitignore index 0c13615..ab28adb 100644 --- a/.gitignore +++ b/.gitignore @@ -10,7 +10,7 @@ roles/main_infra/files/backend.tfvars roles/main_infra/files/remote-backend-selector.tf roles/main_infra/files/terraform.tfvars - +*.backup # Sensitive information /*.privkey diff --git a/README.md b/README.md index 35003c7..8d60796 100644 --- a/README.md +++ b/README.md @@ -189,8 +189,16 @@ You can easily manipulate your deployment from any machine with sufficient prere ## Attaching the existing RDS instance to the current deployment -In some cases you may want not to create a new database, but to add the existing one to use within the deployment. In order to do that configure all the proper values at `group_vars/all.yml` including yours DB ID and name and execute the `ansible-playbook attach_existing_rds.yml` command. This will add the current DB instance into TF managed resource group. After that run `ansible-playbook deploy.yml` as usually. +In some cases you may want not to create a new database, but to add the existing one to use within the deployment. In order to do that configure all the proper values at `group_vars/all.yml` including yours DB ID and name and execute the `ansible-playbook attach_existing_rds.yml` command. This will add the current DB instance into Terraform-managed resource group. After that run `ansible-playbook deploy.yml` as usually. +Note 1: that this action includes creating the S3 and DynamoDB (if `backend` variable is set to `true`) to store Terraform state files. + +Note 2: the actual name of your resource must include prefix that you will use in this deployment! +Example: + Real resource: tf-poa + prefix variable: tf + db_id variable: poa + ## Common Errors and Questions ### S3: 403 error during provisioning diff --git a/attach_existing_rds.yml b/attach_existing_rds.yml index e5049df..f8a2aed 100644 --- a/attach_existing_rds.yml +++ b/attach_existing_rds.yml @@ -5,6 +5,10 @@ - { role: s3, when: "backend|bool == true" } - { role: dynamodb, when: "backend|bool == true" } - { role: attach_existing_rds } + vars_prompt: + - name: "confirmation" + prompt: "Are you sure you want to attach the existing RDS? If backend variable is set to True, this action includes creating the S3 and DynamoDB table for storing Terraform state files." + default: False environment: AWS_ACCESS_KEY_ID: "{{ aws_access_key }}" AWS_SECRET_ACCESS_KEY: "{{ aws_secret_key }}" diff --git a/group_vars/all.yml.example b/group_vars/all.yml.example index 45f2055..d23c4ea 100644 --- a/group_vars/all.yml.example +++ b/group_vars/all.yml.example @@ -36,7 +36,6 @@ prefix: "sokol" ## The following settings are related to the RDS instance which will be created during deployment db_id: "poa" -db_name: "poa" db_username: "username" db_password: "qwerty12345" db_instance_class: "db.m4.xlarge" diff --git a/roles/attach_existing_rds/tasks/main.yml b/roles/attach_existing_rds/tasks/main.yml index 42c8a85..c472c17 100644 --- a/roles/attach_existing_rds/tasks/main.yml +++ b/roles/attach_existing_rds/tasks/main.yml @@ -24,10 +24,10 @@ when: backend|bool == true #Workaround since terraform module return unexpected error. -- name: Terraform provisioning +- name: Attach RDS to the current deployment shell: "echo yes | {{ terraform_location }} {{ item }}" args: chdir: "roles/main_infra/files" with_items: - "init{{ ' -backend-config=backend.tfvars' if backend|bool == true else '' }}" - - "import aws_db_instance.default {{ db_id }}" + - "import aws_db_instance.default {{ prefix }}-{{ db_id }}" diff --git a/roles/main_infra/files/rds.tf b/roles/main_infra/files/rds.tf index c70e2d9..d153635 100644 --- a/roles/main_infra/files/rds.tf +++ b/roles/main_infra/files/rds.tf @@ -1,5 +1,4 @@ resource "aws_db_instance" "default" { - name = "${var.prefix}-${var.db_name}" identifier = "${var.prefix}-${var.db_id}" engine = "postgres" engine_version = "10.5" diff --git a/roles/main_infra/files/variables.tf b/roles/main_infra/files/variables.tf index 8afc588..4406d58 100644 --- a/roles/main_infra/files/variables.tf +++ b/roles/main_infra/files/variables.tf @@ -45,7 +45,6 @@ variable "chain_network_icon" { } variable "db_id" {} -variable "db_name" {} variable "db_username" {} variable "db_password" {} variable "db_storage" {} diff --git a/roles/main_infra/tasks/main.yml b/roles/main_infra/tasks/main.yml index ace86b9..3eff267 100644 --- a/roles/main_infra/tasks/main.yml +++ b/roles/main_infra/tasks/main.yml @@ -35,11 +35,11 @@ - name: Show Terraform plan debug: - var: tf_plan.stdout_lines + var: tf_plan.results[1].stdout_lines - name: User prompt pause: - prompt: "Are you absolutely sure you want to execute the deployment plan shown above? [Yes/No] Default: No" + prompt: "Are you absolutely sure you want to execute the deployment plan shown above? [False]" register: user_answer - name: Terraform provisioning diff --git a/roles/main_infra/templates/terraform.tfvars.j2 b/roles/main_infra/templates/terraform.tfvars.j2 index a5bb976..e7e8509 100644 --- a/roles/main_infra/templates/terraform.tfvars.j2 +++ b/roles/main_infra/templates/terraform.tfvars.j2 @@ -11,7 +11,6 @@ root_block_size = "{{ root_block_size }}" pool_size = "{{ pool_size }}" db_id = "{{ db_id }}" -db_name = "{{ db_name }}" db_username = "{{ db_username }}" db_password = "{{ db_password }}" db_instance_class = "{{ db_instance_class }}"