mirror of
https://github.com/ansible-collections/community.general.git
synced 2024-09-14 20:13:21 +02:00
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:
parent
22d983c5c1
commit
449a0f33e1
1 changed files with 6 additions and 40 deletions
|
@ -1,19 +1,8 @@
|
||||||
#!/usr/bin/env python
|
#!/usr/bin/env python
|
||||||
|
# -*- coding: utf-8 -*-
|
||||||
#
|
#
|
||||||
# This file is part of Ansible,
|
# Copyright (C): 2017, Ansible Project
|
||||||
#
|
# GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt)
|
||||||
# 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/>.
|
|
||||||
|
|
||||||
# Requirements
|
# Requirements
|
||||||
# - pyvmomi >= 6.0.0.2016.4
|
# - pyvmomi >= 6.0.0.2016.4
|
||||||
|
@ -40,7 +29,6 @@ from __future__ import print_function
|
||||||
|
|
||||||
import atexit
|
import atexit
|
||||||
import datetime
|
import datetime
|
||||||
import getpass
|
|
||||||
import itertools
|
import itertools
|
||||||
import json
|
import json
|
||||||
import os
|
import os
|
||||||
|
@ -48,7 +36,6 @@ import re
|
||||||
import ssl
|
import ssl
|
||||||
import sys
|
import sys
|
||||||
import uuid
|
import uuid
|
||||||
from collections import defaultdict
|
|
||||||
from time import time
|
from time import time
|
||||||
|
|
||||||
import six
|
import six
|
||||||
|
@ -67,14 +54,6 @@ try:
|
||||||
except ImportError:
|
except ImportError:
|
||||||
sys.exit("ERROR: This inventory script required 'pyVmomi' Python module, it was not able to load it")
|
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):
|
def regex_match(s, pattern):
|
||||||
'''Custom filter for regex matching'''
|
'''Custom filter for regex matching'''
|
||||||
|
@ -188,7 +167,6 @@ class VMWareInventory(object):
|
||||||
return json.dumps(data_to_print, indent=2)
|
return json.dumps(data_to_print, indent=2)
|
||||||
|
|
||||||
def is_cache_valid(self):
|
def is_cache_valid(self):
|
||||||
|
|
||||||
''' Determines if the cache files have expired, or if it is still valid '''
|
''' Determines if the cache files have expired, or if it is still valid '''
|
||||||
|
|
||||||
valid = False
|
valid = False
|
||||||
|
@ -202,21 +180,16 @@ class VMWareInventory(object):
|
||||||
return valid
|
return valid
|
||||||
|
|
||||||
def do_api_calls_update_cache(self):
|
def do_api_calls_update_cache(self):
|
||||||
|
|
||||||
''' Get instances and cache the data '''
|
''' Get instances and cache the data '''
|
||||||
|
|
||||||
self.inventory = self.instances_to_inventory(self.get_instances())
|
self.inventory = self.instances_to_inventory(self.get_instances())
|
||||||
self.write_to_cache(self.inventory)
|
self.write_to_cache(self.inventory)
|
||||||
|
|
||||||
def write_to_cache(self, data):
|
def write_to_cache(self, data):
|
||||||
|
|
||||||
''' Dump inventory to json file '''
|
''' Dump inventory to json file '''
|
||||||
|
|
||||||
with open(self.cache_path_cache, 'wb') as f:
|
with open(self.cache_path_cache, 'wb') as f:
|
||||||
f.write(json.dumps(data))
|
f.write(json.dumps(data))
|
||||||
|
|
||||||
def get_inventory_from_cache(self):
|
def get_inventory_from_cache(self):
|
||||||
|
|
||||||
''' Read in jsonified inventory '''
|
''' Read in jsonified inventory '''
|
||||||
|
|
||||||
jdata = None
|
jdata = None
|
||||||
|
@ -225,7 +198,6 @@ class VMWareInventory(object):
|
||||||
return json.loads(jdata)
|
return json.loads(jdata)
|
||||||
|
|
||||||
def read_settings(self):
|
def read_settings(self):
|
||||||
|
|
||||||
''' Reads the settings from the vmware_inventory.ini file '''
|
''' Reads the settings from the vmware_inventory.ini file '''
|
||||||
|
|
||||||
scriptbasename = __file__
|
scriptbasename = __file__
|
||||||
|
@ -271,6 +243,9 @@ class VMWareInventory(object):
|
||||||
vmware_ini_path = os.path.expanduser(os.path.expandvars(vmware_ini_path))
|
vmware_ini_path = os.path.expanduser(os.path.expandvars(vmware_ini_path))
|
||||||
config.read(vmware_ini_path)
|
config.read(vmware_ini_path)
|
||||||
|
|
||||||
|
if 'vmware' not in config.sections():
|
||||||
|
config.add_section('vmware')
|
||||||
|
|
||||||
# apply defaults
|
# apply defaults
|
||||||
for k, v in defaults['vmware'].items():
|
for k, v in defaults['vmware'].items():
|
||||||
if not config.has_option('vmware', k):
|
if not config.has_option('vmware', k):
|
||||||
|
@ -329,7 +304,6 @@ class VMWareInventory(object):
|
||||||
self.config = config
|
self.config = config
|
||||||
|
|
||||||
def parse_cli_args(self):
|
def parse_cli_args(self):
|
||||||
|
|
||||||
''' Command line argument processing '''
|
''' Command line argument processing '''
|
||||||
|
|
||||||
parser = argparse.ArgumentParser(description='Produce an Ansible Inventory file based on PyVmomi')
|
parser = argparse.ArgumentParser(description='Produce an Ansible Inventory file based on PyVmomi')
|
||||||
|
@ -346,7 +320,6 @@ class VMWareInventory(object):
|
||||||
self.args = parser.parse_args()
|
self.args = parser.parse_args()
|
||||||
|
|
||||||
def get_instances(self):
|
def get_instances(self):
|
||||||
|
|
||||||
''' Get a list of vm instances with pyvmomi '''
|
''' Get a list of vm instances with pyvmomi '''
|
||||||
kwargs = {'host': self.server,
|
kwargs = {'host': self.server,
|
||||||
'user': self.username,
|
'user': self.username,
|
||||||
|
@ -361,9 +334,7 @@ class VMWareInventory(object):
|
||||||
return self._get_instances(kwargs)
|
return self._get_instances(kwargs)
|
||||||
|
|
||||||
def _get_instances(self, inkwargs):
|
def _get_instances(self, inkwargs):
|
||||||
|
|
||||||
''' Make API calls '''
|
''' Make API calls '''
|
||||||
|
|
||||||
instances = []
|
instances = []
|
||||||
try:
|
try:
|
||||||
si = SmartConnect(**inkwargs)
|
si = SmartConnect(**inkwargs)
|
||||||
|
@ -425,9 +396,7 @@ class VMWareInventory(object):
|
||||||
return instance_tuples
|
return instance_tuples
|
||||||
|
|
||||||
def instances_to_inventory(self, instances):
|
def instances_to_inventory(self, instances):
|
||||||
|
|
||||||
''' Convert a list of vm objects into a json compliant inventory '''
|
''' Convert a list of vm objects into a json compliant inventory '''
|
||||||
|
|
||||||
self.debugl('re-indexing instances based on ini settings')
|
self.debugl('re-indexing instances based on ini settings')
|
||||||
inventory = VMWareInventory._empty_inventory()
|
inventory = VMWareInventory._empty_inventory()
|
||||||
inventory['all'] = {}
|
inventory['all'] = {}
|
||||||
|
@ -539,7 +508,6 @@ class VMWareInventory(object):
|
||||||
return inventory
|
return inventory
|
||||||
|
|
||||||
def create_template_mapping(self, inventory, pattern, dtype='string'):
|
def create_template_mapping(self, inventory, pattern, dtype='string'):
|
||||||
|
|
||||||
''' Return a hash of uuid to templated string from pattern '''
|
''' Return a hash of uuid to templated string from pattern '''
|
||||||
|
|
||||||
mapping = {}
|
mapping = {}
|
||||||
|
@ -627,7 +595,6 @@ class VMWareInventory(object):
|
||||||
return rdata
|
return rdata
|
||||||
|
|
||||||
def facts_from_vobj(self, vobj, level=0):
|
def facts_from_vobj(self, vobj, level=0):
|
||||||
|
|
||||||
''' Traverse a VM object and return a json compliant data structure '''
|
''' Traverse a VM object and return a json compliant data structure '''
|
||||||
|
|
||||||
# pyvmomi objects are not yet serializable, but may be one day ...
|
# pyvmomi objects are not yet serializable, but may be one day ...
|
||||||
|
@ -759,7 +726,6 @@ class VMWareInventory(object):
|
||||||
return rdata
|
return rdata
|
||||||
|
|
||||||
def get_host_info(self, host):
|
def get_host_info(self, host):
|
||||||
|
|
||||||
''' Return hostvars for a single host '''
|
''' Return hostvars for a single host '''
|
||||||
|
|
||||||
if host in self.inventory['_meta']['hostvars']:
|
if host in self.inventory['_meta']['hostvars']:
|
||||||
|
|
Loading…
Reference in a new issue