Add an inventory config check option

Previously there was no way to do a 'dry run' against a set of inventory
configuration files without actually writing the full inventory
structure to disk.

This change adds a simple check flag to the script, allowing users to
pre-validate their configurations prior to executing the Ansible
playbooks. No syntax validation happens against the YAML files; rather,
the dynamic_inventory.py script is executed and builds a full inventory
in memory, but does not write any output.

Currently, this is meant as a starting point. Further validation can
build from here, but the flag is largely meant to check for known
exceptions/errors early.

Change-Id: Ic58566ee124c824c8bba549ade46bce5c268905a
This commit is contained in:
Nolan Brubaker 2016-08-10 21:14:34 -04:00 committed by Jesse Pretorius (odyssey4me)
parent 813be4f33f
commit d68e65bfdd
4 changed files with 45 additions and 1 deletions

View File

@ -101,6 +101,17 @@ The same JSON structure is printed to stdout, which is consumed by Ansible as
the inventory for the playbooks.
Checking Inventory Configuration for Errors
-------------------------------------------
Using the ``--check`` flag when running ``dynamic_inventory.py`` will run the
inventory build process and look for known errors, but not write any files to
disk.
This check does not do YAML syntax validation, though it will fail if there
are unparseable errors.
Inspecting and Managing the Inventory
-------------------------------------

View File

@ -133,6 +133,12 @@ def args(arg_list):
action='store_true'
)
parser.add_argument(
'--check',
help="Configuration check only, don't generate inventory",
action='store_true',
)
return vars(parser.parse_args(arg_list))
@ -1138,6 +1144,10 @@ def main(all_args):
sort_keys=True
)
check = all_args.get('check')
if check:
return 'Configuration ok!'
# Generate a list of all hosts and their used IP addresses
hostnames_ips = {}
for _host, _vars in dynamic_inventory['_meta']['hostvars'].iteritems():

View File

@ -0,0 +1,7 @@
---
features:
- The `dynamic_inventory.py` file now takes a new argument, ``--check``,
which will run the inventory build without writing any files to the file
system. This is useful for checking to make sure your configuration does
not contain known errors prior to running Ansible commands.

View File

@ -737,7 +737,6 @@ class TestMultipleRuns(unittest.TestCase):
inventory_file_path = os.path.join(TARGET_DIR,
'openstack_inventory.json')
inv = di.get_inventory(TARGET_DIR, inventory_file_path)
self.assertIsInstance(inv, dict)
self.assertIn('_meta', inv)
@ -1001,5 +1000,22 @@ class TestSetUsedIPS(unittest.TestCase):
di.USED_IPS = set()
class TestConfigCheckFunctional(TestConfigCheckBase):
def duplicate_ip(self):
ip = self.user_defined_config['log_hosts']['aio1']
self.user_defined_config['log_hosts']['bogus'] = ip
def test_checking_good_config(self):
output = di.main({'config': TARGET_DIR, 'check': True})
self.assertEqual(output, 'Configuration ok!')
def test_duplicated_ip(self):
self.duplicate_ip()
self.write_config()
with self.assertRaises(di.MultipleHostsWithOneIPError) as context:
di.main({'config': TARGET_DIR, 'check': True})
self.assertEqual(context.exception.ip, '172.29.236.100')
if __name__ == '__main__':
unittest.main()