Update openstack inventory script to keep basic functionality Backport/2.6/43432 (#43465)

* Update openstack inventory script to keep basic functionality (#43432)

re-applies commit 6667ec4474 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.
(cherry picked from commit fa45c44026)

* fixed the plugin to the script so that it will work with current
This commit is contained in:
John R Barker 2018-07-31 08:28:28 -07:00 committed by Matt Clay
parent d067de5206
commit fc21507a30
2 changed files with 14 additions and 1 deletions

View file

@ -0,0 +1,8 @@
---
bugfixes:
- openstack_inventory.py dynamic inventory file
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.
(https://github.com/ansible/ansible/pull/43432)

View file

@ -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: