From 3c81a6ad667b9b8b395b5b5bbd8ae9a77479d986 Mon Sep 17 00:00:00 2001
From: Vince Power <vince.power@gmail.com>
Date: Tue, 17 Mar 2020 21:50:12 +0100
Subject: [PATCH] Checking if generated key were created or not before deployment (#1355)

---
 ansible/cloud_providers/azure_infrastructure_deployment.yml |   17 +++++++++++++++--
 ansible/configs/aro/env_vars.yml                            |    4 ++++
 ansible/cloud_providers/gcp_infrastructure_deployment.yml   |   11 +++++++++--
 3 files changed, 28 insertions(+), 4 deletions(-)

diff --git a/ansible/cloud_providers/azure_infrastructure_deployment.yml b/ansible/cloud_providers/azure_infrastructure_deployment.yml
index 25194b4..0803615 100644
--- a/ansible/cloud_providers/azure_infrastructure_deployment.yml
+++ b/ansible/cloud_providers/azure_infrastructure_deployment.yml
@@ -64,6 +64,13 @@
         name: "{{ az_resource_group }}"
         location: "{{ azure_region }}"
 
+    - name: Check for auto-generated SSH Key
+      stat:
+        path: "{{output_dir}}/{{env_authorized_key}}"
+      register: env_authorized_key_status
+      tags:
+        - check_for_env_keys
+
     - name: Get SSH public key
       set_fact:
         ssh_key: "~/.ssh/{{key_name}}.pem"
@@ -72,7 +79,7 @@
         - set_existing_ssh_key
         - must
         - create_inventory
-      when: not set_env_authorized_key | bool
+      when: not env_authorized_key_status.stat.exists
 
     - name: Get SSH public key
       set_fact:
@@ -82,7 +89,7 @@
         - set_generated_ssh_key
         - must
         - create_inventory
-      when: set_env_authorized_key | bool
+      when: env_authorized_key_status.stat.exists
 
     - name: Setting windows_password variable
       set_fact:
@@ -90,6 +97,11 @@
       when:
         - windows_password is not defined
         - generated_windows_password is defined
+
+    - name: Check if the parameter file exists
+      stat:
+        path: "{{params_dest}}"
+      register: params_dest_status
 
     - name: Build parameter file
       copy:
@@ -101,6 +113,7 @@
             "guid": { "value": "{{guid}}"},
           }
         dest: "{{params_dest}}"
+      when: not params_dest_status.stat.exists
       tags:
         - azure_infrastructure_deployment
         - validate_azure_template
diff --git a/ansible/cloud_providers/gcp_infrastructure_deployment.yml b/ansible/cloud_providers/gcp_infrastructure_deployment.yml
index 20258b8..4a8aa80 100644
--- a/ansible/cloud_providers/gcp_infrastructure_deployment.yml
+++ b/ansible/cloud_providers/gcp_infrastructure_deployment.yml
@@ -19,6 +19,13 @@
         msg: you need Google Cloud SDK installed
       when: gcloud_result is failed
 
+    - name: Check for auto-generated SSH Key
+      stat:
+        path: "{{output_dir}}/{{env_authorized_key}}"
+      register: env_authorized_key_status
+      tags:
+        - check_for_env_keys
+
     - name: Get SSH public key
       set_fact:
         ssh_key: "~/.ssh/{{key_name}}.pem"
@@ -27,7 +34,7 @@
         - set_existing_ssh_key
         - must
         - create_inventory
-      when: not set_env_authorized_key | bool
+      when: not env_authorized_key_status.stat.exists
 
     - name: Get SSH public key
       set_fact:
@@ -37,7 +44,7 @@
         - set_generated_ssh_key
         - must
         - create_inventory
-      when: set_env_authorized_key | bool
+      when: env_authorized_key_status.stat.exists
 
     - name: Set the destination for the template
       set_fact:
diff --git a/ansible/configs/aro/env_vars.yml b/ansible/configs/aro/env_vars.yml
index 0f6b382..51c1a47 100644
--- a/ansible/configs/aro/env_vars.yml
+++ b/ansible/configs/aro/env_vars.yml
@@ -6,6 +6,10 @@
 az_destroy_method: resource_group
 az_resource_group: "{{ project_tag }}"
 
+# Will not autogenerate SSH Keys
+set_env_authorized_key: false
+env_authorized_key: "{{guid}}key"
+
 # Setting the key_name and ssh_keyfile
 key_name: id_rsa
 

--
Gitblit v1.9.3