xci: kubespray: Switch kubespray to dynamic inventory 91/57391/7
authorMarkos Chandras <mchandras@suse.de>
Tue, 15 May 2018 09:56:23 +0000 (10:56 +0100)
committerMarkos Chandras <mchandras@suse.de>
Wed, 16 May 2018 13:46:00 +0000 (14:46 +0100)
The kubespray installer contains one inventory per flavor. We can get
rid of these files and use the dynamic inventory similar to OSA.
Moreover, we extend the dynamic inventory to read additional group
variables per flavor if necessary. This way we can still pass additional
information to inventory on per-flavor basis. This also fixes a typo
in the 'IDF' file. We also need to bump Ansible for kubespray since the
version we were using is having troubles with dynamic inventories.

Change-Id: Ic58101555f81aec5fee3c193608440aa89bbe445
Signed-off-by: Markos Chandras <mchandras@suse.de>
xci/config/env-vars
xci/installer/kubespray/deploy.sh
xci/installer/kubespray/files/aio/inventory/inventory.cfg [deleted file]
xci/installer/kubespray/files/ha/inventory/inventory.cfg [deleted file]
xci/installer/kubespray/files/mini/inventory/inventory.cfg [deleted file]
xci/installer/kubespray/files/noha/inventory/inventory.cfg [deleted file]
xci/installer/kubespray/playbooks/configure-opnfvhost.yml
xci/playbooks/dynamic_inventory.py
xci/var/idf.yml

index bf333bd..fe75cb8 100755 (executable)
@@ -50,7 +50,7 @@ export ANSIBLE_HOST_KEY_CHECKING=False
 # subject of the certificate
 export XCI_SSL_SUBJECT=${XCI_SSL_SUBJECT:-"/C=US/ST=California/L=San Francisco/O=IT/CN=xci.releng.opnfv.org"}
 export DEPLOY_SCENARIO=${DEPLOY_SCENARIO:-"os-nosdn-nofeature"}
-# Kubespray requires that ansible version is 2.4.0.0
-export XCI_KUBE_ANSIBLE_PIP_VERSION=2.4.0.0
+# Kubespray requires that ansible version is 2.4.4
+export XCI_KUBE_ANSIBLE_PIP_VERSION=2.4.4
 # OpenStack global requirements version
 export OPENSTACK_REQUIREMENTS_VERSION=${OPENSTACK_REQUIREMENTS_VERSION:-$(awk '/requirements_git_install_branch:/ {print $2}' ${XCI_PATH}/xci/installer/osa/files/openstack_services.yml)}
index 02a9d43..bcd7dc1 100755 (executable)
@@ -28,8 +28,7 @@ echo "Info: Configuring localhost for kubespray"
 echo "-----------------------------------------------------------------------"
 cd $XCI_PLAYBOOKS
 ansible-playbook ${XCI_ANSIBLE_PARAMS} -e XCI_PATH="${XCI_PATH}" \
-        -i ${XCI_FLAVOR_ANSIBLE_FILE_PATH}/inventory/inventory.cfg \
-        configure-localhost.yml
+        -i dynamic_inventory.py configure-localhost.yml
 echo "-----------------------------------------------------------------------"
 echo "Info: Configured localhost for kubespray"
 
@@ -46,9 +45,8 @@ echo "Info: Configured localhost for kubespray"
 echo "Info: Configuring opnfv deployment host for kubespray"
 echo "-----------------------------------------------------------------------"
 cd $K8_XCI_PLAYBOOKS
-ansible-playbook ${XCI_ANSIBLE_PARAMS} -e XCI_PATH="${XCI_PATH}" \
-        -i ${XCI_FLAVOR_ANSIBLE_FILE_PATH}/inventory/inventory.cfg \
-        configure-opnfvhost.yml
+ansible-playbook ${XCI_ANSIBLE_PARAMS} \
+        -i ${XCI_PLAYBOOKS}/dynamic_inventory.py configure-opnfvhost.yml
 echo "-----------------------------------------------------------------------"
 echo "Info: Configured opnfv deployment host for kubespray"
 
@@ -65,25 +63,23 @@ if [ $XCI_FLAVOR != "aio" ]; then
     echo "Info: Configuring target hosts for kubespray"
     echo "-----------------------------------------------------------------------"
     cd $K8_XCI_PLAYBOOKS
-    ansible-playbook ${XCI_ANSIBLE_PARAMS} -e XCI_PATH="${XCI_PATH}" \
-            -i ${XCI_FLAVOR_ANSIBLE_FILE_PATH}/inventory/inventory.cfg \
-            configure-targethosts.yml
+    ansible-playbook ${XCI_ANSIBLE_PARAMS} \
+            -i ${XCI_PLAYBOOKS}/dynamic_inventory.py configure-targethosts.yml
     echo "-----------------------------------------------------------------------"
     echo "Info: Configured target hosts for kubespray"
 fi
 
 echo "Info: Using kubespray to deploy the kubernetes cluster"
 echo "-----------------------------------------------------------------------"
