Skip to content
Snippets Groups Projects
Commit a3d95ec7 authored by Thomas Bellman's avatar Thomas Bellman
Browse files

Add 'default_self' flag to parse_host_mac_ip_list().

This adds a new flag 'default_self' to the parse_host_mac_ip_list()
function.  When given, the IP address list will default to consist
of the hostname if neither any addresses nor an explicity "-" are
listed.  In combination with the 'resolve' flag, the hostname will
be resolved into numeric IP addresses; else it will behave as if the
hostname was surrounded by square brackets, and thus added to the
'ip_Name' element in the result.

This is useful when the IP addresses are provided outside of the
hostlist, e.g. in DNS, NIS or LDAP.  Users can then provide lists
of just the hostnames and the MAC addresses, have the hostnames
resolved into IP addresses, and generate e.g. DHCP config files
with numeric addresses in them.

Another potential use case, could be to generate /etc/hosts files
that effectively act as caches for DNS, allowing a node to do
hostname lookups of the most common names or addresses even if the
catalogue service is unavailable.
parent 8cea3e4c
No related branches found
No related tags found
No related merge requests found
......@@ -25,6 +25,8 @@ module Puppet::Parser::Functions
list of such flag string.
The following flags are recognized:
- resolve Resolve names in the IP address section.
- default_self Default IP address list to the hostname if
no addresses are given.
The lines in the hostlist_lines parameter must be on the format
......@@ -50,6 +52,12 @@ module Puppet::Parser::Functions
"ipv6" elements as appropriate; the name itself will not be added to
the "ip_Name" element.
The "default_self" flag causes the IP address list to default to the
hostname if no addresses are listed, and "-" is not explicitly given.
If the "resolve" flag is given, the name will be resolved, else it
will just be added to the "ip_Name" element, as if surrounded by
square brackets.
The return value is a hash with the host names as keys, and the
value for each host being a hash with the following items:
- "mac" The MAC addresses (if any) for the host
......@@ -138,6 +146,7 @@ module Puppet::Parser::Functions
hostlist_lines = [args[0]].flatten.join("\n").split("\n")
flags = [(args[1] or [])].flatten.join(",").split(",")
do_resolve = flags.delete('resolve')
do_default_self = flags.delete('default_self')
if not flags.empty?
raise(Puppet::ParseError,
"parse_host_mac_ip_list(): Bad flag ``#{flags[0]}''")
......@@ -153,7 +162,9 @@ module Puppet::Parser::Functions
hostlist_lines.each do |hostline|
hostname,macs,*ips = hostline.split()
macaddrs = (macs == "-") ? [] : [macs]
if ips == ["-"]
if ips.empty? and do_default_self
ips = do_resolve ? [ hostname ] : [ "["+hostname+"]" ]
elsif ips == ["-"]
ips = []
end
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment