Skip to content

Conversation

@cidrblock
Copy link
Contributor

Add MOLECULE_SHARED_INVENTORY_DIR environment variable and playbook variable

Summary

Introduces MOLECULE_SHARED_INVENTORY_DIR as both an environment variable and playbook variable to enable access to shared inventory directories when using the --shared-inventory option. This enhancement allows scenarios to programmatically create and consume shared inventory files across multiple test runs.

Use Case

When testing complex multi-scenario setups, users often need to share inventory data between scenarios. The existing --shared-inventory option provides shared storage but lacks programmatic access from playbooks. This implementation enables:

  • Dynamic inventory file creation in shared locations
  • Cross-scenario inventory data consumption
  • Custom inventory management workflows
  • Enhanced testing of multi-service deployments

Implementation Details

Core Changes

Environment Variable (src/molecule/config.py)

  • Added MOLECULE_SHARED_INVENTORY_DIR to environment variables
  • Returns shared inventory path when --shared-inventory enabled and not in parallel mode
  • Returns empty string in normal mode or parallel mode (where shared inventory is disabled)

Playbook Variable (src/molecule/provisioner/ansible.py)

  • Added molecule_shared_inventory_dir to molecule variables available in all playbooks
  • Uses environment lookup: {{ lookup('env', 'MOLECULE_SHARED_INVENTORY_DIR') }}
  • Fixed inventory property type annotation from dict[str, str] to dict[str, Any]

Testing

Unit Tests

  • tests/unit/test_config.py: Environment variable behavior across all modes
  • tests/unit/provisioner/test_ansible.py: Playbook variable inclusion verification

Integration Tests

  • tests/fixtures/integration/.../shared_inventory/: Complete test scenario with realistic inventory templating
  • tests/integration/test_command.py: Dedicated test for shared inventory functionality

Documentation

Configuration Reference (docs/configuration.md)

  • Documented new environment variable with usage context
  • Referenced corresponding playbook variable availability

Usage Guide (docs/guides/sharing.md)

  • Added practical examples showing inventory file creation and consumption
  • Demonstrated real-world use cases with Jinja2 templating

Behavior

Mode shared_inventory parallel Variable Value
Normal false false "" (empty)
Shared true false /path/to/shared/inventory
Parallel true true "" (shared disabled)

Example Usage

- name: Create shared inventory
  ansible.builtin.copy:
    content: "{{ inventory_ini }}"
    dest: "{{ molecule_shared_inventory_dir }}/shared_inventory.ini"
    mode: "0600"
  when: molecule_shared_inventory_dir != ""
  vars:
    inventory_ini: |
      [molecule]
      {% for platform in molecule_yml.platforms %}
      {{ platform.name }}{% for key, value in platform.get('host_vars', {}).items() %} {{ key }}={{ value }}{% endfor %}
      {% endfor %}
  delegate_to: localhost

Compatibility

  • Fully backward compatible
  • No changes to existing behavior
  • New variables are additive only
  • Follows established molecule variable patterns

…book variable

Adds support for accessing shared inventory directory in playbooks when using
--shared-inventory option. This enables scenarios to create and consume shared
inventory files across multiple test runs.

- Add MOLECULE_SHARED_INVENTORY_DIR environment variable
- Add molecule_shared_inventory_dir playbook variable
- Variable is empty string in normal mode and parallel mode
- Contains shared inventory path when --shared-inventory is enabled
- Update documentation with usage examples
- Add comprehensive unit and integration tests
cidrblock and others added 2 commits August 5, 2025 14:52
Fix MkDocs build error by properly escaping Jinja2 template variables
in code examples using raw blocks to prevent macro rendering errors.
@cidrblock cidrblock merged commit b65075e into ansible:main Aug 5, 2025
21 checks passed
@github-project-automation github-project-automation bot moved this from Review to Done in 🧰 devtools project board Aug 5, 2025
@cidrblock cidrblock changed the title feat: Add MOLECULE_SHARED_INVENTORY_DIR environment variable and playbook variable Reverted: Add MOLECULE_SHARED_INVENTORY_DIR environment variable and playbook variable Aug 16, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

Archived in project

Development

Successfully merging this pull request may close these issues.

2 participants