简体   繁体   中英

Ansible Deleting AWS Route53 TXT records

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?!!!

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM