Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
;; My IP address data is shaped like this:
(def data {"host-a" {:ip "1.1.1.1"}
"host-b" {:ip "2.2.2.2"}
"container" {"host-c" {:ip "3.3.3.3"}
"host-d" {:ip "4.4.4.4"}}})
;; This logic turns that data into a map ("set" in nix) that would work for `networking.hosts`:
(defn build-hosts [ip-data & [name-prefix]]
(reduce-kv (fn [result hostname {:keys [ip] :as info}]
(let [full-hostname (if name-prefix
(str name-prefix "-" hostname)
hostname)]
(if ip
(assoc result ip [full-hostname])
(merge result (build-hosts info hostname)))))
{}
ip-data))
(build-hosts data)
=> {"1.1.1.1" ["host-a"],
"2.2.2.2" ["host-b"],
"3.3.3.3" ["container-host-c"],
"4.4.4.4" ["container-host-d"]}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.