Add or Delete DNS records in Infoblox using Curl

Infoblox has a handy Web API that you can use to programmatically edit DNS records if you’re using it to manage DNS at your organization. (This is especially helpful when you do it via AWS CloudFormation templates using a Lambda-backed custom resource, since you can have it automatically add, update, and delete records when you create, update, and delete your stacks. (I demo that in my next post.) Before you write a program to automate DNS entries though, you’ll want to test it using a command line tool like “Curl” in Linux. I didn’t find Infoblox’s WAPI documentation particularly helpful, so after some trial and error, here are the basics that I found work well. Read on to save some of the same headaches of figuring this all out, and just get going.

 

To add an A record to DNS enter something like this at the Linux command line:

 curl -k -u <someusername> -H "Content-Type:application/json" -X POST 'https://ipam.yourdomain.com/wapi/v1.2/record:a' -d '{"name":"servername.yourdomain.com","ipv4addr":"10.1.2.3","comment":"hello world"}'

Then, to add a PTR record to DNS do this:

curl -k -u <someusername> -H "Content-Type:application/json" -X POST 'https://ipam.yourdomain.com/wapi/v1.2/record:ptr' -d '{"ptrdname":"servername.yourdomain.com","ipv4addr":"10.1.2.3","comment":"hi there"}'

NOTE: if you add a PTR record too quickly after an A record, it sometimes will look like it’s working, but the record won’t actually appear in Infoblox. Seems like a bug in Infoblox to me, but I found the easiest way to get around it is to build a 30-second pause into your workflow, to give it a break between adding the A and PTR records, so it can catch its breath before being pushed it too hard. ;)

 

To REMOVE A & PTR records, you first must look up the record to remove:

curl -k -u <someusername> 'https://ipam.yourdomain.com/wapi/v2.1/record:a?name=servername.yourdomain.com'

…which yields something like this:

[
 {
 "_ref": "record:a/ZG5zLmmRfYSQuX2RlZmF1bHQuYLndsZ29yZSxzc2NodWxlcnRlc3QsMTAuMjI:servername.yourdomain.com/default",
 "ipv4addr": "10.1.2.3",
 "name": "servername.yourdomain.com",
 "view": "default"
 }
]

Then, you use the results of that “_ref” line at the end of your URL with curl, so it knows which record to delete:

curl -k -u <someusername> -X DELETE 'https://ipam.yourdomain.com/wapi/v2.1/record:a/ZG5zLmmRfYSQuX2RlZmF1bHQuYLndsZ29yZSxzc2NodWxlcnRlc3QsMTAuMjI:servername.yourdomain.com/default'

NOTE: This deletes both the A & PTR records at once, but for some reason, the PTR record sometimes lingers on the system for another 30-seconds or so before finally going away after running this command. (Maybe they have their own 30-second pause built into their code, like I mentioned I use in my note above!)

One last thing to note is you can also “PUT” to update the existing record, but I found this to be challenging to get working reliably. Sometimes it would delete the PTR record when it updated the A, so I’ve found it’s always safest to simply delete and re-create records whenever needing to update. Again, be sure to build a 30-second pause into your script when coding this, since it needs a break after deleting the record before adding one again.

Happy automating!
Steve