Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix bugs in streaming code you hit if using more than 1 inventory #1166

Open
wants to merge 4 commits into
base: devel
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 4 additions & 5 deletions ansible_runner/config/runner.py
Original file line number Diff line number Diff line change
Expand Up @@ -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')
Expand Down Expand Up @@ -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
Expand Down
6 changes: 3 additions & 3 deletions ansible_runner/interface.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
check_isolation_executable_installed,
sanitize_json_response,
signal_handler,
get_relative_inventory_path
)

logging.getLogger('ansible-runner').addHandler(logging.NullHandler())
Expand Down Expand Up @@ -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):
Expand Down
7 changes: 6 additions & 1 deletion ansible_runner/streaming.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
17 changes: 17 additions & 0 deletions ansible_runner/utils/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 [
{
Expand Down