Updates to inventory generation and instance configuration

This change makes few updates to inventory generation mechanism,
where generated inventory.ini files are placed, and how they are
used during the deployment.
- move inventory generation out of create-stack role and place it
in newly created role, generate-inventory, for provisioner Heat.
This will help us when we work on generating generic inventory
by provisioners since both provisioners now use separate roles
to generate inventory.
- move tasks in configure-instances located in provisioner to
installer configure-targethosts playbook
- place generated inventory.ini under config_path to have it in
same place as before.
- symlink config_path/inventory.ini as engine/inventory/inventory.ini
instead of copying it so we have single source of inventory.

Change-Id: I53cd4b1e1c1f641afbb992cf08818bdfa4ab767e
diff --git a/playbooks/configure-instances.yaml b/playbooks/configure-instances.yaml
deleted file mode 100644
index 7ad7a59..0000000
--- a/playbooks/configure-instances.yaml
+++ /dev/null
@@ -1,47 +0,0 @@
----
-# ============LICENSE_START=======================================================
-#  Copyright (C) 2019 The Nordix Foundation. All rights reserved.
-# ================================================================================
-# Licensed under the Apache License, Version 2.0 (the "License");
-# you may not use this file except in compliance with the License.
-# You may obtain a copy of the License at
-#
-#      http://www.apache.org/licenses/LICENSE-2.0
-#
-# Unless required by applicable law or agreed to in writing, software
-# distributed under the License is distributed on an "AS IS" BASIS,
-# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
-# See the License for the specific language governing permissions and
-# limitations under the License.
-#
-# SPDX-License-Identifier: Apache-2.0
-# ============LICENSE_END=========================================================
-
-- hosts: jumphost
-  gather_facts: false
-  become: false
-
-  tasks:
-    - name: Install python and python-dev
-      script: files/install-python.sh
-
-    - name: Copy SSH keys to jumphost for target node access
-      copy:
-        src: "{{ item }}"
-        dest: "/root/.ssh/"
-        owner: "root"
-        group: "root"
-        mode: 0600
-      with_items:
-        - "/home/{{ local_user }}/.ssh/id_rsa"
-        - "/home/{{ local_user }}/.ssh/id_rsa.pub"
-
-- hosts: baremetal
-  gather_facts: false
-  become: false
-
-  tasks:
-    - name: Install python and python-dev
-      script: files/install-python.sh
-
-# vim: set ts=2 sw=2 expandtab:
diff --git a/playbooks/files/install-python.sh b/playbooks/files/install-python.sh
deleted file mode 100755
index f0b5337..0000000
--- a/playbooks/files/install-python.sh
+++ /dev/null
@@ -1,49 +0,0 @@
-#!/bin/bash
-
-# ============LICENSE_START=======================================================
-#  Copyright (C) 2019 The Nordix Foundation. All rights reserved.
-# ================================================================================
-# Licensed under the Apache License, Version 2.0 (the "License");
-# you may not use this file except in compliance with the License.
-# You may obtain a copy of the License at
-#
-#      http://www.apache.org/licenses/LICENSE-2.0
-#
-# Unless required by applicable law or agreed to in writing, software
-# distributed under the License is distributed on an "AS IS" BASIS,
-# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
-# See the License for the specific language governing permissions and
-# limitations under the License.
-#
-# SPDX-License-Identifier: Apache-2.0
-# ============LICENSE_END=========================================================
-
-set -o errexit
-set -o pipefail
-
-# NOTE: due to stack creation issues on public cloud, installation of python
-# and python-dev has been moved out of heat templates. The reason for this is
-# that during initial boot of the instances, Ansible prerequisites python and
-# python-dev are installed using boot script. Due to the reasons we can not
-# explain and perhaps because of network issues on public cloud, apt fails,
-# complaining about corruption - checksum mismatch. In turn, problematic
-# instance(s) can not send completion signal at the end of boot phase, resulting
-# in timeouts in stack creation thus complete failure of deployments.
-
-source /etc/os-release || source /usr/lib/os-release
-case ${ID,,} in
-  ubuntu|debian)
-    export DEBIAN_FRONTEND=noninteractive
-    sudo -H -E apt update -q=3
-    sudo -H -E apt install -y -q=3 python python-dev
-    ;;
-  rhel|fedora|centos)
-    sudo yum install -y python python-devel
-    ;;
-  *)
-    echo "ERROR: Supported package manager not found.  Supported: apt, dnf, yum, zypper"
-    exit 1
-    ;;
-esac
-
-# vim: set ts=2 sw=2 expandtab:
diff --git a/playbooks/roles/create-stack/defaults/main.yml b/playbooks/generate-inventory.yml
similarity index 89%
rename from playbooks/roles/create-stack/defaults/main.yml
rename to playbooks/generate-inventory.yml
index 2a4c55e..5a1538c 100644
--- a/playbooks/roles/create-stack/defaults/main.yml
+++ b/playbooks/generate-inventory.yml
@@ -16,6 +16,12 @@
 #
 # SPDX-License-Identifier: Apache-2.0
 # ============LICENSE_END=========================================================
-stack_name: "{{ lookup('env', 'STACK_NAME') | default('nordix-' + deploy_scenario + '-custom', true) }}"
+
+- hosts: localhost
+  connection: local
+  gather_facts: true
+
+  roles:
+    - role: generate-inventory
 
 # vim: set ts=2 sw=2 expandtab:
diff --git a/playbooks/roles/create-stack/tasks/create-stack.yml b/playbooks/roles/create-stack/tasks/create-stack.yml
deleted file mode 100644
index 31ee79c..0000000
--- a/playbooks/roles/create-stack/tasks/create-stack.yml
+++ /dev/null
@@ -1,76 +0,0 @@
----
-# ============LICENSE_START=======================================================
-#  Copyright (C) 2019 The Nordix Foundation. All rights reserved.
-# ================================================================================
-# Licensed under the Apache License, Version 2.0 (the "License");
-# you may not use this file except in compliance with the License.
-# You may obtain a copy of the License at
-#
-#      http://www.apache.org/licenses/LICENSE-2.0
-#
-# Unless required by applicable law or agreed to in writing, software
-# distributed under the License is distributed on an "AS IS" BASIS,
-# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
-# See the License for the specific language governing permissions and
-# limitations under the License.
-#
-# SPDX-License-Identifier: Apache-2.0
-# ============LICENSE_END=========================================================
-
-- name: Set keypair name
-  set_fact:
-    keypair_name: "keypair-{{ stack_name | regex_replace('\\.', '_') }}"
-
-- name: Delete keypair '{{ keypair_name }}'
-  os_keypair:
-    name: "{{ keypair_name }}"
-    state: absent
-  ignore_errors: yes
-
-- name: Delete stack '{{ stack_name }}'
-  os_stack:
-    name: "{{ stack_name }}"
-    state: absent
-    wait: yes
-  ignore_errors: no
-  register: stack_delete_output
-
-- name: Log stack delete output to console
-  debug:
-    msg: "{{ stack_delete_output }}"
-
-- name: Create keypair '{{ keypair_name }}'
-  os_keypair:
-    name: "{{ keypair_name }}"
-    state: present
-    public_key_file: "{{ ansible_env.HOME }}/.ssh/id_rsa.pub"
-
-- name: Create stack '{{ stack_name }}'
-  os_stack:
-    name: "{{ stack_name }}"
-    state: present
-    template: "{{ engine_cache }}/config/heat-template.yaml"
-    environment:
-      - "{{ engine_cache }}/config/heat-environment.yaml"
-    parameters:
-      keypair: "{{ keypair_name }}"
-    wait: yes
-  ignore_errors: no
-  register: stack_create_output
-
-- name: Log stack create output to console
-  debug:
-    msg: "{{ stack_create_output }}"
-
-- name: Get stack list
-  command: "openstack stack list --property name={{ stack_name }}"
-  when: stack_create_output
-  register: stack_list_output
-  environment:
-    PATH: "{{ engine_venv }}/bin"
-
-- name: Log stack list output to console
-  debug:
-    msg: "{{ stack_list_output.stdout_lines }}"
-
-# vim: set ts=2 sw=2 expandtab:
diff --git a/playbooks/roles/create-stack/tasks/main.yml b/playbooks/roles/create-stack/tasks/main.yml
index 7db242d..31ee79c 100644
--- a/playbooks/roles/create-stack/tasks/main.yml
+++ b/playbooks/roles/create-stack/tasks/main.yml
@@ -17,11 +17,60 @@
 # SPDX-License-Identifier: Apache-2.0
 # ============LICENSE_END=========================================================
 
-# create stack
-- include: create-stack.yml
+- name: Set keypair name
+  set_fact:
+    keypair_name: "keypair-{{ stack_name | regex_replace('\\.', '_') }}"
 
-# generate kubespray inventory from heat output
-- include: generate-kubespray-inventory.yml
-  when: installer_type == 'kubespray'
+- name: Delete keypair '{{ keypair_name }}'
+  os_keypair:
+    name: "{{ keypair_name }}"
+    state: absent
+  ignore_errors: yes
+
+- name: Delete stack '{{ stack_name }}'
+  os_stack:
+    name: "{{ stack_name }}"
+    state: absent
+    wait: yes
+  ignore_errors: no
+  register: stack_delete_output
+
+- name: Log stack delete output to console
+  debug:
+    msg: "{{ stack_delete_output }}"
+
+- name: Create keypair '{{ keypair_name }}'
+  os_keypair:
+    name: "{{ keypair_name }}"
+    state: present
+    public_key_file: "{{ ansible_env.HOME }}/.ssh/id_rsa.pub"
+
+- name: Create stack '{{ stack_name }}'
+  os_stack:
+    name: "{{ stack_name }}"
+    state: present
+    template: "{{ engine_cache }}/config/heat-template.yaml"
+    environment:
+      - "{{ engine_cache }}/config/heat-environment.yaml"
+    parameters:
+      keypair: "{{ keypair_name }}"
+    wait: yes
+  ignore_errors: no
+  register: stack_create_output
+
+- name: Log stack create output to console
+  debug:
+    msg: "{{ stack_create_output }}"
+
+- name: Get stack list
+  command: "openstack stack list --property name={{ stack_name }}"
+  when: stack_create_output
+  register: stack_list_output
+  environment:
+    PATH: "{{ engine_venv }}/bin"
+
+- name: Log stack list output to console
+  debug:
+    msg: "{{ stack_list_output.stdout_lines }}"
 
 # vim: set ts=2 sw=2 expandtab:
diff --git a/playbooks/roles/create-stack/tasks/generate-kubespray-inventory.yml b/playbooks/roles/generate-inventory/tasks/generate-kubespray-inventory.yml
similarity index 78%
rename from playbooks/roles/create-stack/tasks/generate-kubespray-inventory.yml
rename to playbooks/roles/generate-inventory/tasks/generate-kubespray-inventory.yml
index 9093a4c..f21d0ef 100644
--- a/playbooks/roles/create-stack/tasks/generate-kubespray-inventory.yml
+++ b/playbooks/roles/generate-inventory/tasks/generate-kubespray-inventory.yml
@@ -53,19 +53,33 @@
     master_ip: "{{ stack_output_master_ip.stdout }}"
     worker_ip: "{{ stack_output_worker_ip.stdout }}"
 
-- name: Delete outdated kubespray inventory
+# TODO: update jobs and scenarios to ensure inventory.ini is not used from /tmp
+- name: Ensure outdated inventory.ini does not exist
   file:
     path: "{{ item }}"
     state: absent
   with_items:
     - "{{ inventory_path }}/inventory.ini"
     - "/tmp/inventory.ini"
+    - "{{ config_path }}/inventory.ini"
   ignore_errors: yes
 
-- name: Generate kubespray inventory
+- name: Generate Kubespray inventory from Heat stack output
   template:
     src: inventory_kubespray.ini.j2
+    dest: "{{ config_path }}/inventory.ini"
+    mode: 0644
+    force: true
+
+# NOTE: generated inventory is linked from config_path to inventory folder
+# in order to ensure we have single inventory file
+# TODO: update jobs and scenarios to ensure inventory.ini is not used from /tmp
+- name: Link generated inventory.ini to inventory folder
+  file:
+    src: "{{ config_path }}/inventory.ini"
     dest: "{{ item }}"
+    state: link
+    force: true
   with_items:
     - "{{ inventory_path }}/inventory.ini"
     - "/tmp/inventory.ini"
diff --git a/playbooks/roles/create-stack/defaults/main.yml b/playbooks/roles/generate-inventory/tasks/main.yml
similarity index 86%
copy from playbooks/roles/create-stack/defaults/main.yml
copy to playbooks/roles/generate-inventory/tasks/main.yml
index 2a4c55e..2384384 100644
--- a/playbooks/roles/create-stack/defaults/main.yml
+++ b/playbooks/roles/generate-inventory/tasks/main.yml
@@ -16,6 +16,9 @@
 #
 # SPDX-License-Identifier: Apache-2.0
 # ============LICENSE_END=========================================================
-stack_name: "{{ lookup('env', 'STACK_NAME') | default('nordix-' + deploy_scenario + '-custom', true) }}"
+
+# generate kubespray inventory from Heat stack output
+- include: generate-kubespray-inventory.yml
+  when: installer_type == 'kubespray'
 
 # vim: set ts=2 sw=2 expandtab:
diff --git a/playbooks/roles/create-stack/templates/inventory_kubespray.ini.j2 b/playbooks/roles/generate-inventory/templates/inventory_kubespray.ini.j2
similarity index 100%
rename from playbooks/roles/create-stack/templates/inventory_kubespray.ini.j2
rename to playbooks/roles/generate-inventory/templates/inventory_kubespray.ini.j2
diff --git a/provision.sh b/provision.sh
index 9ccdbeb..79df169 100755
--- a/provision.sh
+++ b/provision.sh
@@ -41,10 +41,13 @@
     -i "${ENGINE_PATH}/engine/inventory/localhost.ini" \
     "${PROVISIONER_ROOT_DIR}/playbooks/main.yml"
 
+echo "Info: Generate Ansible inventory"
+echo "-------------------------------------------------------------------------"
 cd "${ENGINE_PATH}"
 ansible-playbook "${ENGINE_ANSIBLE_PARAMS[@]}" \
-    -i "${ENGINE_PATH}/engine/inventory/inventory.ini" \
-    "${PROVISIONER_ROOT_DIR}/playbooks/configure-instances.yaml"
+    -i "${ENGINE_PATH}/engine/inventory/localhost.ini" \
+    "${PROVISIONER_ROOT_DIR}/playbooks/generate-inventory.yml"
+
 echo "-------------------------------------------------------------------------"
 echo "Info: Nodes are provisioned using OpenStack Heat!"
 echo "-------------------------------------------------------------------------"