Created
June 11, 2013 13:22
-
-
Save ptman/5756780 to your computer and use it in GitHub Desktop.
Build dhcpd.conf based on information in LDAP, using erb templating in puppet.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# file managed by puppet | |
<%# Copyright (c) 2013, ZenRobotics Ltd. All rights reserved. %> | |
<% | |
require 'ipaddr' | |
raise Puppet::Error, 'Could not set up LDAP Connection: Missing ruby/ldap libraries' unless Puppet.features.ldap? | |
conn = Puppet::Util::Ldap::Connection.instance | |
conn.start | |
connection = conn.connection | |
base = Puppet.settings[:ldapbase] | |
filter = '(&(objectClass=dhcpServer)(cn=%s))' % @dhcp_server_cn | |
dn = nil | |
connection.search(base, LDAP::LDAP_SCOPE_SUBTREE, filter, | |
'dhcpServiceDN') do |server| | |
dns = server.vals('dhcpServiceDN') | |
if dns.nil? or dns.size != 1 | |
raise Puppet::Error, 'Could not find dhcpServiceDN for %s' % @dhcp_server_cn | |
else | |
dn = server.vals('dhcpServiceDN')[0] | |
end | |
end | |
connection.search(dn, LDAP::LDAP_SCOPE_BASE, '(objectClass=dhcpService)', | |
['dhcpStatements', 'dhcpOption']) do |service| | |
statements = service.vals('dhcpStatements') | |
if !statements.nil? | |
statements.each do |statement| | |
-%> | |
<%= statement %>; | |
<% | |
end # statements.each | |
end # if statements | |
options = service.vals('dhcpOption') | |
if !options.nil? | |
options.each do |option| | |
-%> | |
option <%= option %>; | |
<% | |
end # options.each | |
end # if options | |
connection.search(dn, LDAP::LDAP_SCOPE_SUBTREE, '(objectClass=dhcpSubnet)', | |
['cn', 'dhcpNetMask', 'dhcpStatements', | |
'dhcpOption']) do |subnet| | |
cn = subnet.vals('cn')[0] | |
cidr = subnet.vals('dhcpNetMask')[0] | |
ipaddr = IPAddr.new('0.0.0.0/' + cidr).inspect | |
netmask = ipaddr[ipaddr.index('/')+1..ipaddr.index('>')-1] | |
-%> | |
subnet <%= cn %> netmask <%= netmask %> { | |
<% | |
statements = subnet.vals('dhcpStatements') | |
if !statements.nil? | |
statements.each do |statement| | |
-%> | |
<%= statement %>; | |
<% | |
end # statements.each | |
end # if statements | |
options = subnet.vals('dhcpOption') | |
if !options.nil? | |
options.each do |option| | |
-%> | |
option <%= option %>; | |
<% | |
end # options.each | |
end # if options | |
connection.search(subnet.dn, LDAP::LDAP_SCOPE_SUBTREE, | |
'(objectClass=dhcpHost)', ['cn', 'dhcpHWAddress', | |
'dhcpStatements', 'dhcpOption']) do |host| | |
cn = host.vals('cn')[0] | |
-%> | |
host <%= cn %> { | |
<% | |
hardware = host.vals('dhcpHWAddress') | |
if !hardware.nil? | |
-%> | |
hardware <%= hardware[0] %>; | |
<% | |
end # if hardware | |
statements = host.vals('dhcpStatements') | |
if !statements.nil? | |
statements.each do |statement| | |
-%> | |
<%= statement %>; | |
<% | |
end # statements.each | |
end # if statements | |
options = host.vals('dhcpOption') | |
if !options.nil? | |
options.each do |option| | |
-%> | |
option <%= option %>; | |
<% | |
end # options.each | |
end # if options | |
-%> | |
} | |
<% | |
end # host | |
connection.search(subnet.dn, LDAP::LDAP_SCOPE_SUBTREE, | |
'(objectClass=dhcpPool)', ['dhcpRange']) do |pool| | |
range = pool.vals('dhcpRange')[0] | |
-%> | |
pool { | |
range <%= range %>; | |
} | |
<% | |
end # pool | |
-%> | |
} | |
<% | |
end # subnet | |
end # service | |
-%> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment