Editing DNS records with gcloud by reading / output from a file

I can edit DNS records using the gcloud tool, interactively editing the JSON file from vi / mate with the command:

gcloud dns records --zone=myzone edit 

However, I would like to be able to do bulk updates, something like this:

 gcloud dns records --zone=myzone edit < my-additional-records.txt 

... where my-extra-records.txt contains the DNS records I want to add.

I think this is not as simple as editing JSON file contains both addition and removal of DNS records. So any advice would be appreciated.

+6
source share
3 answers

I think you could try using the linux 'ed' line editor as follows:

 EDITOR=ed gcloud dns records --zone=myzone edit <<-EOF 12i , { "kind": "dns#resourceRecordSet", "name": "a.mydomain.org.", "rrdatas": [ "111.222.111.222" ], "ttl": 21600, "type": "A" } . ,wq EOF 

This assumes that the top of the editing file looks something like this (so you are adding an addition to the 12th line)

 { "additions": [ { "kind": "dns#resourceRecordSet", "name": "mydomain.org.", "rrdatas": [ "ns-cloud-c1.googledomains.com. dns-admin.google.com. 2 21600 3600 1209600 300" ], "ttl": 21600, "type": "SOA" }, 

Learn more about how to use ed here: http://www.gnu.org/software/ed/manual/ed_manual.html

+4
source

Providing this new answer since gcloud dns ... been greatly improved since the request of this question. Now there are two ways to edit record sets.

  • Using export / import commands as follows:

     gcloud dns record-sets export --zone my-zone RECORDS-FILE 

    By default, RECORDS-FILE is in yaml format. You can also get it in the zone file format using the --zone-file-format flag.

    Then you can edit the RECORDS-FILE using any editor you select and import the modified records using:

     gcloud dns record-sets import --zone my-zone --delete-all-existing RECORDS-FILE 

    Import also accepts --zone-file-format . If you want to simply add / edit some entries, but not all of them, you can omit the --delete-all-existing flag.

  • Using a transaction group as follows:

     gcloud dns record-sets transaction start # Start transaction gcloud dns record-sets transaction add/remove # Add remove records gcloud dns record-sets transaction execute # Execute transaction 
+1
source

I have not tried this myself, but if the JSON format is here: https://developers.google.com/cloud-dns/getting-started

Once set to deletions : [] (empty JSON array). Won't this achieve what you want (without deleting)? Is this something wrong?

Update 1:

If the problem is actually entering the channel into the program, and you cannot find a shell-based method, I found this migration script that shows how you can script to perform this action (using python).

0
source

Source: https://habr.com/ru/post/971129/


All Articles