1
0
Fork 0
mirror of https://github.com/ansible-collections/hetzner.hcloud.git synced 2026-02-03 23:51:48 +00:00

feat: add txt_record filter to format TXT records (#721)

##### SUMMARY

The format of TXT records must consist of one or many quoted strings of
255 characters.

Use this function to format TXT record that must match the format
required by the API:

```yml
- name: Create a SPF record
  hetzner.hcloud.zone_rrset:
    zone: example.com
    name: "@"
    type: "TXT"
    records:
      - value: "{{ 'v=spf1 include:_spf.example.net ~all' | hetzner.hcloud.text_record }}"
    state: present
```

##### ISSUE TYPE

- Feature Pull Request


##### COMPONENT NAME

zone_rrset
This commit is contained in:
Jonas L. 2025-10-31 12:36:19 +01:00 committed by GitHub
parent 5a40520a9c
commit 7ac361a9cc
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
5 changed files with 66 additions and 1 deletions

View file

@ -47,6 +47,25 @@ def load_balancer_status(load_balancer: dict, *args, **kwargs) -> Literal["unkno
raise AnsibleFilterError(f"load_balancer_status - {to_native(exc)}", orig_exc=exc) from exc
# pylint: disable=unused-argument
def txt_record(record: str, *args, **kwargs) -> str:
"""
Return the status of a Load Balancer based on its targets.
"""
try:
record = record.replace('"', '\\"')
parts = []
for start in range(0, len(record), 255):
end = min(start + 255, len(record))
parts.append('"' + record[start:end] + '"')
record = " ".join(parts)
return record
except Exception as exc:
raise AnsibleFilterError(f"txt_record - {to_native(exc)}", orig_exc=exc) from exc
class FilterModule:
"""
Hetzner Cloud filters.
@ -55,4 +74,5 @@ class FilterModule:
def filters(self):
return {
"load_balancer_status": load_balancer_status,
"txt_record": txt_record,
}

View file

@ -0,0 +1,19 @@
DOCUMENTATION:
name: txt_record
version_added: 6.1.0
short_description: Format a TXT record
description:
- Format a TXT record by splitting it in quoted strings of 255 characters.
options:
_input:
description: Record value to format.
type: string
required: true
EXAMPLES: |
# Format a TXT record
{{ 'v=spf1 include:_spf.example.net ~all' | hetzner.hcloud.txt_record }}
RETURN:
_value:
description: The formatted TXT record.
type: string

View file

@ -93,6 +93,15 @@ EXAMPLES = """
comment: web server 2
state: present
- name: Create a TXT record
hetzner.hcloud.zone_rrset:
zone: example.com
name: "@"
type: "TXT"
records:
- value: "{{ 'v=spf1 include:_spf.example.net ~all' | hetzner.hcloud.txt_record }}"
state: present
- name: Delete a Zone RRSet
hetzner.hcloud.zone_rrset:
zone: 42