-ssh root@$OPNFV_HOST_IP "set -o pipefail; cd releng-xci/.cache/repos/kubespray;\
-         ansible-playbook \
-         -i opnfv_inventory/inventory.cfg cluster.yml -b | tee setup-kubernetes.log"
+ssh root@$OPNFV_HOST_IP "set -o pipefail; export XCI_FLAVOR=$XCI_FLAVOR; export INSTALLER_TYPE=$INSTALLER_TYPE; \
+        cd releng-xci/.cache/repos/kubespray/; ansible-playbook \
+        -i opnfv_inventory/dynamic_inventory.py cluster.yml -b | tee setup-kubernetes.log"
 scp root@$OPNFV_HOST_IP:~/releng-xci/.cache/repos/kubespray/setup-kubernetes.log \
-         $LOG_PATH/setup-kubernetes.log
+        $LOG_PATH/setup-kubernetes.log
 
 cd $K8_XCI_PLAYBOOKS
 ansible-playbook ${XCI_ANSIBLE_PARAMS} \
-    -i ${XCI_FLAVOR_ANSIBLE_FILE_PATH}/inventory/inventory.cfg \
-    configure-kubenet.yml
+    -i ${XCI_PLAYBOOKS}/dynamic_inventory.py configure-kubenet.yml
 echo
 echo "-----------------------------------------------------------------------"
 echo "Info: Kubernetes installation is successfully completed!"
diff --git a/xci/installer/kubespray/files/aio/inventory/inventory.cfg b/xci/installer/kubespray/files/aio/inventory/inventory.cfg
deleted file mode 100644 (file)
index a72d0fe..0000000
+++ /dev/null
@@ -1,20 +0,0 @@
-[all]
-opnfv ansible_host=192.168.122.2 ip=192.168.122.2
-
-[kube-master]
-opnfv
-
-[kube-node]
-opnfv
-
-[etcd]
-opnfv
-
-[k8s-cluster:children]
-kube-node
-kube-master
-
-[calico-rr]
-
-[vault]
-opnfv
diff --git a/xci/installer/kubespray/files/ha/inventory/inventory.cfg b/xci/installer/kubespray/files/ha/inventory/inventory.cfg
deleted file mode 100644 (file)
index aae3632..0000000
+++ /dev/null
@@ -1,32 +0,0 @@
-[all]
-opnfv ansible_host=192.168.122.2 ip=192.168.122.2
-master1 ansible_host=192.168.122.3 ip=192.168.122.3
-master2 ansible_host=192.168.122.4 ip=192.168.122.4
-master3 ansible_host=192.168.122.5 ip=192.168.122.5
-node1 ansible_host=192.168.122.6 ip=192.168.122.6
-node2 ansible_host=192.168.122.7 ip=192.168.122.7
-
-[kube-master]
-master1
-master2
-master3
-
-[kube-node]
-node1
-node2
-
-[etcd]
-master1
-master2
-master3
-
-[k8s-cluster:children]
-kube-node
-kube-master
-
-[calico-rr]
-
-[vault]
-master1
-master2
-master3
diff --git a/xci/installer/kubespray/files/mini/inventory/inventory.cfg b/xci/installer/kubespray/files/mini/inventory/inventory.cfg
deleted file mode 100644 (file)
index bf8bf19..0000000
+++ /dev/null
@@ -1,22 +0,0 @@
-[all]
-opnfv ansible_host=192.168.122.2 ip=192.168.122.2
-master1 ansible_host=192.168.122.3 ip=192.168.122.3
-node1 ansible_host=192.168.122.4 ip=192.168.122.4
-
-[kube-master]
-master1
-
-[kube-node]
-node1
-
-[etcd]
-master1
-
-[k8s-cluster:children]
-kube-node
-kube-master
-
-[calico-rr]
-
-[vault]
-master1
diff --git a/xci/installer/kubespray/files/noha/inventory/inventory.cfg b/xci/installer/kubespray/files/noha/inventory/inventory.cfg
deleted file mode 100644 (file)
index 73c1e0a..0000000
+++ /dev/null
@@ -1,24 +0,0 @@
-[all]
-opnfv ansible_host=192.168.122.2 ip=192.168.122.2
-master1 ansible_host=192.168.122.3 ip=192.168.122.3
-node1 ansible_host=192.168.122.4 ip=192.168.122.4
-node2 ansible_host=192.168.122.5 ip=192.168.122.5
-
-[kube-master]
-master1
-
-[kube-node]
-node1
-node2
-
-[etcd]
-master1
-
-[k8s-cluster:children]
-kube-node
-kube-master
-
-[calico-rr]
-
-[vault]
-master1
index 01904ba..7141f51 100644 (file)
       file:
         path: "{{ remote_xci_path }}/.cache/repos/kubespray/opnfv_inventory"
         state: absent
