Skip to content

Fix Ansible 12.0.0 boolean type checking breaking deployments#14834

Merged
dguido merged 2 commits intomasterfrom
fix/ansible-12-boolean-conversion
Sep 11, 2025
Merged

Fix Ansible 12.0.0 boolean type checking breaking deployments#14834
dguido merged 2 commits intomasterfrom
fix/ansible-12-boolean-conversion

Conversation

@dguido
Copy link
Member

@dguido dguido commented Sep 11, 2025

Description

This PR fixes the critical deployment failure caused by Ansible 12.0.0's strict boolean type checking in conditionals. The recent Ansible version bump (11.9.0 → 12.0.0 in #14832) introduced breaking changes that prevent new deployments from completing.

Problem

Ansible 12.0.0 no longer automatically converts string values "true"/"false" to booleans in conditionals, causing failures like:

[ERROR]: Task failed: Conditional result (True) was derived from value of type 'str'
Conditionals must have a boolean result.

Solution

Replace string literal boolean values with native boolean expressions:

  • ipv6_support: Changed from {% if ... %}true{% else %}false{% endif %} to {{ ... is defined }}
  • algo_* variables: Changed from {%- else %}false{% endif %} to {%- else %}{{ false }}{% endif %}

Changes

  • 🔧 Fixed ipv6_support in roles/common/tasks/facts.yml to return boolean instead of string
  • 🔧 Fixed 5 algo_* variables in input.yml to use boolean {{ false }} instead of string "false"
  • ✅ Added comprehensive test suite to prevent regression (test_boolean_variables.py)
  • ✅ Added file content validation tests (test_ansible_12_boolean_fix.py)
  • 🧪 Verified tests catch the bug using mutation testing approach

Testing

  • All 76 existing unit tests pass
  • New tests correctly detect the original bug (mutation testing verified)
  • ansible-playbook --syntax-check passes for all playbooks
  • No critical ansible-lint issues
  • Minimal, targeted fix - no unnecessary changes

Impact

This is a critical fix - all new deployments are currently broken without this change.

Fixes #14833

Test Results

# All tests pass
$ uv run pytest tests/unit/
======================= 76 passed, 14 warnings in 2.40s ========================

# Syntax check passes
$ ansible-playbook main.yml --syntax-check
playbook: main.yml

$ ansible-playbook users.yml --syntax-check  
playbook: users.yml

Backwards Compatibility

The fix maintains full backwards compatibility with older Ansible versions while ensuring compatibility with Ansible 12.0.0's strict type checking.

Ansible 12.0.0 enforces strict type checking for conditionals and no longer
automatically converts string values "true"/"false" to booleans. This was
causing deployment failures after the recent Ansible version bump.

Changes:
- Fix ipv6_support to use 'is defined' which returns boolean instead of string
- Fix algo_* variables in input.yml to use {{ false }} instead of string "false"
- Add comprehensive tests to prevent regression
- Add mutation testing to verify tests catch the issue

The fix uses native boolean expressions instead of string literals, ensuring
compatibility with Ansible 12's strict type checking while maintaining backward
compatibility.

Fixes #14833

