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:
parent
813be4f33f
commit
d68e65bfdd
@ -101,6 +101,17 @@ The same JSON structure is printed to stdout, which is consumed by Ansible as
|
|||||||
the inventory for the playbooks.
|
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
|
Inspecting and Managing the Inventory
|
||||||
-------------------------------------
|
-------------------------------------
|
||||||
|
|
||||||
|
@ -133,6 +133,12 @@ def args(arg_list):
|
|||||||
action='store_true'
|
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))
|
return vars(parser.parse_args(arg_list))
|
||||||
|
|
||||||
|
|
||||||
@ -1138,6 +1144,10 @@ def main(all_args):
|
|||||||
sort_keys=True
|
sort_keys=True
|
||||||
)
|
)
|
||||||
|
|
||||||
|
check = all_args.get('check')
|
||||||
|
if check:
|
||||||
|
return 'Configuration ok!'
|
||||||
|
|
||||||
# Generate a list of all hosts and their used IP addresses
|
# Generate a list of all hosts and their used IP addresses
|
||||||
hostnames_ips = {}
|
hostnames_ips = {}
|
||||||
for _host, _vars in dynamic_inventory['_meta']['hostvars'].iteritems():
|
for _host, _vars in dynamic_inventory['_meta']['hostvars'].iteritems():
|
||||||
|
@ -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.
|
||||||
|
|
@ -737,7 +737,6 @@ class TestMultipleRuns(unittest.TestCase):
|
|||||||
|
|
||||||
inventory_file_path = os.path.join(TARGET_DIR,
|
inventory_file_path = os.path.join(TARGET_DIR,
|
||||||
'openstack_inventory.json')
|
'openstack_inventory.json')
|
||||||
|
|
||||||
inv = di.get_inventory(TARGET_DIR, inventory_file_path)
|
inv = di.get_inventory(TARGET_DIR, inventory_file_path)
|
||||||
self.assertIsInstance(inv, dict)
|
self.assertIsInstance(inv, dict)
|
||||||
self.assertIn('_meta', inv)
|
self.assertIn('_meta', inv)
|
||||||
@ -1001,5 +1000,22 @@ class TestSetUsedIPS(unittest.TestCase):
|
|||||||
di.USED_IPS = set()
|
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__':
|
if __name__ == '__main__':
|
||||||
unittest.main()
|
unittest.main()
|
||||||
|
Loading…
x
Reference in New Issue
Block a user