From fa45c44026ed471714d0383fd2731911d16a1271 Mon Sep 17 00:00:00 2001 From: Alan Rominger Date: Mon, 30 Jul 2018 19:38:05 -0400 Subject: [PATCH] Update openstack inventory script to keep basic functionality (#43432) re-applies commit 6667ec447466abf1641787afccf9175369319d1f which fixed the plugin to the script so that it will work with current ansible-inventory. Also redirect stdout before dumping the ouptput, because not doing so will cause JSON parse errors in some cases. --- contrib/inventory/openstack_inventory.py | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/contrib/inventory/openstack_inventory.py b/contrib/inventory/openstack_inventory.py index 1bb5e17de95..81237a7bf22 100755 --- a/contrib/inventory/openstack_inventory.py +++ b/contrib/inventory/openstack_inventory.py @@ -57,6 +57,7 @@ import os import sys import time from distutils.version import StrictVersion +from io import StringIO try: import json @@ -81,7 +82,8 @@ def get_groups_from_server(server_vars, namegroup=True): groups.append(cloud) # Create a group on region - groups.append(region) + if region: + groups.append(region) # And one by cloud_region groups.append("%s_%s" % (cloud, region)) @@ -235,6 +237,8 @@ def parse_args(): def main(): args = parse_args() try: + # openstacksdk library may write to stdout, so redirect this + sys.stdout = StringIO() config_files = cloud_config.CONFIG_FILES + CONFIG_FILES sdk.enable_logging(debug=args.debug) inventory_args = dict( @@ -255,6 +259,7 @@ def main(): inventory = sdk_inventory.OpenStackInventory(**inventory_args) + sys.stdout = sys.__stdout__ if args.list: output = get_host_groups(inventory, refresh=args.refresh, cloud=args.cloud) elif args.host: