Fix ConfigParser.set error in vmware_inventory (#31643)

Fix adds default 'vmware' section in configuration,
when this section is not found.

Fixes: #31549

Signed-off-by: Abhijeet Kasurde <akasurde@redhat.com>
This commit is contained in:
Abhijeet Kasurde 2017-11-28 22:52:59 +05:30 committed by GitHub
parent 22d983c5c1
commit 449a0f33e1
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -1,19 +1,8 @@
#!/usr/bin/env python
# -*- coding: utf-8 -*-
#
# This file is part of Ansible,
#
# Ansible is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# Ansible is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with Ansible. If not, see <http://www.gnu.org/licenses/>.
# Copyright (C): 2017, Ansible Project
# GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt)
# Requirements
# - pyvmomi >= 6.0.0.2016.4
@ -40,7 +29,6 @@ from __future__ import print_function
import atexit
import datetime
import getpass
import itertools
import json
import os
@ -48,7 +36,6 @@ import re
import ssl
import sys
import uuid
from collections import defaultdict
from time import time
import six
@ -67,14 +54,6 @@ try:
except ImportError:
sys.exit("ERROR: This inventory script required 'pyVmomi' Python module, it was not able to load it")
hasvcr = False
try:
import vcr
hasvcr = True
except ImportError:
pass
def regex_match(s, pattern):
'''Custom filter for regex matching'''
@ -188,7 +167,6 @@ class VMWareInventory(object):
return json.dumps(data_to_print, indent=2)
def is_cache_valid(self):
''' Determines if the cache files have expired, or if it is still valid '''
valid = False
@ -202,21 +180,16 @@ class VMWareInventory(object):
return valid
def do_api_calls_update_cache(self):
''' Get instances and cache the data '''
self.inventory = self.instances_to_inventory(self.get_instances())
self.write_to_cache(self.inventory)
def write_to_cache(self, data):
''' Dump inventory to json file '''
with open(self.cache_path_cache, 'wb') as f:
f.write(json.dumps(data))
def get_inventory_from_cache(self):
''' Read in jsonified inventory '''
jdata = None
@ -225,7 +198,6 @@ class VMWareInventory(object):
return json.loads(jdata)
def read_settings(self):
''' Reads the settings from the vmware_inventory.ini file '''
scriptbasename = __file__
@ -271,6 +243,9 @@ class VMWareInventory(object):
vmware_ini_path = os.path.expanduser(os.path.expandvars(vmware_ini_path))
config.read(vmware_ini_path)
if 'vmware' not in config.sections():
config.add_section('vmware')
# apply defaults
for k, v in defaults['vmware'].items():
if not config.has_option('vmware', k):
@ -329,7 +304,6 @@ class VMWareInventory(object):
self.config = config
def parse_cli_args(self):
''' Command line argument processing '''
parser = argparse.ArgumentParser(description='Produce an Ansible Inventory file based on PyVmomi')
@ -346,7 +320,6 @@ class VMWareInventory(object):
self.args = parser.parse_args()
def get_instances(self):
''' Get a list of vm instances with pyvmomi '''
kwargs = {'host': self.server,
'user': self.username,
@ -361,9 +334,7 @@ class VMWareInventory(object):
return self._get_instances(kwargs)
def _get_instances(self, inkwargs):
''' Make API calls '''
instances = []
try:
si = SmartConnect(**inkwargs)
@ -425,9 +396,7 @@ class VMWareInventory(object):
return instance_tuples
def instances_to_inventory(self, instances):
''' Convert a list of vm objects into a json compliant inventory '''
self.debugl('re-indexing instances based on ini settings')
inventory = VMWareInventory._empty_inventory()
inventory['all'] = {}
@ -539,7 +508,6 @@ class VMWareInventory(object):
return inventory
def create_template_mapping(self, inventory, pattern, dtype='string'):
''' Return a hash of uuid to templated string from pattern '''
mapping = {}
@ -627,7 +595,6 @@ class VMWareInventory(object):
return rdata
def facts_from_vobj(self, vobj, level=0):
''' Traverse a VM object and return a json compliant data structure '''
# pyvmomi objects are not yet serializable, but may be one day ...
@ -759,7 +726,6 @@ class VMWareInventory(object):
return rdata
def get_host_info(self, host):
''' Return hostvars for a single host '''
if host in self.inventory['_meta']['hostvars']: