Kolla: Fetch network interface name from baremetal

network_interface and neutron_external_interface variables
are now fetched directly from the corresponding baremetal
nodes, so the hardcoding of them is removed.
These variables are now set in the inventory per host.

deploy-scenario: os-nosdn-nofeature
installer-type: kolla

Change-Id: I5b54d1f8f9fb27396502de33a9aff9cd29c6bc4f
diff --git a/playbooks/generate-inventory.yml b/playbooks/generate-inventory.yml
index fa9b24a..4fa7ffd 100644
--- a/playbooks/generate-inventory.yml
+++ b/playbooks/generate-inventory.yml
@@ -17,6 +17,18 @@
 # SPDX-License-Identifier: Apache-2.0
 # ============LICENSE_END=========================================================
 
+- hosts: baremetal
+  remote_user: root
+  gather_facts: true
+  vars_files:
+    - "{{ engine_path }}/engine/var/versions.yml"
+    - "{{ engine_path }}/engine/var/global.yml"
+    - "{{ pdf_file }}"
+    - "{{ idf_file }}"
+
+  roles:
+    - role: get-baremetal-info
+
 - hosts: localhost
   connection: local
   gather_facts: true
diff --git a/playbooks/roles/generate-inventory/templates/inventory_kolla.ini.j2 b/playbooks/roles/generate-inventory/templates/inventory_kolla.ini.j2
index 0ffa0f6..1faade1 100644
--- a/playbooks/roles/generate-inventory/templates/inventory_kolla.ini.j2
+++ b/playbooks/roles/generate-inventory/templates/inventory_kolla.ini.j2
@@ -4,7 +4,8 @@
 [baremetal]
 {% for node in nodes %}
 {% set ansible_host = node.interfaces[idf.net_config[engine.pxe_network].interface].address %}
-{{ idf[installer_type].hostnames[node.name] }} ansible_host={{ ansible_host }} ansible_user=root
+{% set ansible_hostname = idf[installer_type].hostnames[node.name] %}
+{{ ansible_hostname }} ansible_host={{ ansible_host }} ansible_user=root network_interface={{hostvars[ansible_hostname].mgmt_ifname}} neutron_external_interface={{hostvars[ansible_hostname].neutron_ifname}}
 {% endfor %}
 
 [localhost]
diff --git a/playbooks/roles/get-baremetal-info/tasks/main.yml b/playbooks/roles/get-baremetal-info/tasks/main.yml
new file mode 100644
index 0000000..32df85a
--- /dev/null
+++ b/playbooks/roles/get-baremetal-info/tasks/main.yml
@@ -0,0 +1,48 @@
+---
+# ============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: Print interface information
+  debug:
+    msg: "{{ ansible_interfaces }}"
+
+- name: Get node name from IDF for host "{{ inventory_hostname }}"
+  set_fact:
+    node_name: "{{ item.key }}"
+  with_dict: "{{ (installer_type == 'kolla') | ternary(idf.kolla.hostnames, idf.kubespray.hostnames) }}"
+  when: item.value == inventory_hostname
+
+- name: Set facts for the node "{{ node_name }}"
+  set_fact:
+    node: "{{ nodes | selectattr('name', 'equalto', node_name) | first }}"
+
+- name: Fetch mgmt network interface name
+  set_fact:
+    mgmt_ifname: "{{ (node.interfaces[idf.net_config.mgmt.interface].vlan == 'native') | ternary(interface.device, interface.device + '.' + node.interfaces[idf.net_config.mgmt.interface].vlan) }}"
+  loop: "{{ ansible_interfaces | map('regex_replace', '-', '_') | map('regex_replace', '^', 'ansible_') | map('extract', hostvars[inventory_hostname]) |  selectattr('macaddress','defined') | list }}"
+  loop_control:
+    loop_var: interface
+  when: interface.macaddress == node.interfaces[idf.net_config.mgmt.interface].mac_address
+
+- name: Fetch neutron network interface name
+  set_fact:
+    neutron_ifname: "{{ (node.interfaces[idf.net_config.neutron.interface].vlan == 'native') | ternary(interface.device, interface.device + '.' + node.interfaces[idf.net_config.neutron.interface].vlan) }}"
+  loop: "{{ ansible_interfaces | map('regex_replace', '-', '_') | map('regex_replace', '^', 'ansible_') | map('extract', hostvars[inventory_hostname]) |  selectattr('macaddress','defined') | list }}"
+  loop_control:
+    loop_var: interface
+  when: interface.macaddress == node.interfaces[idf.net_config.neutron.interface].mac_address
diff --git a/provision.sh b/provision.sh
index f29ebcc..0bbf43f 100755
--- a/provision.sh
+++ b/provision.sh
@@ -69,6 +69,8 @@
 echo "-------------------------------------------------------------------------"
 cd ${ENGINE_CACHE}/repos/bifrost/playbooks
 ansible-playbook ${ENGINE_ANSIBLE_PARAMS} \
+  -i inventory/bifrost_inventory.py \
+  --ssh-extra-args ' -o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null'\
   ${BIFROST_ROOT_DIR}/playbooks/generate-inventory.yml
 
 echo "-------------------------------------------------------------------------"