algo/files/cloud-init
Dan Guido 4bb13a5ce8
Fix Ansible 12 double-templating and Jinja2 spacing issues (#14836)
* Fix Ansible 12 double-templating and Jinja2 spacing issues

This PR fixes critical deployment issues and improves code consistency for Ansible 12 compatibility.

## Fixed Issues

### 1. Double-templating bug (Issue #14835)
Fixed 7 instances of invalid double-templating that breaks deployments:
- Changed `{{ lookup('file', '{{ var }}') }}` to `{{ lookup('file', var) }}`
- Affects Azure, DigitalOcean, GCE, Linode, and IPsec configurations
- Added comprehensive test to prevent regression

### 2. Jinja2 spacing inconsistencies
Fixed 33+ spacing issues for better code quality:
- Removed spaces between Jinja2 blocks: `}} {%` → `}}{%`
- Fixed operator spacing: `int -1` → `int - 1`
- Fixed filter spacing: `|b64encode` → `| b64encode`
- Consolidated multiline expressions to single lines

### 3. Test suite improvements
Enhanced boolean type checking test to be more targeted:
- Excludes external dependencies and CloudFormation templates
- Only tests Algo's actual codebase
- Verified with mutation testing
- Added comprehensive documentation

## Testing
- All 87 unit tests pass
- 0 Jinja2 spacing issues remaining (verified by ansible-lint)
- Ansible syntax checks pass for all playbooks
- Mutation testing confirms tests catch real issues

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

Co-Authored-By: Claude <noreply@anthropic.com>

* Fix Python linting issue

- Remove unnecessary f-string prefix where no placeholders are used
- Fixes ruff F541 error

* Fix line length linting issues

- Break long lines to stay within 120 character limit
- Extract variables for better readability
- Fixes ruff E501 errors

---------

Co-authored-by: Claude <noreply@anthropic.com>
2025-09-15 09:54:45 -04:00
..
base.sh Fix Vultr (#14389) 2021-12-23 21:25:36 +03:00
base.yml Fix Ansible 12 double-templating and Jinja2 spacing issues (#14836) 2025-09-15 09:54:45 -04:00
README.md Fix DigitalOcean cloud-init compatibility and deprecation warnings (#14801) 2025-08-03 14:25:47 -04:00
sshd_config Change default SSH port and introduce cloud-init support (#1636) 2020-01-07 14:28:19 +01:00

Cloud-Init Files - Critical Format Requirements

⚠️ CRITICAL WARNING ⚠️

The files in this directory have STRICT FORMAT REQUIREMENTS that must not be changed by linters or automated formatting tools.

Cloud-Config Header Format

The first line of base.yml MUST be exactly:

#cloud-config

DO NOT CHANGE TO:

  • # cloud-config (space after #) - BREAKS CLOUD-INIT PARSING
  • Add YAML document start --- - NOT ALLOWED IN CLOUD-INIT

Why This Matters

Cloud-init's YAML parser expects the exact string #cloud-config as the first line. Any deviation causes:

  1. Complete parsing failure - All directives are skipped
  2. SSH configuration not applied - Servers remain on port 22 instead of 4160
  3. Deployment timeouts - Ansible cannot connect to configure the VPN
  4. DigitalOcean specific impact - Other providers may be more tolerant

Historical Context

  • Working: All versions before PR #14775 (August 2025)
  • Broken: PR #14775 "Apply ansible-lint improvements" added space by mistake
  • Fixed: PR #14801 restored correct format + added protections

See GitHub issue #14800 for full technical details.

Linter Configuration

These files are excluded from:

  • yamllint (.yamllint config)
  • ansible-lint (.ansible-lint config)

This prevents automated tools from "fixing" the format and breaking deployments.

Template Variables

The cloud-init files use Jinja2 templating:

  • {{ ssh_port }} - Configured SSH port (typically 4160)
  • {{ lookup('file', '{{ SSH_keys.public }}') }} - SSH public key

Editing Guidelines

  1. Never run automated formatters on these files
  2. Test immediately after any changes with real deployments
  3. Check yamllint warnings are expected (missing space in comment, missing ---)
  4. Verify first line remains exactly #cloud-config

References