Browse Source

Merge pull request #9112 from tzumainn/openstack-static-inventory

Add option to generate a static inventory for Openstack
OpenShift Merge Robot 6 years ago
parent
commit
4fe45ae79f
2 changed files with 82 additions and 1 deletions
  1. 21 0
      playbooks/openstack/configuration.md
  2. 61 1
      playbooks/openstack/inventory.py

+ 21 - 0
playbooks/openstack/configuration.md

@@ -25,6 +25,7 @@ Environment variables may also be used.
 * [Cinder-Backed Registry Configuration](#cinder-backed-registry-configuration)
 * [Swift or Ceph Rados GW Backed Registry Configuration](#swift-or-ceph-rados-gw-backed-registry-configuration)
 * [Deploying At Scale](#deploying-at-scale)
+* [Using A Static Inventory](#using-a-static-inventory)
 
 
 ## OpenStack Configuration
@@ -798,3 +799,23 @@ a long time and eventually time out.  The following setting in
 `inventory/group_vars/all.yml` is recommended to prevent the timeouts:
 
 * `openshift_openstack_resolve_heat_outputs`: False
+
+
+## Using A Static Inventory
+
+The playbooks default to using a dynamic inventory in `openshift-ansible/playbooks/openstack/inventory.py`.
+You can also create a static inventory after the provision step, and
+then use that inventory for the install step. The steps to do so are as
+follows:
+
+```bash
+$ ansible-playbook --user openshift \
+  -i openshift-ansible/playbooks/openstack/inventory.py \
+  -i inventory \
+  openshift-ansible/playbooks/openstack/openshift-cluster/provision.yml
+$ python openshift-ansible/playbooks/openstack/inventory.py --static hosts
+$ ansible-playbook --user openshift \
+  -i hosts \
+  -i inventory \
+  openshift-ansible/playbooks/openstack/openshift-cluster/install.yml
+```

+ 61 - 1
playbooks/openstack/inventory.py

@@ -9,8 +9,13 @@ environment.
 
 from __future__ import print_function
 
+import argparse
 import json
 import os
+try:
+    import ConfigParser
+except ImportError:
+    import configparser as ConfigParser
 
 from keystoneauth1.exceptions.catalog import EndpointNotFound
 import shade
@@ -233,5 +238,60 @@ def _get_kuryr_vars(cloud_client, data):
     return settings
 
 
+def output_inventory(inventory, output_file):
+    """Outputs inventory into a file in ini format"""
+    config = ConfigParser.ConfigParser(allow_no_value=True)
+
+    host_meta_vars = _get_host_meta_vars_as_dict(inventory)
+
+    for key in sorted(inventory.keys()):
+        if key == 'localhost':
+            config.add_section('localhost')
+            config.set('localhost', 'localhost')
+            config.add_section('localhost:vars')
+            for var, value in inventory['localhost'].items():
+                config.set('localhost:vars', var, value)
+        elif key not in ('localhost', '_meta'):
+            if 'hosts' in inventory[key]:
+                config.add_section(key)
+                for host in inventory[key]['hosts']:
+                    if host in host_meta_vars.keys():
+                        config.set(key, host + " " + host_meta_vars[host])
+                    else:
+                        config.set(key, host)
+            if 'vars' in inventory[key]:
+                config.add_section(key + ":vars")
+                for var, value in inventory[key]['vars'].items():
+                    config.set(key + ":vars", var, value)
+
+    with open(output_file, 'w') as configfile:
+        config.write(configfile)
+
+
+def _get_host_meta_vars_as_dict(inventory):
+    """parse host meta vars from inventory as dict"""
+    host_meta_vars = {}
+    if '_meta' in inventory.keys():
+        if 'hostvars' in inventory['_meta']:
+            for host in inventory['_meta']['hostvars'].keys():
+                host_meta_vars[host] = ' '.join(
+                    '{}={}'.format(key, val) for key, val in inventory['_meta']['hostvars'][host].items())
+    return host_meta_vars
+
+
+def parse_args():
+    """parse arguments to script"""
+    parser = argparse.ArgumentParser(description="Create ansible inventory.")
+    parser.add_argument('--static', type=str, default='',
+                        help='File to store a static inventory in.')
+    parser.add_argument('--list', action="store_true", default=False,
+                        help='List inventory.')
+
+    return parser.parse_args()
+
+
 if __name__ == '__main__':
-    print(json.dumps(build_inventory(), indent=4, sort_keys=True))
+    if parse_args().static:
+        output_inventory(build_inventory(), parse_args().static)
+    else:
+        print(json.dumps(build_inventory(), indent=4, sort_keys=True))