Skip to content

Commit 3c23ce4

Browse files
authored
cloudflare_dns: add support for comment and tags (#9132)
* `cloudflare_dns`: add support for `comment` and `tags` * `cloudflare_dns`: add return values for `comment`/`tags` fields * `cloudflare_dns`: fix return values samples * `cloudflare_dns`: changelog fragment formatting * `cloudflare_dns`: add missing `version_added` * `cloudflare_dns`: remove explicit `required: false` * `cloudflare_dns`: empty `comment` idempotency fix
1 parent a9449cc commit 3c23ce4

File tree

2 files changed

+63
-5
lines changed

2 files changed

+63
-5
lines changed
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
minor_changes:
2+
- cloudflare_dns - add support for ``comment`` and ``tags`` (https://github.com/ansible-collections/community.general/pull/9132).

plugins/modules/cloudflare_dns.py

Lines changed: 61 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -31,21 +31,18 @@
3131
- "You can obtain your API token from the bottom of the Cloudflare 'My Account' page, found here: U(https://dash.cloudflare.com/)."
3232
- Can be specified in E(CLOUDFLARE_TOKEN) environment variable since community.general 2.0.0.
3333
type: str
34-
required: false
3534
version_added: '0.2.0'
3635
account_api_key:
3736
description:
3837
- Account API key.
3938
- Required for api keys authentication.
4039
- "You can obtain your API key from the bottom of the Cloudflare 'My Account' page, found here: U(https://dash.cloudflare.com/)."
4140
type: str
42-
required: false
4341
aliases: [ account_api_token ]
4442
account_email:
4543
description:
4644
- Account email. Required for API keys authentication.
4745
type: str
48-
required: false
4946
algorithm:
5047
description:
5148
- Algorithm number.
@@ -57,6 +54,11 @@
5754
- Required for O(type=TLSA) when O(state=present).
5855
type: int
5956
choices: [ 0, 1, 2, 3 ]
57+
comment:
58+
description:
59+
- Comments or notes about the DNS record.
60+
type: str
61+
version_added: 10.1.0
6062
flag:
6163
description:
6264
- Issuer Critical Flag.
@@ -134,6 +136,12 @@
134136
type: str
135137
choices: [ absent, present ]
136138
default: present
139+
tags:
140+
description:
141+
- Custom tags for the DNS record.
142+
type: list
143+
elements: str
144+
version_added: 10.1.0
137145
timeout:
138146
description:
139147
- Timeout for Cloudflare API calls.
@@ -191,6 +199,18 @@
191199
value: 127.0.0.1
192200
api_token: dummyapitoken
193201
202+
- name: Create a record with comment and tags
203+
community.general.cloudflare_dns:
204+
zone: example.net
205+
record: test
206+
type: A
207+
value: 127.0.0.1
208+
comment: Local test website
209+
tags:
210+
- test
211+
- local
212+
api_token: dummyapitoken
213+
194214
- name: Create a example.net CNAME record to example.com
195215
community.general.cloudflare_dns:
196216
zone: example.net
@@ -299,6 +319,18 @@
299319
returned: success, except on record deletion
300320
type: complex
301321
contains:
322+
comment:
323+
description: Comments or notes about the DNS record.
324+
returned: success
325+
type: str
326+
sample: Domain verification record
327+
version_added: 10.1.0
328+
comment_modified_on:
329+
description: When the record comment was last modified. Omitted if there is no comment.
330+
returned: success
331+
type: str
332+
sample: "2024-01-01T05:20:00.12345Z"
333+
version_added: 10.1.0
302334
content:
303335
description: The record content (details depend on record type).
304336
returned: success
@@ -333,7 +365,7 @@
333365
type: bool
334366
sample: false
335367
meta:
336-
description: No documentation available.
368+
description: Extra Cloudflare-specific information about the record.
337369
returned: success
338370
type: dict
339371
sample: { auto_added: false }
@@ -362,6 +394,19 @@
362394
returned: success
363395
type: bool
364396
sample: false
397+
tags:
398+
description: Custom tags for the DNS record.
399+
returned: success
400+
type: list
401+
elements: str
402+
sample: ['production', 'app']
403+
version_added: 10.1.0
404+
tags_modified_on:
405+
description: When the record tags were last modified. Omitted if there are no tags.
406+
returned: success
407+
type: str
408+
sample: "2025-01-01T05:20:00.12345Z"
409+
version_added: 10.1.0
365410
ttl:
366411
description: The time-to-live for the record.
367412
returned: success
@@ -410,9 +455,11 @@ def __init__(self, module):
410455
self.account_email = module.params['account_email']
411456
self.algorithm = module.params['algorithm']
412457
self.cert_usage = module.params['cert_usage']
458+
self.comment = module.params['comment']
413459
self.hash_type = module.params['hash_type']
414460
self.flag = module.params['flag']
415461
self.tag = module.params['tag']
462+
self.tags = module.params['tags']
416463
self.key_tag = module.params['key_tag']
417464
self.port = module.params['port']
418465
self.priority = module.params['priority']
@@ -662,7 +709,7 @@ def delete_dns_records(self, **kwargs):
662709
def ensure_dns_record(self, **kwargs):
663710
params = {}
664711
for param in ['port', 'priority', 'proto', 'proxied', 'service', 'ttl', 'type', 'record', 'value', 'weight', 'zone',
665-
'algorithm', 'cert_usage', 'hash_type', 'selector', 'key_tag', 'flag', 'tag']:
712+
'algorithm', 'cert_usage', 'hash_type', 'selector', 'key_tag', 'flag', 'tag', 'tags', 'comment']:
666713
if param in kwargs:
667714
params[param] = kwargs[param]
668715
else:
@@ -798,6 +845,9 @@ def ensure_dns_record(self, **kwargs):
798845
}
799846
search_value = None
800847

848+
new_record['comment'] = params['comment'] or None
849+
new_record['tags'] = params['tags'] or []
850+
801851
zone_id = self._get_zone_id(params['zone'])
802852
records = self.get_dns_records(params['zone'], params['type'], search_record, search_value)
803853
# in theory this should be impossible as cloudflare does not allow
@@ -826,6 +876,10 @@ def ensure_dns_record(self, **kwargs):
826876
do_update = True
827877
if (params['type'] == 'CNAME') and (cur_record['content'] != new_record['content']):
828878
do_update = True
879+
if cur_record['comment'] != new_record['comment']:
880+
do_update = True
881+
if sorted(cur_record['tags']) != sorted(new_record['tags']):
882+
do_update = True
829883
if do_update:
830884
if self.module.check_mode:
831885
result = new_record
@@ -856,11 +910,13 @@ def main():
856910
account_email=dict(type='str', required=False),
857911
algorithm=dict(type='int'),
858912
cert_usage=dict(type='int', choices=[0, 1, 2, 3]),
913+
comment=dict(type='str'),
859914
hash_type=dict(type='int', choices=[1, 2]),
860915
key_tag=dict(type='int', no_log=False),
861916
port=dict(type='int'),
862917
flag=dict(type='int', choices=[0, 1]),
863918
tag=dict(type='str', choices=['issue', 'issuewild', 'iodef']),
919+
tags=dict(type='list', elements='str'),
864920
priority=dict(type='int', default=1),
865921
proto=dict(type='str'),
866922
proxied=dict(type='bool', default=False),

0 commit comments

Comments
 (0)