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

Enable host-to-IP resolving in parse_host_mac_ip_list().

This adds functionality to the parse_host_mac_ip_list() function
making it able to resolve hostnames into numeric IP addresses.
Users can now have lines looking like

	www	01:23:45:67:89:ab	www.example.com

and the "www.example.com" name will be resolved into IP address(es)
that are added to the 'ipv4' and 'ipv6' elements as appropriate.

This is however not the default behaviour.  To enable it, users
must specify the flag 'resolve' in the new (optional) ``flags''
parameter to the function.
parent 38b974ce
No related branches found
No related tags found
No related merge requests found
......@@ -12,11 +12,23 @@ module Puppet::Parser::Functions
This function parses lines containing hostname, MAC address, and IP
address(es), returning a hash mapping from hostname to a records
(in the form of hashes) with the MAC and IP addresses. The single
parameter is a string, or (nested) list of strings, containing
lines. Each line should be on the format
(in the form of hashes) with the MAC and IP addresses. It takes two
parameters:
NAME MACADDR [IPADDR ...]
* hostlist_lines (mandatory)
String or (nested) list of strings containing lines of hosts
with MAC and IP addresses. See below for more details.
* flags (optional)
Flags altering the behaviour of the function. Flags can be
given as comma-separated names in a string, or as a (nested)
list of such flag string.
The following flags are recognized:
- resolve Resolve names in the IP address section.
The lines in the hostlist_lines parameter must be on the format
NAME MACADDR [IPADDR ...]
i.e, a name, a MAC address, and zero, one or more IP addresses, all
separated by horizontal whitespace.
......@@ -32,6 +44,12 @@ module Puppet::Parser::Functions
returned with the brackets removed. Numeric IP addresses will be
canonicalized.
If the "resolve" flag is specified, hostnames without square brackets
around them can also be listed. Such names will be resolved into
numeric IP addresses, and those will be added to the "ipv4" and
"ipv6" elements as appropriate; the name itself will not be added to
the "ip_Name" element.
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
......@@ -95,13 +113,35 @@ module Puppet::Parser::Functions
},
}
If the "resolve" flag is specified, then the line
web - www.example.com [dub-dub-dub]
will be parsed into the element
"web" => {
"mac" => [ ],
"ipv4" => [ "192.0.2.80" ],
"ipv6" => [ "2001:db8:1::80", "2001:db8:1::443" ],
"ip_Name" => [ "dub-dub-dub" ],
"ip" => [ "192.0.2.80", "2001:db8:1::80", "2001:db8:1::443", "dub-dub-dub" ],
},
(assuming some specific entries in DNS for www.example.com).
') \
do |args|
if args.length != 1
if args.length < 1 or args.length > 2
raise(Puppet::ParseError,
"parse_host_mac_ip_list(): Wrong number of arguments")
end
hostlist_lines = [args[0]].flatten.join("\n").split("\n")
flags = [(args[1] or [])].flatten.join(",").split(",")
do_resolve = flags.delete('resolve')
if not flags.empty?
raise(Puppet::ParseError,
"parse_host_mac_ip_list(): Bad flag ``#{flags[0]}''")
end
# Skip blank lines and comments
hostlist_lines = hostlist_lines.select { |line|
......@@ -141,13 +181,32 @@ module Puppet::Parser::Functions
begin
ipa = IPAddr.new(ip)
rescue ArgumentError => err
errmsg = "parse_host_mac_ip_list(): #{err.to_s}, `#{ip}'"
raise(err.class.new(errmsg))
if not do_resolve
errmsg = "parse_host_mac_ip_list(): #{err.to_s}, `#{ip}'"
raise(err.class.new(errmsg))
else
# Ignore, and continue to see if it can be resolved
end
else
ipaddrs[ipa.family] << ipa.to_s
next
end
if do_resolve
begin
addrinfos = Socket.getaddrinfo( # No reverse lookups
ip, nil, nil, nil, nil, 0, false)
rescue SocketError => err
errmsg = "parse_host_mac_ip_list(): #{err.to_s}, `#{ip}'"
raise(err.class.new(errmsg))
else
addrinfos.each do |ai|
ipaddrs[ai[4]] << ai[3]
end
next
end
end
# Catch-all
raise(Puppet::ParseError,
"parse_host_mac_ip_list(): Invalid IP format, `#{ip}'")
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment