Skip to content

Conversation

cidrblock
Copy link
Contributor

@cidrblock cidrblock commented Aug 7, 2025

Summary

Enhances Molecule's ansible_args handling to intelligently provide arguments to user-provided create/destroy playbooks while maintaining safety for bundled playbooks. Previously, ansible_args were excluded from all create/destroy actions regardless of playbook source.

Changes Overview

Core Enhancement

  • Smart playbook detection: Differentiates between user-provided and bundled playbooks by checking file existence
  • Selective argument passing: Provides ansible_args to user-provided create/destroy playbooks, excludes them from bundled playbooks
  • Backward compatibility: Maintains existing behavior for converge/verify actions and bundled playbooks

Key Features

  • Environment variable fallback: MOLECULE_ANSIBLE_ARGS_STRICT_MODE reverts to legacy behavior (exclude all create/destroy args)
  • Native inventory support: Enables external inventory usage with create/destroy playbooks via ansible_args
  • Type-safe implementation: Handles all edge cases including None actions and missing provisioners

Technical Implementation

Modified Files

src/molecule/provisioner/ansible_playbook.py

  • Added _should_provide_args() method with smart detection logic
  • Simplified argument handling in bake() method
  • Maintains argument precedence: provisioner args → CLI args

tests/unit/provisioner/test_ansible_playbook.py

  • Fixed existing test with strict mode environment variable
  • Added comprehensive test coverage for new functionality
  • Tests cover: user-provided playbooks, bundled playbooks, strict mode, edge cases

docs/configuration.md

  • Documented MOLECULE_ANSIBLE_ARGS_STRICT_MODE environment variable
  • Added usage examples and behavior explanations

docs/examples/native_inventory.md (NEW)

  • Complete native inventory documentation with real-world examples
  • Demonstrates static YAML inventory + constructed plugin integration
  • Shows proper inventory file naming conventions for load order
  • Explains benefits over Molecule's built-in inventory management
  • Links to fixture files for practical reference

tests/fixtures/integration/test_command/molecule/native_inventory/ (NEW SCENARIO)

  • Complete integration test scenario demonstrating native Ansible inventory
  • Static inventory (01-inventory.yml): Host definitions with container images
  • Dynamic inventory (02-constructed.yml): Constructed plugin creating 'containers' group
  • Group variables (group_vars/containers.yml): Connection settings for container hosts
  • Host variables (host_vars/container-devtools.yml): Environment variables per host
  • Custom playbooks using native inventory:
    • create.yml: Creates containers from inventory-defined hosts
    • converge.yml: Targets constructed 'containers' group
    • verify.yml: Validates environment variables and group membership
    • destroy.yml: Cleans up containers using inventory data
  • Requirements (requirements.yml): Collections needed for container management
  • Documentation (README.md): Scenario overview and implementation details

Use Cases Enabled

Native Inventory Integration

provisioner:
  name: ansible
  ansible_args:
    - --inventory=${MOLECULE_SCENARIO_DIRECTORY}/inventory/

Multiple Inventory Sources

  • Static YAML/INI files: Traditional host definitions
  • Dynamic inventory scripts: Cloud providers, CMDBs
  • Inventory plugins: AWS, Azure, GCP, Kubernetes
  • Constructed plugin: Dynamic grouping based on host facts
  • Mixed approaches: Static + dynamic in same directory

Real-World Scenarios

  • Existing inventory reuse: Leverage production inventory for testing
  • Cloud-native testing: Use provider plugins for dynamic hosts
  • Complex grouping: Constructed plugin for sophisticated host organization
  • Multi-source inventory: Combine static definitions with dynamic discovery

Migration Path

Existing Users

  • No action required: Change is backward compatible
  • Optional: Set MOLECULE_ANSIBLE_ARGS_STRICT_MODE=true to maintain exact legacy behavior

New Capabilities

  • Users can now leverage external inventory in create/destroy playbooks
  • Multiple inventory sources work seamlessly with Molecule scenarios
  • Native Ansible inventory features (plugins, constructed groups) fully supported

Testing

  • Unit tests: 100% coverage of new logic paths including edge cases
  • Integration test: Complete native inventory scenario with real containers
  • Regression tests: Existing functionality preserved
  • Documentation: Comprehensive examples and fixture references

Breaking Changes

None. All existing functionality is preserved with optional strict mode fallback.

@cidrblock cidrblock requested a review from a team as a code owner August 7, 2025 21:08
@cidrblock cidrblock changed the title feat: Enable ansible_args for user-provided create/destroy playbooks with smart detection Enable ansible_args for user-provided create/destroy playbooks with smart detection Aug 7, 2025
@cidrblock cidrblock changed the title Enable ansible_args for user-provided create/destroy playbooks with smart detection Enable ansible_args for user-provided create/destroy playbooks Aug 7, 2025
@cidrblock cidrblock changed the title Enable ansible_args for user-provided create/destroy playbooks Enable ansible_args for user-provided create/destroy playbooks Aug 7, 2025
@cidrblock cidrblock force-pushed the external_inventory branch from d6c15f9 to 9bafd40 Compare August 7, 2025 21:19
@cidrblock cidrblock changed the title Enable ansible_args for user-provided create/destroy playbooks Enable ansible_args for user-provided create/destroy playbooks Aug 8, 2025
@Qalthos Qalthos force-pushed the external_inventory branch from 9f561f3 to 291629d Compare August 11, 2025 15:39
@cidrblock cidrblock merged commit 8ee204c into ansible:main Aug 11, 2025
21 checks passed
cidrblock added a commit that referenced this pull request Aug 15, 2025
Depends on #4505 

## Summary

This pull request adds comprehensive testing philosophy documentation to
help users understand Molecule's approach to Ansible automation testing
and its strategic direction.

## Changes Made

### New Documentation: docs/philosophy.md
- **Testing suite fundamentals**: Universal testing framework
capabilities and core phases applicable to any robust testing system
- **Molecule's Ansible-centric approach**: How Molecule addresses
testing requirements specifically for Ansible automation across all
domains (infrastructure, applications, network, cloud, APIs, business
processes)
- **Resource lifecycle management**: Detailed explanation of shared
state and shared inventory mechanisms for cost optimization and workflow
efficiency
- **Configuration flexibility**: Ansible as unified driver, provisioner,
and verifier with enterprise customization capabilities
- **Evolution and strategic direction**: Molecule's focused development
on deeper Ansible integration rather than broader tool support

### Key Technical Improvements
- **Corrected shared state implementation**: Accurate documentation of
automatic default scenario management for create/destroy operations
- **Shared inventory mechanism**: Clear explanation of how centralized
inventory enables resource sharing across scenarios
- **Enterprise customization**: Documentation of how sequence
configurations can be committed and shared between teams
- **Framework evolution**: Nine-year optimization history and future
roadmap focusing on Ansible ecosystem alignment

### Documentation Quality
- Professional technical writing suitable for seasoned Ansible
practitioners
- Framework-agnostic core concepts with Ansible-specific implementation
details
- Comprehensive workflow examples and real-world usage patterns
- Accurate technical details verified through codebase analysis

## Target Audience

This documentation serves automation practitioners, enterprise teams,
and decision makers who need to understand Molecule's testing
philosophy, capabilities, and strategic direction within the Ansible
ecosystem.

## Testing

- Documentation builds successfully with ROOT: will run in automatically
provisioned tox, host
/home/bthornto/github/molecule311/.venv/bin/python3 is missing [requires
(has)]: tox-uv>=1.20.2
ROOT: provision> .tox/.tox/bin/python -m tox -e docs
.pkg: _optional_hooks> python
/home/bthornto/github/molecule311/.tox/.tox/lib/python3.10/site-packages/pyproject_api/_backend.py
True setuptools.build_meta
.pkg: get_requires_for_build_editable> python
/home/bthornto/github/molecule311/.tox/.tox/lib/python3.10/site-packages/pyproject_api/_backend.py
True setuptools.build_meta
.pkg: build_editable> python
/home/bthornto/github/molecule311/.tox/.tox/lib/python3.10/site-packages/pyproject_api/_backend.py
True setuptools.build_meta
docs: install_package> .tox/.tox/bin/uv pip install --reinstall
--no-deps
molecule@/home/bthornto/github/molecule311/.tox/.tmp/package/49/molecule-25.7.1.dev20-0.editable-py3-none-any.whl
docs: commands_pre[0]> sh -c 'rm -f
/home/bthornto/github/molecule311/.tox/docs/.coverage* 2>/dev/null ||
true'
docs: commands[0]> mkdocs build --strict --site-dir=_readthedocs/html/
  docs: OK (8.61=setup[1.41]+cmd[0.01,7.19] seconds)
  congratulations :) (8.71 seconds)
- All technical details verified through codebase review
- Examples tested for accuracy and clarity

---------

Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
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