-    - name: copy kubespray inventory directory
-      command: "cp -rf {{ remote_xci_flavor_files }}/inventory \
-                   {{ remote_xci_path }}/.cache/repos/kubespray/opnfv_inventory"
-      args:
-        creates: "{{ remote_xci_path }}/.cache/repos/kubespray/opnfv_inventory"
+
     - name: make sure kubespray/opnfv_inventory/group_vars/ exist
       file:
         path: "{{ remote_xci_path }}/.cache/repos/kubespray/opnfv_inventory/group_vars"
         state: directory
 
+    - name: copy kubespray inventory directory
+      file:
+        src: "{{ remote_xci_playbooks }}/dynamic_inventory.py"
+        path: "{{ remote_xci_path }}/.cache/repos/kubespray/opnfv_inventory/dynamic_inventory.py"
+        state: link
+
     - name: Reload XCI deployment host facts
       setup:
         filter: ansible_local
index 8f49874..535dc25 100755 (executable)
@@ -13,6 +13,7 @@
 # Based on https://raw.githubusercontent.com/ansible/ansible/devel/contrib/inventory/cobbler.py
 
 import argparse
+import glob
 import os
 import sys
 import yaml
@@ -30,10 +31,12 @@ class XCIInventory(object):
         self.inventory['_meta']['hostvars'] = {}
         self.installer = os.environ.get('INSTALLER_TYPE', 'osa')
         self.flavor = os.environ.get('XCI_FLAVOR', 'mini')
+        self.flavor_files = os.path.dirname(os.path.realpath(__file__)) + "/../installer/" + self.installer + "/files/" + self.flavor
 
         # Static information for opnfv host for now
         self.add_host('opnfv')
-        self.add_hostvar('opnfv', 'ansible_ssh_host', '192.168.122.2')
+        self.add_hostvar('opnfv', 'ansible_host', '192.168.122.2')
+        self.add_hostvar('opnfv', 'ip', '192.168.122.2')
         self.add_to_group('deployment', 'opnfv')
         self.add_to_group('opnfv', 'opnfv')
 
@@ -93,6 +96,7 @@ class XCIInventory(object):
             pdf_host_info = filter(lambda x: x['name'] == host, pdf['nodes'])[0]
             native_vlan_if = filter(lambda x: x['vlan'] == 'native', pdf_host_info['interfaces'])
             self.add_hostvar(hostname, 'ansible_host', native_vlan_if[0]['address'])
+            self.add_hostvar(hostname, 'ip', native_vlan_if[0]['address'])
             host_networks[hostname] = {}
             # And now record the rest of the information
             for network in idf['idf']['net_config'].keys():
@@ -107,6 +111,24 @@ class XCIInventory(object):
         for parent in idf['xci'][self.installer]['groups'].keys():
             map(lambda x: self.add_group(x, parent), idf['xci'][self.installer]['groups'][parent])
 
+        # Read additional group variables
+        self.read_additional_group_vars()
+
+    def read_additional_group_vars(self):
+        if not os.path.exists(self.flavor_files + "/inventory/group_vars"):
+            return
+        group_dir = self.flavor_files + "/inventory/group_vars/*.yml"
+        group_file = glob.glob(group_dir)
+        for g in group_file:
+            with open(g) as f:
+                try:
+                    group_vars = yaml.safe_load(f)
+                except yaml.YAMLError as e:
+                    print(e)
+                    sys.exit(1)
+                for k,v in group_vars.items():
+                    self.add_groupvar(os.path.basename(g.replace('.yml', '')), k, v)
+
     def dump(self, data):
         print (json.dumps(data, sort_keys=True, indent=2))
 
@@ -134,8 +156,8 @@ class XCIInventory(object):
         self.inventory['_meta']['hostvars'][host].update({param: value})
 
     def add_groupvar(self, group, param, value):
-        if group not in self.groupvars(group):
-            self.inventory[group]['vars'] = {}
+        if param not in self.groupvars(group):
+            self.inventory[group]['vars'][param] = {}
         self.inventory[group]['vars'].update({param: value})
 
     def hostvars(self):
index 148508d..0238bae 100644 (file)
@@ -83,7 +83,7 @@ xci:
     groups:
       k8s-cluster:
         - kube-node
-        - kude-master
+        - kube-master
     hostnames:
       opnfv: opnfv
       node1: master1