diff --git a/ansible_runner/config/runner.py b/ansible_runner/config/runner.py index 098d38196..7f8d88f4a 100644 --- a/ansible_runner/config/runner.py +++ b/ansible_runner/config/runner.py @@ -31,7 +31,7 @@ from ansible_runner.config._base import BaseConfig, BaseExecutionMode from ansible_runner.exceptions import ConfigurationError from ansible_runner.output import debug -from ansible_runner.utils import register_for_cleanup +from ansible_runner.utils import register_for_cleanup, get_relative_inventory_path logger = logging.getLogger('ansible-runner') @@ -160,14 +160,13 @@ def prepare_inventory(self): """ Prepares the inventory default under ``private_data_dir`` if it's not overridden by the constructor. """ - if self.containerized: - self.inventory = '/runner/inventory/hosts' - return - if self.inventory is None: if os.path.exists(os.path.join(self.private_data_dir, "inventory")): self.inventory = os.path.join(self.private_data_dir, "inventory") + if self.containerized: + self.inventory = get_relative_inventory_path(self.inventory, self.private_data_dir, new_base='/runner') + def prepare_env(self): """ Manages reading environment metadata files under ``private_data_dir`` and merging/updating diff --git a/ansible_runner/interface.py b/ansible_runner/interface.py index b9975fab0..85ab6cfbb 100644 --- a/ansible_runner/interface.py +++ b/ansible_runner/interface.py @@ -36,6 +36,7 @@ check_isolation_executable_installed, sanitize_json_response, signal_handler, + get_relative_inventory_path ) logging.getLogger('ansible-runner').addHandler(logging.NullHandler()) @@ -64,9 +65,8 @@ def init_runner(**kwargs): if os.path.isabs(playbook_path) and playbook_path.startswith(project_dir): kwargs['playbook'] = os.path.relpath(playbook_path, project_dir) - inventory_path = kwargs.get('inventory') or '' - if os.path.isabs(inventory_path) and inventory_path.startswith(private_data_dir): - kwargs['inventory'] = os.path.relpath(inventory_path, private_data_dir) + if kwargs.get('inventory'): + kwargs['inventory'] = get_relative_inventory_path(kwargs['inventory'], private_data_dir) roles_path = kwargs.get('envvars', {}).get('ANSIBLE_ROLES_PATH') or '' if os.path.isabs(roles_path) and roles_path.startswith(private_data_dir): diff --git a/ansible_runner/streaming.py b/ansible_runner/streaming.py index 1a64be740..e5aafa1d9 100644 --- a/ansible_runner/streaming.py +++ b/ansible_runner/streaming.py @@ -88,7 +88,12 @@ def update_paths(self, kwargs): roles_dir = os.path.join(self.private_data_dir, 'roles') kwargs['envvars']['ANSIBLE_ROLES_PATH'] = os.path.join(roles_dir, roles_path) if kwargs.get('inventory'): - kwargs['inventory'] = os.path.join(self.private_data_dir, kwargs['inventory']) + if isinstance(kwargs['inventory'], list): + kwargs['inventory'] = [ + os.path.join(self.private_data_dir, inventory_path) for inventory_path in kwargs['inventory'] + ] + else: + kwargs['inventory'] = os.path.join(self.private_data_dir, kwargs['inventory']) return kwargs diff --git a/ansible_runner/utils/__init__.py b/ansible_runner/utils/__init__.py index 21a01bbce..e3e57532f 100644 --- a/ansible_runner/utils/__init__.py +++ b/ansible_runner/utils/__init__.py @@ -463,6 +463,23 @@ def sanitize_container_name(original_name): return re.sub('[^a-zA-Z0-9_-]', '_', text_type(original_name)) +def get_relative_inventory_path(inventory, private_data_dir, new_base=''): + if isinstance(inventory, list): + new_inventory = [] + for inventory_path in inventory: + if os.path.isabs(inventory_path) and inventory_path.startswith(private_data_dir): + new_inventory.append( + os.path.join(new_base, os.path.relpath(inventory_path, private_data_dir)) + ) + else: + new_inventory.append(inventory_path) + return new_inventory + elif isinstance(inventory, string_types): + if os.path.isabs(inventory) and inventory.startswith(private_data_dir): + return os.path.join(new_base, os.path.relpath(inventory, private_data_dir)) + return inventory + + def cli_mounts(): return [ {