Merge "Add an inventory config check option"

This commit is contained in:
Jenkins 2016-08-21 00:04:41 +00:00 committed by Gerrit Code Review
commit 811487c714
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))
@ -1132,6 +1138,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()