/etc/hosts 'cname'?

1,111 views
Skip to first unread message

Dick Davies

unread,
Feb 24, 2010, 1:55:30 AM2/24/10
to puppet...@googlegroups.com
I want to use a CNAME as a level of indirection
( to point N webservers at the active mySQL replica).

I'd like to use /etc/hosts to avoid the DNS propagation delays.

I don't want to do away with DNS altogether though - is it possible to lookup
a hostname and pass that IP to the hosts type? Something like:

# this gets used by several classes
$activedbhost = "mysql1.mydomain.com"

host { "db.hosting.mydomain.com":
ensure => present,
ip => ip_of($activedbhost)
}

'ip_of()' is what Puppet calls a 'function', right?
Does anyone have a reference on how to write them -
the reductive labs docs seem a little broken today.

Or better still, has someone already written such a thing :)

Dick Davies

unread,
Feb 24, 2010, 9:03:15 AM2/24/10
to puppet...@googlegroups.com
On Wed, Feb 24, 2010 at 6:55 AM, Dick Davies
<rasp...@hellooperator.net> wrote:

> Does anyone have a reference on how to write functions -


> the reductive labs docs seem a little broken today.

Never mind, found it.

http://reductivelabs.com/trac/puppet/wiki/WritingYourOwnFunctions

Thomas Bellman

unread,
Feb 24, 2010, 9:07:44 AM2/24/10
to puppet...@googlegroups.com
Dick Davies wrote:

> # this gets used by several classes
> $activedbhost = "mysql1.mydomain.com"
>
> host { "db.hosting.mydomain.com":
> ensure => present,
> ip => ip_of($activedbhost)
> }
>
> 'ip_of()' is what Puppet calls a 'function', right?
> Does anyone have a reference on how to write them -
> the reductive labs docs seem a little broken today.
>
> Or better still, has someone already written such a thing :)

As a matter of fact, yes, I have. :-)

In my "nsc-puppet-utils" module, I have a function resolve_ipnets()
which does that. (It does have some additional features as well.
For example, it resolves names on the form "foo.example.com/24"
to "10.20.30.17/24", and it handles lists of hostnames as well.)

Do a 'git clone http://www.nsc.liu.se/~bellman/nsc-puppet-utils.git'
to get my module. There is documentation inside the file
plugins/puppet/parser/functions/resolve_ipnets.rb.


A little note, though. If you use this to populate /etc/hosts from
DNS, and you do so on your Puppetmaster, you won't be able to change
the address of that name. The next time resolve_ipnets() is called,
it will find the address in /etc/hosts, and then you're stuck. (You
can of course remove that entry from /etc/hosts manually, and then
you'll be OK again, but that's cheating. :-)


/Bellman

Dick Davies

unread,
Feb 25, 2010, 5:25:10 AM2/25/10
to puppet...@googlegroups.com
On Wed, Feb 24, 2010 at 2:07 PM, Thomas Bellman <bel...@nsc.liu.se> wrote:
> Dick Davies wrote:

>> host { "db.hosting.mydomain.com":
>>   ensure => present,
>>   ip          => ip_of($activedbhost)
>> }
>>
>> 'ip_of()' is what Puppet calls a 'function', right?
>> Does anyone have a reference on how to write them -
>> the reductive labs docs seem a little broken today.
>>
>> Or better still, has someone already written such a thing :)
>
> As a matter of fact, yes, I have. :-)
>
> In my "nsc-puppet-utils" module, I have a function resolve_ipnets()
> which does that.  (It does have some additional features as well.
> For example, it resolves names on the form "foo.example.com/24"
> to "10.20.30.17/24", and it handles lists of hostnames as well.)
>
> Do a 'git clone http://www.nsc.liu.se/~bellman/nsc-puppet-utils.git'
> to get my module.

Nice, thanks - lot of useful stuff there. I went for a dirt simple:

[rasputnik@puppet lamp]# cat plugins/puppet/parser/functions/ip_of.rb
module Puppet::Parser::Functions
require 'resolv'
newfunction(:ip_of, :type => :rvalue) do |args|
hostname = args[0]
Resolv.getaddress(hostname)
end
end

So I can now do:

class webserver {
...
host { "$lamp::cfg::mysql_alias":
ensure => present,
ip => ip_of("$lamp::cfg::mysql_master")
}
...
}

and the webservers all get an /etc/hosts entry for
whichever mysql backend is set to be active in my settings class
(to avoid having to edit 150+ wp-config.phps).

A 2:5 useful code to boilerplate ratio is pretty good for a plugin
framework, so I think I'll stick with mine for now (and maybe pinch
yours if we need to generalize things :) )

Thanks again for the other functions, though. I'll use them as a reference
when we start getting more heavily into custom functions.

Reply all
Reply to author
Forward
0 new messages