Ansible 删除 AWS Route53 TXT 记录

发布于 2025-01-16 16:49:21 字数 5278 浏览 0 评论 0原文

我正在尝试使用 Ansible 删除 AWS Route53 TXT 记录

这是我的剧本的一部分

- name: "Retrieve the details for {{ item }}.{{ build_number }}.{{ internal_domain }} TXT Record"
  community.aws.route53:
    state: get
    private_zone: true
    record: "{{ item }}.{{ build_number }}.{{ internal_domain }}"
    type: TXT
    zone: "{{ internal_domain }}"
  register: rec_TXT

- name: display record
  debug: var=rec_TXT

- name: "Delete {{ item }}.{{ build_number }}.{{ internal_domain }} TXT Record"
  community.aws.route53:
    state: absent
    private_zone: true
    record: "{{ rec_TXT.set.record }}"
    ttl: "{{ rec_TXT.set.ttl }}"
    type: "{{ rec_TXT.set.type }}"
    value: "{{ rec_TXT.set.value | string }}"
    zone: "{{ rec_TXT.set.zone }}"
  when: rec_TXT.set | length > 0

,这会导致错误

    "msg": "[Tried to delete resource record set [name='dashboard.uat1tx.test.xyz.internal.', type='TXT'] but the rdata provided is invalid]"

在详细模式下运行剧本时 (-vvv) get 请求产生

ok: [localhost] => {
    "rec_TXT": {
        "changed": false,
        "failed": false,
        "nameservers": [
            "ns-1536.awsdns-00.co.uk.",
            "ns-0.awsdns-00.com.",
            "ns-1024.awsdns-00.org.",
            "ns-512.awsdns-00.net."
        ],
        "set": {
            "alias": false,
            "failover": null,
            "health_check": null,
            "hosted_zone_id": "HIAAGVXXXXPM9",
            "identifier": null,
            "record": "dashboard.uat1tx.test.xyz.internal.",
            "region": null,
            "ttl": "300",
            "type": "TXT",
            "value": "\"heritage=external-dns,external-dns/owner=SST4985-EKSCluster-uat1tx,external-dns/resource=service/default/k8s-dashboard-kubernetes-dashboard\"",
            "values": [
                "\"heritage=external-dns,external-dns/owner=SST4985-EKSCluster-uat1tx,external-dns/resource=service/default/k8s-dashboard-kubernetes-dashboard\""
            ],
            "weight": null,
            "zone": "test.xyz.internal."
        }
    }
}

absent 播放产生

The full traceback is:
  File "/tmp/ansible_community.aws.route53_payload_xb_ilskb/ansible_community.aws.route53_payload.zip/ansible_collections/community/aws/plugins/modules/route53.py", line 687, in main
  File "/tmp/ansible_community.aws.route53_payload_xb_ilskb/ansible_community.aws.route53_payload.zip/ansible_collections/community/aws/plugins/modules/route53.py", line 457, in invoke_with_throttling_retries
  File "/tmp/ansible_community.aws.route53_payload_xb_ilskb/ansible_community.aws.route53_payload.zip/ansible_collections/community/aws/plugins/modules/route53.py", line 453, in invoke_with_throttling_retries
  File "/tmp/ansible_community.aws.route53_payload_xb_ilskb/ansible_community.aws.route53_payload.zip/ansible_collections/community/aws/plugins/modules/route53.py", line 428, in commit
  File "/tmp/ansible_community.aws.route53_payload_xb_ilskb/ansible_community.aws.route53_payload.zip/ansible_collections/community/aws/plugins/modules/route53.py", line 422, in commit
  File "/usr/local/lib/python3.8/site-packages/boto/route53/record.py", line 168, in commit
    return self.connection.change_rrsets(self.hosted_zone_id, self.to_xml())
  File "/usr/local/lib/python3.8/site-packages/boto/route53/connection.py", line 473, in change_rrsets
    raise exception.DNSServerError(response.status,
fatal: [localhost]: FAILED! => {
    "changed": false,
    "invocation": {
        "module_args": {
            "alias": null,
            "alias_evaluate_target_health": false,
            "alias_hosted_zone_id": null,
            "aws_access_key": null,
            "aws_ca_bundle": null,
            "aws_config": null,
            "aws_secret_key": null,
            "debug_botocore_endpoint_logs": false,
            "ec2_url": null,
            "failover": null,
            "health_check": null,
            "hosted_zone_id": null,
            "identifier": null,
            "overwrite": null,
            "private_zone": true,
            "profile": null,
            "record": "dashboard.uat1tx.test.xyz.internal.",
            "region": null,
            "retry_interval": 500,
            "security_token": null,
            "state": "absent",
            "ttl": 300,
            "type": "TXT",
            "validate_certs": true,
            "value": [
                "\"\"heritage=external-dns",
                "external-dns/owner=SST4985-EKSCluster-uat1tx",
                "external-dns/resource=service/default/k8s-dashboard-kubernetes-dashboard\"\""
            ],
            "vpc_id": null,
            "wait": false,
            "wait_timeout": 300,
            "weight": null,
            "zone": "test.xyz.internal."
        }
    },
    "msg": "[Tried to delete resource record set [name='dashboard.uat1tx.test.xyz.internal.', type='TXT'] but the rdata provided is invalid]"
}

问题在于值中的逗号。

有人提出了问题,但没有提供提示。 https://github.com/ansible/ansible/issues/58084

我该如何将“字面”刺痛传递给值选项?

任何人都可以提供任何提示/解决方案吗?!!!

I'm trying to delete AWS Route53 TXT records using Ansible

This is a section of my playbook

- name: "Retrieve the details for {{ item }}.{{ build_number }}.{{ internal_domain }} TXT Record"
  community.aws.route53:
    state: get
    private_zone: true
    record: "{{ item }}.{{ build_number }}.{{ internal_domain }}"
    type: TXT
    zone: "{{ internal_domain }}"
  register: rec_TXT

- name: display record
  debug: var=rec_TXT

- name: "Delete {{ item }}.{{ build_number }}.{{ internal_domain }} TXT Record"
  community.aws.route53:
    state: absent
    private_zone: true
    record: "{{ rec_TXT.set.record }}"
    ttl: "{{ rec_TXT.set.ttl }}"
    type: "{{ rec_TXT.set.type }}"
    value: "{{ rec_TXT.set.value | string }}"
    zone: "{{ rec_TXT.set.zone }}"
  when: rec_TXT.set | length > 0

this results in the error

    "msg": "[Tried to delete resource record set [name='dashboard.uat1tx.test.xyz.internal.', type='TXT'] but the rdata provided is invalid]"

When running the playbook in verbose mode (-vvv)
the get request produces

ok: [localhost] => {
    "rec_TXT": {
        "changed": false,
        "failed": false,
        "nameservers": [
            "ns-1536.awsdns-00.co.uk.",
            "ns-0.awsdns-00.com.",
            "ns-1024.awsdns-00.org.",
            "ns-512.awsdns-00.net."
        ],
        "set": {
            "alias": false,
            "failover": null,
            "health_check": null,
            "hosted_zone_id": "HIAAGVXXXXPM9",
            "identifier": null,
            "record": "dashboard.uat1tx.test.xyz.internal.",
            "region": null,
            "ttl": "300",
            "type": "TXT",
            "value": "\"heritage=external-dns,external-dns/owner=SST4985-EKSCluster-uat1tx,external-dns/resource=service/default/k8s-dashboard-kubernetes-dashboard\"",
            "values": [
                "\"heritage=external-dns,external-dns/owner=SST4985-EKSCluster-uat1tx,external-dns/resource=service/default/k8s-dashboard-kubernetes-dashboard\""
            ],
            "weight": null,
            "zone": "test.xyz.internal."
        }
    }
}

The absent play produced

The full traceback is:
  File "/tmp/ansible_community.aws.route53_payload_xb_ilskb/ansible_community.aws.route53_payload.zip/ansible_collections/community/aws/plugins/modules/route53.py", line 687, in main
  File "/tmp/ansible_community.aws.route53_payload_xb_ilskb/ansible_community.aws.route53_payload.zip/ansible_collections/community/aws/plugins/modules/route53.py", line 457, in invoke_with_throttling_retries
  File "/tmp/ansible_community.aws.route53_payload_xb_ilskb/ansible_community.aws.route53_payload.zip/ansible_collections/community/aws/plugins/modules/route53.py", line 453, in invoke_with_throttling_retries
  File "/tmp/ansible_community.aws.route53_payload_xb_ilskb/ansible_community.aws.route53_payload.zip/ansible_collections/community/aws/plugins/modules/route53.py", line 428, in commit
  File "/tmp/ansible_community.aws.route53_payload_xb_ilskb/ansible_community.aws.route53_payload.zip/ansible_collections/community/aws/plugins/modules/route53.py", line 422, in commit
  File "/usr/local/lib/python3.8/site-packages/boto/route53/record.py", line 168, in commit
    return self.connection.change_rrsets(self.hosted_zone_id, self.to_xml())
  File "/usr/local/lib/python3.8/site-packages/boto/route53/connection.py", line 473, in change_rrsets
    raise exception.DNSServerError(response.status,
fatal: [localhost]: FAILED! => {
    "changed": false,
    "invocation": {
        "module_args": {
            "alias": null,
            "alias_evaluate_target_health": false,
            "alias_hosted_zone_id": null,
            "aws_access_key": null,
            "aws_ca_bundle": null,
            "aws_config": null,
            "aws_secret_key": null,
            "debug_botocore_endpoint_logs": false,
            "ec2_url": null,
            "failover": null,
            "health_check": null,
            "hosted_zone_id": null,
            "identifier": null,
            "overwrite": null,
            "private_zone": true,
            "profile": null,
            "record": "dashboard.uat1tx.test.xyz.internal.",
            "region": null,
            "retry_interval": 500,
            "security_token": null,
            "state": "absent",
            "ttl": 300,
            "type": "TXT",
            "validate_certs": true,
            "value": [
                "\"\"heritage=external-dns",
                "external-dns/owner=SST4985-EKSCluster-uat1tx",
                "external-dns/resource=service/default/k8s-dashboard-kubernetes-dashboard\"\""
            ],
            "vpc_id": null,
            "wait": false,
            "wait_timeout": 300,
            "weight": null,
            "zone": "test.xyz.internal."
        }
    },
    "msg": "[Tried to delete resource record set [name='dashboard.uat1tx.test.xyz.internal.', type='TXT'] but the rdata provided is invalid]"
}

The issue is with the commas in the values.

Someone has raised an issue, but no tips provided. https://github.com/ansible/ansible/issues/58084

How can I pass the 'literal' sting the the value option?

Can anyone provide any tips/solutions please?!!!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

骄傲 2025-01-23 16:49:21

由于我有一个类似的用例,但使用其他 REST API,因此我想在这里分享我的解决方案方法。

在我的用例中,还需要 CSV_STRING。就像

    CSV_STRING:
      '
         "heritage=external-dns",
         "external-dns/owner=SST4985-EKSCluster-uat1tx",
         "external-dns/resource=service/default/k8s-dashboard-kubernetes-dashboard"
      '

我设置值一样

{{ CSV_STRING | trim | replace(' ', '') }}

如果我已经将其作为列表,

---

CSV_STRING
  - '"heritage=external-dns"'
  - '"external-dns/owner=SST4985-EKSCluster-uat1tx"'
  - '"external-dns/resource=service/default/k8s-dashboard-kubernetes-dashboard"'

我可以使用设置值

{{ CSV_STRING | join(',') }}

但是,查看 ansible-collections/community.aws/blob/main/plugins/modules/route53.py,看起来 value 不需要字符串。

def main():
    argument_spec = dict(
...
        value=dict(type='list', elements='str'),
...

以及 route53 的文档 –在 Amazon Route 53 DNS 服务中添加或删除条目 对于参数 ,需要一个字符串元素列表。

这意味着,您需要执行相反的操作,将逗号上的字符串拆分为列表。

进一步问答

Since I have a somehow similar use case, but with an other REST API, I wanted to share my solution approach here.

In my use case also a CSV_STRING is expected. Something like

    CSV_STRING:
      '
         "heritage=external-dns",
         "external-dns/owner=SST4985-EKSCluster-uat1tx",
         "external-dns/resource=service/default/k8s-dashboard-kubernetes-dashboard"
      '

I set the value with

{{ CSV_STRING | trim | replace(' ', '') }}

If I have it as list already

---

CSV_STRING
  - '"heritage=external-dns"'
  - '"external-dns/owner=SST4985-EKSCluster-uat1tx"'
  - '"external-dns/resource=service/default/k8s-dashboard-kubernetes-dashboard"'

I can set the value with

{{ CSV_STRING | join(',') }}

However, looking into the current source code of ansible-collections/community.aws/blob/main/plugins/modules/route53.py, it looks like that value do not expect a string.

def main():
    argument_spec = dict(
...
        value=dict(type='list', elements='str'),
...

As well the documentation of route53 – add or delete entries in Amazons Route 53 DNS service say that for parameter value, a list of string elements is expected.

This means, you would need to do the opposite, splitting up your string on comma into a list before.

Further Q&A

暮年慕年 2025-01-23 16:49:21

我作弊并默认使用命令模块

我仍然想使用route53模块并解决问题。

我有一个模板

{ 
  "Comment": "Record Set Delete Changes",
  "Changes": [
    {
      "Action": "DELETE",
      "ResourceRecordSet": {
        "Name": "{{ rec_TXT.set.record }}",
        "Type": "TXT",
        "TTL" : {{ rec_TXT.set.ttl }},
        "ResourceRecords": [
          {
            "Value": "\"{{ rec_TXT.set.value[1:-1] }}\""
          }
        ]
      }
    }
  ]
}

戏剧看起来像

- name: "Retrieve the details for {{ item }}.{{ build_number }}.{{ internal_domain }} TXT Record"
  community.aws.route53:
    state: get
    private_zone: true
    record: "{{ item }}.{{ build_number }}.{{ internal_domain }}"
    type: TXT
    zone: "{{ internal_domain }}"
  register: rec_TXT

- block:
  - name: Create the JSON file to delete the TXT record
    ansible.builtin.template:
      src: delete_txt_record.json.j2
      dest: "{{ output_dir }}/{{ item }}_delete_txt_record.json"
      owner: test
      group: test
      mode: '0644'
  - name: "Delete {{ item }}.{{ build_number }}.{{ internal_domain }} TXT Record"
    ansible.builtin.command:
      cmd: "/usr/local/bin/aws route53 change-resource-record-sets --hosted-zone-id {{ rec_TXT.set.hosted_zone_id }} --change-batch file://{{ output_dir }}/{{ item }}_delete_txt_record.json"
    when: rec_TXT.set | length > 0

  when: rec_TXT.set | length > 0

I cheated and defaulted to the command module

I would still like to use the route53 module and resolve the issue.

I have a template

{ 
  "Comment": "Record Set Delete Changes",
  "Changes": [
    {
      "Action": "DELETE",
      "ResourceRecordSet": {
        "Name": "{{ rec_TXT.set.record }}",
        "Type": "TXT",
        "TTL" : {{ rec_TXT.set.ttl }},
        "ResourceRecords": [
          {
            "Value": "\"{{ rec_TXT.set.value[1:-1] }}\""
          }
        ]
      }
    }
  ]
}

The plays looks like

- name: "Retrieve the details for {{ item }}.{{ build_number }}.{{ internal_domain }} TXT Record"
  community.aws.route53:
    state: get
    private_zone: true
    record: "{{ item }}.{{ build_number }}.{{ internal_domain }}"
    type: TXT
    zone: "{{ internal_domain }}"
  register: rec_TXT

- block:
  - name: Create the JSON file to delete the TXT record
    ansible.builtin.template:
      src: delete_txt_record.json.j2
      dest: "{{ output_dir }}/{{ item }}_delete_txt_record.json"
      owner: test
      group: test
      mode: '0644'
  - name: "Delete {{ item }}.{{ build_number }}.{{ internal_domain }} TXT Record"
    ansible.builtin.command:
      cmd: "/usr/local/bin/aws route53 change-resource-record-sets --hosted-zone-id {{ rec_TXT.set.hosted_zone_id }} --change-batch file://{{ output_dir }}/{{ item }}_delete_txt_record.json"
    when: rec_TXT.set | length > 0

  when: rec_TXT.set | length > 0
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文