summaryrefslogtreecommitdiffstats
path: root/deploy/adapters/ansible
diff options
context:
space:
mode:
authorhu xinhui <xinhui_hu@foxmail.com>2017-09-29 17:06:33 +0800
committerhu xinhui <xinhui_hu@foxmail.com>2017-09-30 15:54:28 +0800
commit179cd6db9f1bd9be1606187cf74e128bede7a8e6 (patch)
tree5b6f6b381e594b42cb9bf8ac3cf45009a566be1c /deploy/adapters/ansible
parentf70d3aa8ffe7878e86b8f12c91c99018924a74b3 (diff)
Modification for kargo to generate dynamic inventory
JIRA: - There is some hardcode for kargo inventory.This patch is to generate dynamic inventory for kargo Change-Id: If4a7c5f973e520e4ca24c3c9e0bfa97725a5a44c Signed-off-by: hu xinhui <xinhui_hu@foxmail.com>
Diffstat (limited to 'deploy/adapters/ansible')
-rwxr-xr-x[-rw-r--r--]deploy/adapters/ansible/kubernetes/roles/kargo/files/generate_inventories.py79
-rw-r--r--deploy/adapters/ansible/kubernetes/roles/kargo/tasks/main.yml9
2 files changed, 62 insertions, 26 deletions
diff --git a/deploy/adapters/ansible/kubernetes/roles/kargo/files/generate_inventories.py b/deploy/adapters/ansible/kubernetes/roles/kargo/files/generate_inventories.py
index 62f29d84..2ffb4cae 100644..100755
--- a/deploy/adapters/ansible/kubernetes/roles/kargo/files/generate_inventories.py
+++ b/deploy/adapters/ansible/kubernetes/roles/kargo/files/generate_inventories.py
@@ -1,26 +1,32 @@
import yaml
import sys
+import os
from jinja2 import Environment
+try:
+ import json
+except ImportError:
+ import simplejson as json
INVENTORY_TEMPLATE = """
[all]
-{% for host, ip in hosts.iteritems() %}
-{{ host }} ansible_ssh_host={{ ip }} ansible_ssh_pass=root ansible_user=root
+{% for host, vales in hostvars.iteritems() %}
+{{ host }} ansible_ssh_host={{ vales['ansible_ssh_host'] }} \
+ansible_ssh_pass=root ansible_user=root
{% endfor %}
[kube-master]
-host1
-host2
+{% for host in kube_master %}
+{{ host }}
+{% endfor %}
[etcd]
-host1
-host2
-host3
+{% for host in etcd %}
+{{ host }}
+{% endfor %}
[kube-node]
-host2
-host3
-host4
-host5
+{% for host in kube_node %}
+{{ host }}
+{% endfor %}
[k8s-cluster:children]
kube-node
@@ -31,27 +37,50 @@ kube-master
"""
-def create_inventory_file(inventories_path, hosts):
- content = Environment().from_string(INVENTORY_TEMPLATE).render(hosts=hosts)
+def _byteify(data, ignore_dicts=False):
+
+ if isinstance(data, unicode):
+ return data.encode('utf-8')
+ if isinstance(data, list):
+ return [_byteify(item, ignore_dicts=True) for item in data]
+ if isinstance(data, dict) and not ignore_dicts:
+ return {
+ _byteify(key, ignore_dicts=True):
+ _byteify(value, ignore_dicts=True)
+ for key, value in data.iteritems()
+ }
+ return data
+
+
+def load_inventory(inventory):
+ if not os.path.exists(inventory):
+ raise RuntimeError('file: %s not exist' % inventory)
+ with open(inventory, 'r') as fd:
+ return json.load(fd, object_hook=_byteify)
+
+
+def create_inventory_file(inventories_path,
+ hostvars, kube_master, etcd, kube_node):
+ content = Environment().from_string(INVENTORY_TEMPLATE).render(
+ hostvars=hostvars, kube_master=kube_master,
+ etcd=etcd, kube_node=kube_node)
with open(inventories_path, 'w+') as f:
f.write(content)
-def fetch_all_sorted_external_ip(ip_cfg):
- hosts = {}
- for host, settings in ip_cfg.iteritems():
- external = settings["external"]["ip"]
- hosts[host] = external
- return hosts
-
+def main(inventories_path, local_inventory):
+ inventory_data = load_inventory(local_inventory)
+ hostvars = inventory_data['_meta']['hostvars']
+ kube_node = inventory_data['kube_node']['hosts']
+ kube_master = inventory_data['kube_master']['hosts']
+ etcd = inventory_data['etcd']['hosts']
-def main(inventories_path, ip_cfg):
- hosts = fetch_all_sorted_external_ip(ip_cfg)
- create_inventory_file(inventories_path, hosts)
+ create_inventory_file(inventories_path,
+ hostvars, kube_master, etcd, kube_node)
if __name__ == "__main__":
path = yaml.load(sys.argv[1])
- ipv_cfg = yaml.load(sys.argv[2])
+ local_inventory = yaml.load(sys.argv[2])
- main(path, ipv_cfg)
+ main(path, local_inventory)
diff --git a/deploy/adapters/ansible/kubernetes/roles/kargo/tasks/main.yml b/deploy/adapters/ansible/kubernetes/roles/kargo/tasks/main.yml
index 4e902606..d3dcb878 100644
--- a/deploy/adapters/ansible/kubernetes/roles/kargo/tasks/main.yml
+++ b/deploy/adapters/ansible/kubernetes/roles/kargo/tasks/main.yml
@@ -58,11 +58,18 @@
tags:
- ansible
+- name: copy inventoriy.json file
+ copy:
+ src: /var/ansible/run/kubernetes-opnfv2/inventories/inventory.json
+ dest: /tmp/inventory.json
+ tags:
+ - ansible
+
- name: generate kargo inventories
shell: >
python /tmp/generate_inventories.py \
"/opt/kargo_k8s/inventory/inventory.cfg" \
- "{{ ip_settings | to_json }}"
+ "/tmp/inventory.json"
tags:
- ansible