ansible/test/integration/targets/deb822_repository/tasks/name_handling.yml
Mohammed Saalim K 2f621c95fd
deb822_repository: validate name parameter instead of over-normalizing (#86494)
* deb822_repository: validate name parameter instead of over-normalizing

The deb822_repository module was silently mangling the name parameter by converting to lowercase, replacing underscores with hyphens, and removing other characters. This caused filename collisions where distinct repository names produced identical filenames.

This change validates that the name parameter contains only valid APT sources.list filename characters (a-zA-Z0-9_.-) and preserves the name as-is. Invalid characters now result in a clear error message.

Fixes: #86243

* fix: update install.yml test to use valid name without spaces

* deb822_repository: add backward compatibility for legacy filenames

Check if a file with the old normalized naming convention already exists before using the new naming. If a legacy file exists, reuse that slug to avoid creating duplicate files for the same repository.

This addresses feedback from #86343 to maintain backward compatibility.

* deb822_repository: adopt PR #86343 approach for backward compatibility

Based on maintainer feedback, removed strict validation that hard-failed on invalid characters. Instead:

- Convert spaces to hyphens (backward compatible with existing playbooks)

- Preserve case, underscores, and periods

- Check for legacy-normalized files and reuse them if they exist

This maintains backward compatibility while still avoiding over-normalization.

Addresses feedback from https://github.com/ansible/ansible/pull/86494#discussion_r1975816493

* Replicate PR #86343 structure and fix trailing whitespace

- Created name_handling.yml with exact tests from #86343

- Import name_handling.yml in main.yml

- Removed conflicting tests from test.yml

- Updated changelog to match #86343 format

- Fixed trailing whitespace on line 584 (pep8/pylint fix)

* Apply suggested change: inline name.replace() directly
2026-02-03 16:21:45 +01:00

55 lines
1.3 KiB
YAML

---
- name: Add repositories with distinct valid names
ansible.builtin.deb822_repository:
state: present
name: "{{ item }}"
types: [deb]
uris: ["http://example.invalid/repo"]
suites: ["stable"]
components: [main]
loop:
- foo-1.2.3
- foo_123
- FOO_1.23
- name: Verify each sources file exists
stat:
path: "/etc/apt/sources.list.d/{{ item }}.sources"
loop:
- foo-1.2.3
- foo_123
- FOO_1.23
register: repo_files
- name: Assert all sources files exist
assert:
that:
- repo_files.results | map(attribute='stat.exists') | min
- name: Add repository with space in name
ansible.builtin.deb822_repository:
state: present
name: "foo 12.3"
types: [deb]
uris: ["http://example.invalid/repo"]
suites: ["stable"]
components: [main]
register: spaced_name_result
- name: Assert repo creation succeeded
assert:
that:
- spaced_name_result is changed
- name: Verify sources file normalized with dash
stat:
path: /etc/apt/sources.list.d/foo-12.3.sources
register: spaced_name_stat
- name: Assert normalized sources file exists
assert:
that:
- spaced_name_stat.stat.exists
- name: Clean up test repositories
ansible.builtin.deb822_repository:
name: "{{ item }}"
state: absent
loop:
- foo-1.2.3
- foo_123
- FOO_1.23
- foo 12.3