StackExchange / dnscontrol

Infrastructure as code for DNS!
https://dnscontrol.org/
MIT License
3.14k stars 400 forks source link

Duplicate Domains Same Records Except for handful #831

Closed misilot closed 4 years ago

misilot commented 4 years ago

Hello,

I am trying to do the following, and it works. I just want to see if there is a better way to do this. Lets say I have test.example.com and test.example.org. They are identical and should have the same records except for specific use cases. These tend to be one offs that are pointing at 3rd party systems that can't be configured to respond to both something.test.example.com and something.test.example.org.

var RECORDS = [
    A('@', '10.10.10.11'),
    CNAME('test', 'website.com.'),
    CNAME('www', 'website.com.')
    A('service', '10.10.10.10'),
];

D("test.example.com", NO_REGISTRAR,
    DnsProvider(AWS_GLOBAL),
    RECORDS,
    CNAME('something', 'secure.something.com.')
)

D("test.example.org", NO_REGISTRAR,
    DnsProvider(AWS_GLOBAL),
    RECORDS,
    CNAME('something', 'redirect.example.org.')
)

Where something.test.example.com can go directly to secure.something.com and respond correctly, and something.test.example.org goes to a box that redirects the site to something.test.example.com

This is what I came up with, but not sure if it is the best way to do it. Thanks!

tlimoncelli commented 4 years ago

That's exactly right.

We do something similar. We use those lists like macros. We have 3 different ones for MX records (each domain gets the appropriate one), one for Google Apps domains (so everyone sets the records consistently), and so on.

If they are exactly the same, you can use a loop:

// Set all these domains to use the same records:
_.each(
  [ 
    "example1.tld",
    "example2.tld",
    "example3.tld",
  ],
  function (d) {
    D(d, REG_NAMECOM, DnsProvider(NAMECOM),
       A("@", "10.2.3.4"),
       CNAME("www", "@"),
    END);
  }
);

I only use the loop if the domain records are exactly the same for each. For example, we do that for parked domains. As soon as one item in the list deviates, we take it out of the loop and define it individually. We don't have to do this, but we find that it keeps things simple for the next person that has to read and understand the config file.

In the next release (or master if you want to use the feature now) there will be a D_EXTEND() function that adds to a domain. I would use this with caution as you don't want to make the configuration too unreadable.

Tom

misilot commented 4 years ago

Thank you! I did take a look at D_EXTEND() through the docker environment earlier and maybe I was misunderstanding it's usage but I was thinking I could do something like

var RECORDS = [
    A('@', '10.10.10.11'),
    CNAME('test', 'website.com.'),
    CNAME('www', 'website.com.')
    A('service', '10.10.10.10'),
    CNAME('something', 'secure.something.com.')
];
D("test.example.com", NO_REGISTRAR,
    DnsProvider(AWS_GLOBAL),
    RECORDS
)
D("test.example.org", NO_REGISTRAR,
    DnsProvider(AWS_GLOBAL),
    RECORDS
)
D_EXTEND("test.example.org",
    CNAME('something', 'redirect.example.org.')
)

But I get the error ERROR: Cannot have multiple CNAMEs with same name: something.test.example.org

At least looking at the example provided it made it seem like it would just overwrite that value with the value in D_EXTEND().

tlimoncelli commented 4 years ago

Yeah, D_EXTEND() always tries to add records, never replace them.

Hope that helps!

misilot commented 4 years ago

Yes thank you!