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

New function parse_host_mac_ip_list().

This adds a function for parsing a simple line-based file format for
specifying MAC and IP addresses for hosts, returning a data structure
suitable for the networking::hostsfile::hostentries definition.
parent d4cedd90
No related branches found
No related tags found
No related merge requests found
# Copyright (C) 2020 Thomas Bellman.
# Licensed under the GNU LGPL v3+; see the README file for more information.
module Puppet::Parser::Functions
newfunction(:parse_host_mac_ip_list, :type => :rvalue, :doc => '
Parse lines of hostnames, MAC addresses and IP addresses.
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
NAME MACADDR [IPADDR ...]
i.e, a name, a MAC address, and zero, one or more IP addresses, all
separated by horizontal whitespace.
The return value is a hash with the host names as keys, and the
value for each host being a hash with two items, "ip" and "mac".
Each of those will be a list of strings. The special value "-" (a
single dash and nothing else) in the MAC address column means there
is no MAC address, and the "mac" element will be the empty list.
Blank lines are ignored. Lines starting with a hash sign ("#") are
comments, and are also skipped
The intent is to have simple text files with one line per host,
that can be used as source for generating HOSTS.TXT (/etc/hosts)
files and DHCP configuration files. This function parses such
files into a data structure that is easy to use in ERB templates
generating such files.
Example input:
# A simple comment line
foo 012345-6789ab 192.0.2.6
fie - 192.0.2.7 2001:db8:1::7
fum cdef-0123-4567
This will be parsed into the hash
{
"foo" => {
"mac" => [ "012345-6789ab" ],
"ip" => [ "192.0.2.6" ],
},
"fie" => {
"mac" => [ ],
"ip" => [ "192.0.2.7", "2001:db8:1::7" ],
},
"fum" => {
"mac" => [ "cdef-0123-4567" ],
"ip" => [ ],
},
}
Hostnames, MAC addresses and IP addresses in the input are not
validated for correctness. Multiple lines using the same hostname
are not permitted.
') \
do |args|
if args.length != 1
raise(Puppet::ParseError,
"parse_host_mac_ip_list(): Wrong number of arguments")
end
lines = [args[0]].flatten.join("\n").split("\n")
# Skip blank lines and comments
lines = lines.select {|l|
l = l.strip
l != "" and l[0] != "#"
}
hosts = { }
lines.each do |l|
hostname,macs,*ips = l.split()
macs = (macs == "-") ? [] : [macs]
if hosts.has_key?(hostname)
raise(Puppet::ParseError,
"parse_host_mac_ip_list(): Duplicate host name, `#{hostname}'")
end
hosts[hostname] = { 'ip' => ips, 'mac' => [macs].flatten }
end
return hosts
end
end
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment