Skip to content

Instantly share code, notes, and snippets.

@xanderificnl
Last active September 11, 2015 12:31
Show Gist options
  • Save xanderificnl/f3133f995736b881e74d to your computer and use it in GitHub Desktop.
Save xanderificnl/f3133f995736b881e74d to your computer and use it in GitHub Desktop.
Create multiple DNS zones with records on DigitalOcean.
# noinspection PyPackageRequirements
import digitalocean
"""
Create multiple DNS zones with records on DigitalOcean.
This Gist allows you to easily insert several dozens of domain names with the same records (it's possible to
use some variables). It's rather simple to adjust to your use-case, i.e. adding new variables, etc.
Note: Requires python-digitalocean - don't forget to update the script with your token, default IP, your domain
names and your records.
HTH
"""
def replace_all(text, dic):
for i, x in dic.items():
text = text.replace('{%s}' % i, x)
return text
# DO Token.
token = "<YOUR TOKEN>"
default_IP = '<DEFAULT IP>'
# All the domains we're going to create.
domains = [
'dom1.tld',
'dom2.tld',
'dom3.tld',
'dom4.tld'
]
records = [
{
'type': 'A',
'name': 'www',
'data': default_IP
},
{
'type': 'MX',
'name': '@',
'priority': '10',
'data': 'mail.primary.tld.'
},
{
'type': 'TXT',
'name': '@',
'data': '"v=spf1 redirect=_spf.primary.tld"'
},
{
'type': 'CNAME',
'name': '2015._domainkey',
'data': '{domain-notld}_2015._domainkey.primary.tld.'
}
]
# Create the domain names.
for domain in domains:
do_domain = digitalocean.Domain(token=token, name=domain, ip_address=default_IP)
do_domain.create()
for record in records:
record['data'] = replace_all(record['data'], {
'domain': domain,
'domain-notld': domain.split('.')[0]
})
do_domain.create_new_domain_record(**record)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment