diff options
author | Justin chi <chigang@huawei.com> | 2017-10-16 01:46:57 +0000 |
---|---|---|
committer | Gerrit Code Review <gerrit@opnfv.org> | 2017-10-16 01:46:57 +0000 |
commit | 45af5111e2c39105cdfd38a7ab8f3def25c3779d (patch) | |
tree | 8a2750d18690174100ff24697d39b05e2545ba01 /deploy/adapters/ansible | |
parent | b3aec6773447e8ab0b532723b08fd76ebddf9d8c (diff) | |
parent | 179cd6db9f1bd9be1606187cf74e128bede7a8e6 (diff) |
Merge "Modification for kargo to generate dynamic inventory"
Diffstat (limited to 'deploy/adapters/ansible')
-rwxr-xr-x[-rw-r--r--] | deploy/adapters/ansible/kubernetes/roles/kargo/files/generate_inventories.py | 79 | ||||
-rw-r--r-- | deploy/adapters/ansible/kubernetes/roles/kargo/tasks/main.yml | 9 |
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 |