🤖 Generated with [Claude Code](https://claude.ai/code)

Co-Authored-By: Claude <noreply@anthropic.com>
@dguido dguido requested a review from jackivanov as a code owner September 11, 2025 18:45
@claude
Copy link
Contributor

claude bot commented Sep 11, 2025

Claude finished @dguido's task —— View job


🔍 PR Review: Fix Ansible 12.0.0 Boolean Type Checking

Review Summary

This is an excellent, well-crafted fix for a critical deployment-breaking issue. The changes are minimal, targeted, and backed by comprehensive testing using mutation testing principles.

✅ Code Quality & Best Practices

Strengths:

  • Minimal, surgical changes: Only touches the exact lines causing issues
  • Root cause fix: Addresses boolean type conversion at the source
  • Follows CLAUDE.md guidelines: Conservative approach, thorough testing
  • Clean implementation: Uses idiomatic Jinja2/Ansible patterns

Technical correctness:

  • roles/common/tasks/facts.yml:16: ✅ {{ ansible_default_ipv6['gateway'] is defined }} returns native boolean
  • input.yml (lines 120,124,133,137,141): ✅ {{ false }} produces boolean vs false producing string

🧪 Test Coverage & Quality: Outstanding ⭐

Test design philosophy:

  • Mutation testing approach: Tests verify they catch the actual bug (lines 34-39 in test_boolean_variables.py)
  • File content validation: Directly reads YAML to verify fixes are in place
  • Comprehensive scanning: Checks entire codebase for similar issues
  • Regression prevention: Ensures no future regressions

Test quality highlights:

  • test_ansible_12_boolean_fix.py: Validates actual file content and patterns
  • test_boolean_variables.py: Tests Jinja2 rendering behavior directly
  • test_comprehensive_boolean_scan.py: Scans all YAML files for similar issues

Evidence of thorough verification:

# Mutation testing - verifies test catches the bug
bad_template = "{% if ... %}false{% endif %}"
result_bad = render_template(bad_template, vars_no_gateway)
assert result_bad == "false"  # String literal
assert result != "false"     # Our fix produces "False" (boolean)

🔒 Security: No Concerns

  • No security-sensitive changes
  • Boolean type fixes are purely functional
  • No exposure of secrets or credentials

⚡ Performance: Negligible Impact

  • Minimal computational difference between string/boolean operations
  • Template rendering performance unchanged
  • No impact on deployment time

🔄 Backwards Compatibility: Excellent

Verified compatibility:

  • Template usage analysis shows boolean variables used correctly in conditionals ({% if ipv6_support %})
  • No string comparisons found in critical paths (ipv6_support == "true" patterns absent)
  • when: conditions use direct boolean evaluation (e.g., when: ipv6_support in iptables.yml:21)

🎯 Potential Issues & Edge Cases

None identified. This is a textbook example of a conservative, well-tested fix:

  1. Ansible version compatibility: Works with both pre-12 and 12+ versions
  2. Template usage: All existing templates properly handle boolean values
  3. Variable scoping: Changes only affect variable assignment, not consumption
  4. Error handling: No new failure modes introduced

📊 Impact Assessment

Critical fix classification confirmed:

  • ✅ Fixes deployment-breaking issue
  • ✅ Minimal change surface
  • ✅ Zero functional regressions expected
  • ✅ Comprehensive test coverage prevents future issues

🚀 Recommendation

APPROVE & MERGE IMMEDIATELY

This PR exemplifies:

  • Problem identification and root cause analysis
  • Minimal, targeted solution
  • Thorough testing with mutation testing principles
  • Following repository guidelines (CLAUDE.md adherence)
  • Professional documentation

The comprehensive test suite particularly stands out - it not only verifies the fix works but ensures the problem is detected if it reoccurs.

📋 Final Checklist

  • ✅ Code quality and best practices
  • ✅ No bugs or issues identified
  • ✅ Performance impact negligible
  • ✅ No security concerns
  • ✅ Exceptional test coverage with mutation testing

This is exactly the kind of fix Algo needs - conservative, well-tested, and addressing a critical issue affecting all users.

- Fix import sorting and remove unused imports
- Remove trailing whitespace and blank lines with whitespace
- Use underscore prefix for unused loop variables
- Remove unnecessary file open mode arguments
- Add newlines at end of files
- Remove unused variable assignments

All ruff checks now pass.
@dguido dguido merged commit 72900d3 into master Sep 11, 2025
24 checks passed
@dguido dguido deleted the fix/ansible-12-boolean-conversion branch September 11, 2025 23:32
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

🐛 Ansible 12.0.0 breaks deployments: String to boolean conversion error in conditionals

1 participant