diff --git a/doc/source/developer-docs/inventory.rst b/doc/source/developer-docs/inventory.rst index ad641b5b1c..6080363987 100644 --- a/doc/source/developer-docs/inventory.rst +++ b/doc/source/developer-docs/inventory.rst @@ -35,12 +35,17 @@ This invocation is useful when testing changes to the dynamic inventory script. Inputs ^^^^^^ -The ``dynamic_inventory.py`` script takes a single argument, ``--config``. If -not specified, the default is ``/etc/openstack_deploy/``. +The ``dynamic_inventory.py`` takes the ``--config`` argument for the directory +holding configuration from which to create the inventory. If not specified, +the default is ``/etc/openstack_deploy/``. In addition to this argument, the base environment skeleton is provided in the ``playbooks/inventory/env.d`` directory of the OpenStack-Ansible codebase. +Should an ``env.d`` directory be found in the directory specified by +``--config``, it's contents will be added to the base environment, overriding +any previous contents in the event of conflicts. + .. note:: In all versions prior to |previous_release_formal_name|, this argument was ``--file``. The following file must be present in the configuration directory: @@ -101,6 +106,17 @@ The same JSON structure is printed to stdout, which is consumed by Ansible as the inventory for the playbooks. +Changing the Base Environment Directory +--------------------------------------- + +The ``--environment/-e`` argument will take the path to a directory containing +an ``env.d`` directory. This defaults to ``playbooks/inventory/`` in the +OpenStack-Ansible codebase. + +Contents of this directory are populated into the environment *before* the +``env.d`` found in the directory specified by ``--config``. + + Checking Inventory Configuration for Errors ------------------------------------------- diff --git a/playbooks/inventory/dynamic_inventory.py b/playbooks/inventory/dynamic_inventory.py index fa0649715b..297fd35591 100755 --- a/playbooks/inventory/dynamic_inventory.py +++ b/playbooks/inventory/dynamic_inventory.py @@ -158,6 +158,15 @@ def args(arg_list): default=False, ) + parser.add_argument( + '-e', + '--environment', + help=('Directory that contains the base env.d directory.\n' + 'Defaults to /playbooks/inventory/.'), + required=False, + default=os.path.dirname(__file__), + ) + return vars(parser.parse_args(arg_list)) @@ -1166,7 +1175,7 @@ def get_inventory(config_path, inventory_file_path): return dynamic_inventory -def main(config=None, check=False, debug=False, **kwargs): +def main(config=None, check=False, debug=False, environment=None, **kwargs): """Run the main application. :param config: ``str`` Directory from which to pull configs and overrides @@ -1175,6 +1184,7 @@ def main(config=None, check=False, debug=False, **kwargs): :param kwargs: ``dict`` Dictionary of arbitrary arguments; mostly for catching Ansible's required `--list` parameter without name shadowing the `list` built-in. + :param environment: ``str`` Directory containing the base env.d """ if debug: log_fmt = "%(lineno)d - %(funcName)s: %(message)s" @@ -1188,8 +1198,8 @@ def main(config=None, check=False, debug=False, **kwargs): ) user_defined_config = load_user_configuration(config_path) - - base_env = load_environment(os.path.dirname(__file__), {}) + base_env_dir = environment + base_env = load_environment(base_env_dir, {}) environment = load_environment(config_path, base_env) # Load existing inventory file if found diff --git a/tests/test_inventory.py b/tests/test_inventory.py index a0d9b64a81..5e055e84cf 100644 --- a/tests/test_inventory.py +++ b/tests/test_inventory.py @@ -91,7 +91,8 @@ def get_inventory(clean=True, extra_args=None): "Return the inventory mapping in a dict." # Use the list argument to more closely mirror # Ansible's use of the callable. - args = {'config': TARGET_DIR, 'list': True} + args = {'config': TARGET_DIR, 'list': True, + 'environment': BASE_ENV_DIR} if extra_args: args.update(extra_args) try: @@ -1108,14 +1109,15 @@ class TestConfigCheckFunctional(TestConfigCheckBase): self.user_defined_config['log_hosts']['bogus'] = ip def test_checking_good_config(self): - output = di.main(config=TARGET_DIR, check=True) + output = di.main(config=TARGET_DIR, check=True, + environment=BASE_ENV_DIR) 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) + di.main(config=TARGET_DIR, check=True, environment=BASE_ENV_DIR) self.assertEqual(context.exception.ip, '172.29.236.100')